Red Hat / CentOS Install nginx PHP5 FastCGI Webserver

How do I install and configure a ngnix FastCGI php5 HTTP / web server under Red Hat / RHEL / Fedora / CentOS Linux?

Nginx (engine x) is an HTTP(S) server, reverse proxy and IMAP/POP3 proxy server written by Igor Sysoev. It is known for its high performance, stability, rich feature set, simple configuration, and low resource consumption.

Advertisement

Step # 1: Enable EPEL repo

ngnix is not included in the base system. Turn on EPEL repo to install nginx stable release:
# rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/$(uname -m)/epel-release-5-3.noarch.rpm

Step # 2: Install ngnix

Type the following command at a shell prompt:
# yum install nginx
Sample output:

Loaded plugins: downloadonly, fastestmirror, priorities, protectbase
Loading mirror speeds from cached hostfile
 * epel: archive.linux.duke.edu
 * base: ftp.linux.ncsu.edu
 * updates: centos.mirror.nac.net
 * addons: mirror.cs.vt.edu
 * extras: centos.mirror.nac.net
0 packages excluded due to repository protections
Setting up Install Process
Parsing package install arguments
Resolving Dependencies
--> Running transaction check
---> Package nginx.x86_64 0:0.6.34-1.el5 set to be updated
--> Finished Dependency Resolution

Dependencies Resolved

==============================================================================================================================================================
 Package                             Arch                                 Version                                    Repository                          Size
==============================================================================================================================================================
Installing:
 nginx                               x86_64                               0.6.34-1.el5                               epel                               319 k

Transaction Summary
==============================================================================================================================================================
Install      1 Package(s)         
Update       0 Package(s)         
Remove       0 Package(s)         

Total size: 319 k
Is this ok [y/N]: y
Downloading Packages:
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing     : nginx                                             [1/1] 

Installed: nginx.x86_64 0:0.6.34-1.el5
Complete!

nginx configuration file

  • Default config file: /etc/nginx/nginx.conf
  • Default SSL config file: /etc/nginx/conf.d/ssl.conf
  • Default virtual hosting config file: /etc/nginx/conf.d/virtual.conf
  • Default documentroot: /usr/share/nginx/html

Configure PHP As FastCGI

Type the following to install php5 with other modules:
# yum install php-pear-Net-Socket php-pear php-common php-gd php-devel php php-mbstring php-pear-Mail php-cli php-imap php-snmp php-pdo php-xml php-pear-Auth-SASL php-ldap php-pear-Net-SMTP php-mysql

Install spawn-fcgi simple program for spawning FastCGI processes

Type the following command:
# yum install spawn-fcgi
Next, download spawn-fcgi init.d shell script:
# wget http://bash.cyberciti.biz/dl/419.sh.zip
# unzip 419.sh.zip
# mv 419.sh /etc/init.d/php_cgi
# chmod +x /etc/init.d/php_cgi

Start php app server, enter:
# /etc/init.d/php_cgi start
# netstat -tulpn | grep :9000

Sample output:

tcp        0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN      14294/php-cgi

By default php server listens on 127.0.0.1:9000 port. Finally, update /etc/nginx/nginx.conf as follows:
# vi /etc/nginx/nginx.conf
Modify / append as follows:

    location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name;
            include        fastcgi_params;
        }

Save and close the file. Restart nginx:
# service nginx restart
Create /usr/share/nginx/html/test.php as follows:

<?php
     phpinfo();
?>

Further readings:

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

74 comments… add one
  • Ferdian Jul 26, 2012 @ 6:24

    Thank you. It works like a charm! :)

  • Cosmo Dec 31, 2012 @ 19:53

    An error was occurred when I tried to:

    yum install nginx
    Error: Package: nginx-0.8.55-2.el5.i386 (epel)
    Requires: perl(:MODULE_COMPAT_5.8.8)
    You could try using –skip-broken to work around the problem
    You could try running: rpm -Va –nofiles –nodigest

    I tried to:

    Packages skipped because of dependency problems:
    GeoIP-1.4.8-1.el5.i386 from epel
    gd-2.0.35-10.el6.i686 from base
    libxslt-1.1.26-2.el6_3.1.i686 from updates
    nginx-0.8.55-2.el5.i386 from epel

    rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-5.noarch.rpm
    Retrieving http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-5.noarch.rpm
    curl: (22) The requested URL returned error: 404
    error: skipping http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-5.noarch.rpm – transfer failed

    Perhaps you could help me to solve this matter.

  • Paul Jan 4, 2013 @ 13:22

    Thanks for sharing :)

    I’m having trouble with the php_cgi script.

    Getting this error:
    Starting php-cgi: /etc/init.d/php_cgi: line 38: daemon: command not found

    I can’t find the init.d/functions in my CentOs 6.3 minimum install

    Does anyone know how I get this daemon command?

  • Kiran Feb 19, 2013 @ 22:23

    Updated first link:

    rpm -Uvh http://dl.fedoraproject.org/pub/epel/5/$(uname -m)/epel-release-5-4.noarch.rpm

  • shahzaibcb Dec 10, 2013 @ 7:42

    Is there a way to log errors in php-cgi script or in phpfastcgi ? No errors, no tweaking of the max_requests,max_childrens etc.

Leave a Reply

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

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.