How to Setup Nginx With PHP-FastCGI on CentOS 6.2/CentOS 6.3 VPS Server

At the previous post, i have setup Nginx as reverse proxy to apache web server. This post will covers the steps how to setup nginx web server to use PHP-FastCGI for dynamic content. Nginx (pronounced “Engine-X”) is a free, open-source HTTP Web server and one of the alternative to Apache http server. It’s a high performance edge web server with the lowest memory footprint and the key features to build modern and efficient web infrastructure. FastCGI is a language independent, scalable, open extension to CGI that provides high performance without the limitations of server specific APIs. FastCGI provides better scalability and performance.

1. Prepared yum repository for nginx :

[root@centos63 ~]# vi /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

2. Install Nginx :

[root@centos63 ~]# yum install nginx -y

3. Install required php packages :

[root@centos63 ~]# 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 -y

Examples of Software Installed and the Dependency:

Installed:
  php-eaccelerator.i686 1:0.9.6.1-1.el6   php-imap.i686 0:5.3.3-14.el6_3      php-ldap.i686 0:5.3.3-14.el6_3
  php-magickwand.i686 0:1.0.9-1.el6       php-magpierss.noarch 0:0.72-6.el6   php-mssql.i686 0:5.3.3-1.el6
  php-odbc.i686 0:5.3.3-14.el6_3          php-pear.noarch 1:1.9.4-4.el6       php-shout.i686 0:0.9.2-6.el6
  php-snmp.i686 0:5.3.3-14.el6_3          php-soap.i686 0:5.3.3-14.el6_3      php-tidy.i686 0:5.3.3-14.el6_3
  php-xml.i686 0:5.3.3-14.el6_3           php-xmlrpc.i686 0:5.3.3-14.el6_3

Dependency Installed:
  ConsoleKit.i686 0:0.4.1-3.el6                        ConsoleKit-libs.i686 0:0.4.1-3.el6
  GConf2.i686 0:2.28.0-6.el6                           ImageMagick.i686 0:6.5.4.7-6.el6_2
  ORBit2.i686 0:2.14.17-3.1.el6                        atk.i686 0:1.28.0-2.el6
  avahi-libs.i686 0:0.6.25-11.el6                      cairo.i686 0:1.8.8-3.1.el6
  cups-libs.i686 1:1.4.2-48.el6_3.1                    dbus.i686 1:1.2.24-7.el6_3
  eggdbus.i686 0:0.6-3.el6                             fontconfig.i686 0:2.8.0-3.el6
  freetds.i686 0:0.91-2.el6                            ghostscript.i686 0:8.70-14.el6_3.1
  ghostscript-fonts.noarch 0:5.50-23.1.el6             gtk2.i686 0:2.18.9-10.el6
  hicolor-icon-theme.noarch 0:0.11-1.1.el6             jasper-libs.i686 0:1.900.1-15.el6_1.1
  lcms-libs.i686 0:1.19-1.el6                          libICE.i686 0:1.0.6-1.el6
  libIDL.i686 0:0.8.13-2.1.el6                         libSM.i686 0:1.1.0-7.1.el6
  libXcomposite.i686 0:0.4.1-2.el6                     libXcursor.i686 0:1.1.10-2.el6
  libXdamage.i686 0:1.1.2-1.el6                        libXext.i686 0:1.1-3.el6
  libXfixes.i686 0:4.0.4-1.el6                         libXfont.i686 0:1.4.1-2.el6_1
  libXft.i686 0:2.1.13-4.1.el6                         libXi.i686 0:1.3-3.el6
  libXinerama.i686 0:1.1-1.el6                         libXrandr.i686 0:1.3.0-4.el6
  libXrender.i686 0:0.9.5-1.el6                        libXt.i686 0:1.0.7-1.el6
  libc-client.i686 0:2007e-11.el6                      libcroco.i686 0:0.6.2-5.el6
  libfontenc.i686 0:1.0.5-2.el6                        libgsf.i686 0:1.14.15-5.el6
  libogg.i686 2:1.1.4-2.1.el6                          librsvg2.i686 0:2.26.0-5.el6_1.1.0.1.centos
  libshout.i686 0:2.2.2-5.1.el6                        libthai.i686 0:0.1.12-3.el6
  libtheora.i686 1:1.1.0-2.el6                         libtidy.i686 0:0.99.0-19.20070615.1.el6
  libtiff.i686 0:3.9.4-6.el6_3                         libvorbis.i686 1:1.2.3-4.el6_2.1
  libwmf-lite.i686 0:0.2.8.4-22.el6.centos             lm_sensors-libs.i686 0:3.1.1-10.el6
  net-snmp.i686 1:5.5-41.el6_3.1                       net-snmp-libs.i686 1:5.5-41.el6_3.1
  pango.i686 0:1.28.1-3.el6_0.5.1.centos               pixman.i686 0:0.18.4-1.el6_0.1
  polkit.i686 0:0.96-2.el6_0.1                         sgml-common.noarch 0:0.6.3-32.el6
  speex.i686 0:1.2-0.12.rc1.1.el6                      unixODBC.i686 0:2.2.14-11.el6
  urw-fonts.noarch 0:2.4-10.el6                        xorg-x11-font-utils.i686 1:7.2-11.el6

Dependency Updated:
  dbus-libs.i686 1:1.2.24-7.el6_3

Complete!

4. Configure EPEL Repository on CentOS 6.3 and install spawn-fcgi :

[root@centos63 ~]# yum install spawn-fcgi -y

5. Start configure nginx. It is better to backup original nginx config file :

[root@centos63 ~]# cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak

6. Create nginx DocumentRoot for ehowstuff.local virtual host :

[root@centos63 ~]# mkdir -p /var/www/html/ehowstuff.local
[root@centos63 ~]# mkdir -p /var/www/html/ehowstuff.local/public_html
[root@centos63 ~]# chown -R nginx:nginx /var/www/html/ehowstuff.local/public_html

7. Create folder where to store access.log and error.log :

[root@centos63 ~]# mkdir -p /var/log/nginx/ehowstuff.local
[root@centos63 ~]# chown -R nginx:nginx /var/log/nginx/ehowstuff.local

8. Modify default nginx config file :

[root@centos63 ~]# vi /etc/nginx/nginx.conf

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}


9. Add the host configuration file under /etc/nginx/conf.d/. Nginx config file will load all *.conf files under conf.d folder :

See also  How to Install openldap-clients on CentOS 6.2

As example, the website domain is ehowstuff.local. So virtual server ehowstuff.local will be created and named as ehowstuff.local.conf :

[root@centos63 ~]# vi /etc/nginx/conf.d/ehowstuff.local.conf
server {
    server_name www.ehowstuff.local ehowstuff.local;
    access_log /var/log/nginx/ehowstuff.local/access.log;
    error_log /var/log/nginx/ehowstuff.local/error.log;
    root /var/www/html/ehowstuff.local/public_html;

    location / {
        index index.html index.htm index.php;
    }

    location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /var/www/html/ehowstuff.local/public_html$fastcgi_script_name;
    }
}

10. Check your nginx version :

[root@centos63 ~]# nginx -v
nginx version: nginx/1.2.4

11. Configure php-fastcgi :

[root@centos63 ~]# cd /opt
[root@centos63 opt]# wget -O php-fastcgi-rpm.sh http://library.linode.com/assets/696-php-fastcgi-rpm.sh
[root@centos63 opt]# mv php-fastcgi-rpm.sh /usr/bin/php-fastcgi
[root@centos63 opt]# chmod +x /usr/bin/php-fastcgi
[root@centos63 opt]# wget -O php-fastcgi-init-rpm.sh http://library.linode.com/assets/697-php-fastcgi-init-rpm.sh
[root@centos63 opt]# mv php-fastcgi-init-rpm.sh /etc/rc.d/init.d/php-fastcgi
[root@centos63 opt]# chmod +x /etc/rc.d/init.d/php-fastcgi

12. Start php-fastcgi to control spawn-fcgi :

[root@centos63 ~]# /etc/init.d/php-fastcgi start
Starting php-cgi: spawn-fcgi: child spawned successfully: PID: 3380
                                                           [  OK  ]

13. Start Nginx service :

[root@centos63 ~]# service nginx start

14. Make Nginx and php-fastcgi start at boot :

[root@centos63 opt]# chkconfig --add nginx
[root@centos63 opt]# chkconfig nginx on
[root@centos63 opt]# chkconfig --add php-fastcgi
[root@centos63 opt]# chkconfig php-fastcgi on

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *