Nginx with PHP on CentOS 6
This is a post on how to set up Nginx with PHP using spawn-fcgi on CentOS 6.
If you haven’t already done it, you have to set up the EPEL repository.
For 32bit:
rpm -Uvh http://download.fedora.redhat.com/pub/epel/6/i386/epel-release-6-5.noarch.rpm
For 64bit:
rpm -Uvh http://download.fedora.redhat.com/pub/epel/6/x86_64/epel-release-6-5.noarch.rpm
Next install Nginx and spawn-fcgi, I assume you have already installed PHP and all the modules you need.
yum install nginx spawn-fcgi
Now it’s time for the spawn-fcgi configuration. The config should be at /etc/sysconfig/spawn-fcgi
.
vim /etc/sysconfig/spawn-fcgi
# You must set some working options before the "spawn-fcgi" service will work.
# If SOCKET points to a file, then this file is cleaned up by the init script.
#
# See spawn-fcgi(1) for all possible options.
#
# Example :
#SOCKET=/var/run/php-fcgi.sock
OPTIONS="-a 127.0.0.1 -p 9000 -u nginx -g nginx -C 32 -F 1 -P /var/run/spawn-fcgi.pid -- /usr/bin/php-cgi"
It’s very important that you remember the port, you’ll have to set the same in the nginx configuration. You should also use the same username and group as nginx.
To play it safe, make sure the following line is not commented in the file /etc/init.d/spawn-fcgi
:
config="/etc/sysconfig/spawn-fcgi"
Now let’s make sure that spawn-fcgi and nginx automatically start after a reboot.
chkconfig --level 2345 nginx on
chkconfig --level 2345 spawn-fcgi on
For more information about chkconfig check this site: http://www.centos.org/docs/5/html/Deployment_Guide-en-US/s1-services-chkconfig.html
Next up we have to fix a folder permission. The group of the session cookies folder, the address can be found in the php.ini (session.save_path = “/var/lib/php/session”). The folder group has to be changed from apache to whatever you use, e. g. nginx. You should check this after every php update.
As a last step, just add the following line to the /etc/nginx.conf
and/or your custom domain configuration in /etc/nginx/conf.d/yourdomain.conf
.
location ~ .php$ {
include fastcgi_params;
fastcgi_pass localhost:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
You should also make some additional changes in your configuration. I’m going to make another post about some of the settings.
The official wiki is always a good source for help: http://wiki.nginx.org/Configuration