Nginx with PHP FastCGI on CentOS

// February 26th, 2010 // Guides

Nginx is a lightweight alternative to Apache, and has nearly all the features of Apache. Nginx uses much less memory and CPU compared to Apache, and is slightly faster than Lighttpd. To get started, make sure you are using EPEL. If you’re not sure or if you’re not, run the following command:

rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-3.noarch.rpm

Download and install nginx:

yum install nginx

Now, edit the nginx configuration file, which can be found at /etc/nginx/conf.d. Modify it to the following:

worker_processes  1;

events {
	worker_connections 1024;
}

http {
	include mime.types;
	default_type text/plain;
	sendfile on;
	keepalive_timeout 15;
	gzip on;
	server {
		listen 80;
		server_name  localhost;
		location / {
			root /home;
			index index.php;
		}
		location ~ .php$ {
			root html;
			fastcgi_pass 127.0.0.1:9000;
			fastcgi_index index.php;
			fastcgi_param SCRIPT_FILENAME /home$fastcgi_script_name;
			include fastcgi_params;
		}
	}
}

You will need to modify root and fastcgi_param SCRIPT_FILENAME to your home directory. Note that either one end with a slash. After you’ve done all of above, nginx will be all set. Now, you have to install FastCGI.

yum install spawn-fcgi

Download the PHP FastCGI startup script.

wget http://fusionswift.com/files/2010/02/php_cgi.sh.zip
unzip php_cgi.sh.zip
mv php_cgi.sh /etc/init.d/php_cgi
chmod 0755 /etc/init.d/php_cgi

Start nginx and PHP FastCGI.

service nginx start
service php_cgi start

Now you’re all set! If you want to accelerate PHP execution, be sure to check out XCache.

Leave a Reply