In this tutorial, we will show you how to install Nginx With PHP-FastCGI on CentOS 6. For those of you who didn’t know, Nginx is one of the most popular web servers in the world and is responsible for hosting some of the largest and highest-traffic sites on the internet. It is more resource-friendly than Apache in most cases and can be used as a web server or a reverse proxy.
This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple. I will show you through the step-by-step installation of Nginx with fast-CGI on CentOS 6 server.
Install Nginx With PHP-FastCGI on CentOS 6
Step 1. To install, first, you must add the EPEL yum repository information corresponding to your CentOS/RHEL version.
## RHEL/CentOS 6 32-Bit ## # wget http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm # rpm -ivh epel-release-6-8.noarch.rpm
## RHEL/CentOS 6 64-Bit ## # wget http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm # rpm -ivh epel-release-6-8.noarch.rpm
Step 2. Install Nginx package and dependencies.
Installing Nginx using the following command:
yum update yum -y install nginx
Step 3. Install the required PHP packages.
yum install php-cli php-mysql php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-eaccelerator php-magickwand php-magpierss php-mbstring php-mcrypt php-mssql php-shout php-snmp php-soap php-tidy
Step 4. Install spawn-fcgi.
Now we install spawn-fcgi
using the following command below:
yum install spawn-fcgi -y
Configure the Fast-CGI init script:
nano /etc/init.d/php-cgi
#!/bin/sh # # php-cgi - php-fastcgi swaping via spawn-fcgi # # chkconfig: - 85 15 # description: Run php-cgi as app server # processname: php-cgi # config: /etc/sysconfig/phpfastcgi (defaults RH style) # pidfile: /var/run/php-cgi.pid . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 spawnfcgi="/usr/bin/spawn-fcgi" php_cgi="/usr/bin/php-cgi" prog=$(basename $php_cgi) server_ip=127.0.0.1 server_port=9000 server_user=nginx server_group=nginx server_childs=5 pidfile="/var/run/php_cgi.pid" # do not edit, put changes in /etc/sysconfig/phpfastcgi [ -f /etc/sysconfig/phpfastcgi ] && . /etc/sysconfig/phpfastcgi start() { [ -x $php_cgi ] || exit 1 [ -x $spawnfcgi ] || exit 2 echo -n $"Starting $prog: " daemon $spawnfcgi -a ${server_ip} -p ${server_port} -u ${server_user} -g ${server_group} -P ${pidfile} -C ${server_childs} -f ${php_cgi} retval=$? echo return $retval } stop() { echo -n $"Stopping $prog: " killproc -p ${pidfile} $prog -QUIT retval=$? echo [ -f ${pidfile} ] && /bin/rm -f ${pidfile} return $retval } restart(){ stop sleep 2 start } rh_status(){ status -p ${pidfile} $prog } case "$1" in start) start;; stop) stop;; restart) restart;; status) rh_status;; *) echo $"Usage: $0 {start|stop|restart|status}" exit 3 esac
chmod +x /etc/init.d/php-cgi
Step 5. Configure Nginx web server.
#nano /etc/nginx/conf.d/idroot.us.conf server { listen 80; server_name idroot.us www.idroot.us; access_log /var/www/idroot.us/logs/access.log ; error_log /var/www/idroot.us/logs/error.log ; location / { root /var/www/idroot.us/public_html; index index.php index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /var/www/idroot.us/public_html; } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 location ~ .php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; root /var/www/idroot.us/public_html; fastcgi_param SCRIPT_FILENAME /var/www/idroot.us/public_html$fastcgi_script_name; include fastcgi_params; } location ~ /.ht { deny all; } }
Step 5. Start Nginx and PHP-FastCGI service.
etc/init.d/nginx start etc/init.d/php-cgi start
Important: For security reasons, you’ll want to change/add the following line to /etc/php.ini and restart Nginx:
cgi.fix_pathinfo=0
Congratulations! You have successfully installed Nginx and PHP-FastCGI. Thanks for using this tutorial for installing Nginx and PHP-FastCGI on CentOS 6 systems. For additional help or useful information, we recommend you check the official Nginx website.