≡ Menu

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.

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
 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

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    *                   LISTEN      14294/php-cgi

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

    location ~ \.php$ {
            root           html;
            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:


Further readings:

Share this tutorial on:

Your support makes a big difference:
I have a small favor to ask. More people are reading the nixCraft. Many of you block advertising which is your right, and advertising revenues are not sufficient to cover my operating costs. So you can see why I need to ask for your help. The nixCraft, takes a lot of my time and hard work to produce. If you use nixCraft, who likes it, helps me with donations:
Become a Supporter →    Make a contribution via Paypal/Bitcoin →   

Don't Miss Any Linux and Unix Tips

Get nixCraft in your inbox. It's free:

{ 74 comments… add one }
  • Cursors April 22, 2009, 6:29 pm

    Can you add a step on how to add ftp access with nginx?

  • nixCraft April 22, 2009, 7:49 pm

    Do you have ftp server installed? If so you can add user with adduser command itself:

    useradd  -d /usr/share/nginx/html/ -G nginx -k /dev/null username 
    passwd username
    chown -R username:nginx /usr/share/nginx/html/

    Above will get you started. However, for virtual domain you need to do a little more work.

  • GaQuay April 23, 2009, 6:29 am

    it show error, when i try access webpage php
    No input file specified.

    • nixCraft April 23, 2009, 6:34 am

      You must use correct documentroot for php.

  • GaQuay April 23, 2009, 6:51 am

    i config correct
    here is my nginx conf

  • nixCraft April 23, 2009, 7:08 am

    Following is not correct:

    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

    $document_root should be a full path. Try it as follows and restart server:

    fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name;
    • codex73 May 12, 2011, 1:25 am

      Worked perfectly! Thanks

    • tomboy July 2, 2014, 8:42 pm

      This case is good:
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

      But.. full path for $document_root is above and can’t by within directive location, otherwise will by issue.

      For example:
      root /usr/share/nginx/www/www; # my root directory

      location ~ \.php$ { #my directive location for php
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;


  • GaQuay April 23, 2009, 7:13 am

    thank you for your reply, i set it , but it not work for me. still error :(

    • nixCraft April 23, 2009, 9:19 am

      Check your log files for error or messages.

  • GaQuay April 23, 2009, 10:21 am

    no error in error log @@

    • _b1 June 19, 2010, 9:09 am

      it`s work.
      you need change permissions and owners for files/dirs to

  • RobertRade April 23, 2009, 11:55 am


    Can you improve the documentation by including cgi-bin for running the scripts (I write my own custom executable scripts using C)?

    I’m using Lighttpd right now…because of poor “straight installation/configuration” Nginx documentation, especially the cgi-bin part.


    • nixCraft April 23, 2009, 12:48 pm

      Yes, I will try to cover the following in few days:

      1. Virtual hosting
      2. Cgi-bin via Perl / Python , C etc
      3. Ruby configuration
      4. Proxy configuration etc
      5. Security configuration – chroot jail etc
  • Balaviswanathan.V April 26, 2009, 12:07 pm

    Thanks a lot for the reply…….

  • BuLe June 2, 2009, 11:00 am

    Your shell script cannot be downloaded. 403 error….

  • nixCraft June 2, 2009, 11:18 am


    Thanks for the heads-up!

    You can now download script:

    $ wget http://bash.cyberciti.biz/dl/419.sh.zip 
  • adityo July 14, 2009, 10:21 am

    Hi there,

    May i know how to add PHP_FCGI_MAX_REQUESTS=1000 to spawn fcgi , because i need to add number of request before php-process will be restarted and i don’t know how to add it on spawn fcgi. Thank you.



  • nixCraft July 14, 2009, 10:38 am

    Open /etc/sysconfig/phpfastcgi and add

    export PHP_FCGI_MAX_REQUESTS=1000
  • adityo July 14, 2009, 12:12 pm

    Hi Vivek,

    Thank you for the quick response, unfortunatelly i cannot find /etc/sysconfig/phpfastcgi on my /etc/sysconfig/ directory should i create a new files ? do you have /etc/sysconfig/phpfastcgi example so i can copy it? thanks.


    • nixCraft July 14, 2009, 12:34 pm

      Yes, you’ve to create the file. It will get called automatically by /etc/init.d/php_cgi

  • adityo July 14, 2009, 12:25 pm

    i am using CentOS release 5.3 (Final) , spawn-fcgi-1.4.22-2.el5 and nginx-0.6.36-1.el5

  • Oscar August 5, 2009, 2:32 am

    Hi, Vivek,

    I followed your tutorial and everything work like charm. But when I try to add my virtual hosts, php works fine in the default server but not in the other domains. I read in different forums but I can’t make it work. Do you have any idea of what I’m doing wrong?


    server {
    listen 80;
    server_name localhost;

    location / {
    root /usr/share/nginx/html;
    index index.php index.html index.htm;

    error_page 404 /404.html;
    location = /404.html {
    root /usr/share/nginx/html;

    # redirect server error pages to the static page /50x.html
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
    root /usr/share/nginx/html;

    # pass the PHP scripts to FastCGI server listening on
    location ~ \.php$ {
    root /usr/share/nginx/html;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
    include fastcgi_params;


    server {

    listen 80;
    server_name test;

    location / {
    root /usr/share/nginx/test;
    index index.html;
    location ~ \.php$ {
    root /usr/share/nginx/test;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /usr/share/nginx/test$fastcgi_script_name;
    include fastcgi_params;

    When I try to open a php file, the browser try to download it.

    I would appreciate any advise you could give me. Thanks.

    • genarg March 14, 2011, 4:11 pm

      This is maybe already solved for you, but the solution is this:

      In the /etc/nginx/conf.d/virtual.conf file use this line:

      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;


    • abbas August 24, 2011, 5:51 pm

      You must disable apache:
      service httpd stop
      the start nginx (restart):
      service nginx restart

  • billyduc September 7, 2009, 6:26 am

    Hi Vivek!
    I Do exactly the same this How to, but when I try to run test.php in firefox it failed!
    Here’s the result in my browser
    No input file specified.
    I tried to run it in command line mode and I get the result!
    How to solve this error !

  • eyecool September 15, 2009, 10:27 am

    Great, works, got it up but when php_cgi crashes, stalls or dies, it doesn’t automatically respawn. I googled extensively and found other people having the same problem. One guy set up a cron job to run every minute to make sure it’s on. Most everyone else seems to have moved to php-fpm, which requires recompiling php, which breaks my virtualmin/webmin. Wish php_cgi would stay alive.

  • justaname September 17, 2009, 12:19 pm

    Thank you :)

  • friend November 15, 2009, 3:56 pm

    I get this error on phpinfo.php with nginx
    No input file specified.

    but same file runs ok with apache or cherokee

    Any ideas?

  • Vamsi November 18, 2009, 7:46 pm

    Hi !
    can you please tell how to avoid a file type in nginx ?
    In Apache I used .htaccess..
    please help

  • Dimitris December 3, 2009, 7:03 pm

    yum install spawn-fcgi

    No package spawn-fcgi available.
    Nothing to do

    Centos 64


  • Vamsi December 31, 2009, 3:06 pm

    Hi Vivek !
    when I sucessfully installed nginx with php..but when I open a directory without a forward slash..it redirects to http://www._.com/

    • Jason May 6, 2010, 12:05 pm

      I followed this guide and got the same problem. The solution was on another forum, which worked wonders. Inside the nginx server config, add the line server_name_in_redirect off;


      listen 80;
      server_name localhost;
      server_name_in_redirect off;


  • Snake January 6, 2010, 5:25 am

    Thank you for your great article i really appreciate it.

    Thanks to your article i have made my static files server with Nginx :)

  • -Liju January 8, 2010, 12:14 pm

    Hi Vivek,

    Good working and help it me today :-).

    Really it’s quick setup..

    keep it up

  • Nnyan January 15, 2010, 1:06 am

    Wold it be possible to update this (or a new version) that is focused on installing a low memory LEMP on small bare bones VPS accounts for virtual hosting? Something like:
    Nginx, PHP5, FastCGI, XCache, mySQL, openssh, Bind, wordpress (or lite alternatives)?

  • sam February 22, 2010, 6:05 pm

    Which control panel will work with nginx server and, where is the link to download and install instructions?

  • JaMuNix March 21, 2010, 4:01 am

    Tanks !!!! Work Perfect for my server on Centos 5.4
    gracias amigo me sirvio de mucho en mi servidor viejito servidor con Centos 5.4

  • aral April 21, 2010, 6:33 pm

    i was installed kloxo with default root directory /home/admin how to sets that with nginx config because after i installed nginx its show forbiden and now i swicth it back to apache

  • Robert July 3, 2010, 12:11 am

    For all who have the problem “The page you are looking for is temporarily unavailable. Please try again later.” after the installation.
    I did a restart of the server started php_cgi and nginx again and everything worked fine.

  • tom3k July 13, 2010, 4:23 am

    yep, this guide works.

    the one thing i would add… if you want the php_cgi script starting on boot, u need to add it to the ntsysv list, to do so:

    chkconfig –add php_cgi


  • Mohamed M. Hagag August 5, 2010, 5:14 pm

    you may also check this pre-built and src.rpms

  • Usha Iyer August 8, 2010, 3:37 pm

    Hi Vivek ,

    I am Usha Iyer, an Acquisition Editor with Packt Publishing. As an Acquisition Editor, it is my role to evaluate, develop and ultimately bring book ideas to publication. I am also responsible for finding the right author for any book; bringing them onboard, then working with them as the book is written.

    Recently, I have begun to develop a title on ‘Nginx cookbook’, and am now looking for an author to write this book. The book will consist of some advanced recipes for people already familiar with Nginx.

    You can find some more information about writing for us at Packt’s website http://www.packtpub.com/authors.

    Please contact me if you are interested in writing this book and I would love to discuss the opportunity with you further.

    Email: ushai@packtpub.com

  • pablo September 22, 2010, 12:35 am


    I installed nginx in centos 5.5 following this guide but i can not start the program:

    service nginx start
    Starting nginx: [FAILED]

    cat /var/log/nginx/error.log
    2010/09/21 19:25:02 [emerg] 3382#0: bind() to failed (98: Address already in use)
    2010/09/21 19:25:02 [emerg] 3382#0: bind() to failed (98: Address already in use)
    2010/09/21 19:25:02 [emerg] 3382#0: bind() to failed (98: Address already in use)
    2010/09/21 19:25:02 [emerg] 3382#0: bind() to failed (98: Address already in use)
    2010/09/21 19:25:02 [emerg] 3382#0: bind() to failed (98: Address already in use)
    2010/09/21 19:25:02 [emerg] 3382#0: still could not bind()

    Can you help me please?

    • saurav September 22, 2010, 12:02 pm

      Just Install putty on you system and open your web host by ip address and then type this command,
      nginx restart, it will start your nginx server and then start your httpd, by httpd restart.
      This command can start your webserver if you are running nginx as a proxy server.


  • pablo September 22, 2010, 6:59 pm


    Is solved, the problem was apache server running in the port 80.

    I installed nginx to run as httpd server for static content.

    thank you.-

  • Reza Salimian November 11, 2010, 11:03 am

    thank you VIVEK. great article. i am new in CentOS.
    after doing all thing in this article my server dosn’t work.
    and then i stop the firewall it works. where is the problem.

  • Gokhan March 21, 2011, 2:28 pm

    I dont donwload the sh file.

    (try:10) http://bash.cyberciti.biz/dl/419.sh.zip
    Connecting to bash.cyberciti.biz||:80… connected.
    HTTP request sent, awaiting response… No data received.

  • Mike May 6, 2011, 5:26 pm

    Great article strait to the point as usual and works.

    For me I have a strange issue I can’t seem to figure and googled and researched a lot to no end. I’ve got php-fastcgi running fine on nginx. However, I’m trying to load memcache and mongo php extensions and for some reason nginx skips right over their ini files in /etc/php.d/. I know memcache and mongo extensions are installed correctly because apache loads them. I’m thinking maybe I need to build php from source and specifically include them in the ./configure .. just a bit gun shy to do this because I’m not sure how this will impact the installation based on this article.


    • nixCraft May 6, 2011, 6:35 pm

      You need to restart or reload both services after installing any php modules and config file in /etc/php.d/memcache.ini. Finally run phpinfo() to see loaded modules.

      • Mike May 7, 2011, 12:32 am

        Yes, that was the first thing I did. Is there something I’m missing about how FastCGI handles modules?

  • DJ May 15, 2011, 6:21 am

    EXCELLENT tutorial! Thanks!

    Some corrections, if you still maintain this post (I hope you do, since this was a top Google result when I was looking for such instructions)…

    I got 3 issues:

    Retrieving download.fedora.redhat.com/pub/epel/5/x86_64/… error: skipping download.fedora.redhat.com/pub/epel/5/x86_64/… – transfer failed – Unknown or unexpected error warning: u 0x1f876d0 ctrl 0x1f88a40 nrefs != 0 (download.fedora.redhat.com http)

    Cause: The filename is slightly off (probably was updated). It now needs “rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-4.noarch.rpm” (new one ends in “-4” instead of “-3”)

    I got an error when trying to fetch that init.d shell script with wget.
    HTTP request sent, awaiting response… No data received.
    Retrying. (this would loop endlessly)

    Cause: wget failed, I found this command online which worked
    “curl bash.cyberciti.biz/dl/419.sh.zip > 419.sh.zip”

    Last issue was when modifying the nginx.conf file. I got “Unknown directive “location”” error.

    Cause: Due to my own ignorance, I did not realize that this needed to be appended INSIDE the http { … } section of the config, so when you mentioned to append it, i just put it at the end of the file. Fixed by just moving the “location” text to the inside of the http section.

    Overall, GREAT tutorial. Much thanks! …any chance you could do nginx with memcached + virtual host configuration walkthrough too? ;)

    One last note: I am using CentOS 5, so YMMV with these issues I outlined.

  • Coolmax May 19, 2011, 2:18 pm
    $ wget http://bash.cyberciti.biz/dl/419.sh.zip
    --16:17:32--  http://bash.cyberciti.biz/dl/419.sh.zip
    Resolving bash.cyberciti.biz...
    Connecting to bash.cyberciti.biz||:80... connected.
    HTTP request sent, awaiting response... No data received.

    Download link works in browser

    • Coolmax May 19, 2011, 2:39 pm

      Why you block wget user-agent string?

      • nixCraft May 19, 2011, 6:55 pm

        I will get fixed it when I’ve some free time.

        • killshot June 13, 2011, 7:49 pm

          Having this problem too.

          • killshot June 13, 2011, 7:53 pm

            In case anyone else runs into this, you can workaround this by:

            1) Download it from a browser and copy it over, or:
            2) wget -U=whatever

            It seems to be blocking Wget as a user-agent.

            • nixCraft June 13, 2011, 11:22 pm

              It should only block wget UA when you run the wget continuously. I’ve updated few settings and should work:

               wget http://bash.cyberciti.biz/dl/419.sh.zip
              --2011-06-14 04:52:00--  http://bash.cyberciti.biz/dl/419.sh.zip
              Resolving bash.cyberciti.biz...
              Connecting to bash.cyberciti.biz||:80... connected.
              HTTP request sent, awaiting response... 200 OK
              Length: 890 [application/zip]
              Saving to: `419.sh.zip'
              100%[======================================>] 890         --.-K/s   in 0s      
              2011-06-14 04:52:01 (91.4 MB/s) - `419.sh.zip' saved [890/890]
  • t0kneneng June 7, 2011, 6:48 am

    Hi I’m having 540 gateway time out how to increase request time out?

  • t0kneneng June 7, 2011, 7:54 am

    i mean 504 gateway timeout

  • Danny Horne June 20, 2011, 2:57 pm

    Do you know if the Nginx RPM listed at the top of this page has built in IPv6 support or would I need to download the source to enable that?

  • MCorona August 5, 2011, 4:13 am

    Since this is not compiled I was wondering how to get the H264 module working with this. I prefer using rpm files. How can I add this module to nginx installed via RPM?


  • soufiane October 3, 2011, 11:31 am

    Thanks a lot for yr article;
    Please, i do exactly your conf,but i still can not have my php pages affiched, in my log error i have thos errors:

    2011/10/03 11:09:51 [error] 7706#0: *16 connect() failed (111: Connection refused) while connecting to upstream, client:, server: _, request: “GET /index.php HTTP/1.1”, upstream: “fastcgi://”, host: “”

    No problem with the Documentroot.
    Have you any idea ?

    • Coolmax October 3, 2011, 2:57 pm

      Is php-cgi running? What you set as server_name in server {} directive?

      Kind regards, Coolmax

  • soufiane October 4, 2011, 8:21 am


    for php-cgi, i had The OK message after starting it, but its show me the error :
    php-cgi is die, but the pid file exist. i try to remove it and touch it again, but its show e the same error message.

    and for server_name is set to localhost;.

  • sanjay March 20, 2012, 11:39 am

    Resolving download.fedora.redhat.com… failed: Name or service not known.

  • Hadi May 22, 2012, 9:28 pm

    I have same problem and browser download php file.
    Would you please inform us how we can remove this problem ?


  • Sam Anderson May 28, 2012, 12:44 pm

    Solution for “No input file specified” error while accessing the php files thru browser,

    In nginx.conf file replace the following line,

    line no:69 fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
    fastcgi_param SCRIPT_FILENAME /opt/nginx/html/$fastcgi_script_name;

    where “/opt/nginx/html/” is my webroot path…

  • manoj janardhan July 5, 2012, 8:08 am

    We have a server where nginx is installed, we have also configured PHP As FastCGI on the server. Everything is working fine except rewrite rule. Our requirement is to mask an URL

    for eg:- if someone search in our website the URL which comes will be like http://example.com/search.php?searchword=$1 ($1=searched word) . We need to display URL for our customers as http://example.com/$1.html.

    We have set rewrite rule as rewrite ^/(([a-zA-Z_0-9]|-)+/?)$ /search.php?searchword=$1 break;

    The URL is getting redirected however we get a file not found error each time. How can we mask the URL just as we do in Apache. Any help would be greatly appreciated

  • Ferdian July 26, 2012, 6:24 am

    Thank you. It works like a charm! :)

  • Cosmo December 31, 2012, 7:53 pm

    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 January 4, 2013, 1:22 pm

    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 February 19, 2013, 10:23 pm

    Updated first link:

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

  • shahzaibcb December 10, 2013, 7:42 am

    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.

Security: Are you a robot or human?

Leave a Comment

You can use these HTML tags and attributes: <strong> <em> <pre> <code> <a href="" title="">

   Tagged with: , , , , , , , , , ,