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$(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:
 * base:
 * updates:
 * addons:
 * extras:
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
# unzip
# mv /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:

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 74 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
74 comments… add one
  • Cursors Apr 22, 2009 @ 18:29

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

  • 🐧 nixCraft Apr 22, 2009 @ 19:49

    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 Apr 23, 2009 @ 6:29

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

    • 🐧 nixCraft Apr 23, 2009 @ 6:34

      You must use correct documentroot for php.

  • GaQuay Apr 23, 2009 @ 6:51

    i config correct
    here is my nginx conf

  • 🐧 nixCraft Apr 23, 2009 @ 7:08

    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

      Worked perfectly! Thanks

    • tomboy Jul 2, 2014 @ 20:42

      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 Apr 23, 2009 @ 7:13

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

    • 🐧 nixCraft Apr 23, 2009 @ 9:19

      Check your log files for error or messages.

  • GaQuay Apr 23, 2009 @ 10:21

    no error in error log @@

    • _b1 Jun 19, 2010 @ 9:09

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

  • RobertRade Apr 23, 2009 @ 11:55


    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 Apr 23, 2009 @ 12:48

      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 Apr 26, 2009 @ 12:07

    Thanks a lot for the reply…….

  • BuLe Jun 2, 2009 @ 11:00

    Your shell script cannot be downloaded. 403 error….

  • 🐧 nixCraft Jun 2, 2009 @ 11:18


    Thanks for the heads-up!

    You can now download script:

    $ wget 
  • adityo Jul 14, 2009 @ 10:21

    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 Jul 14, 2009 @ 10:38

    Open /etc/sysconfig/phpfastcgi and add

    export PHP_FCGI_MAX_REQUESTS=1000
  • adityo Jul 14, 2009 @ 12:12

    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 Jul 14, 2009 @ 12:34

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

  • adityo Jul 14, 2009 @ 12:25

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

  • Oscar Aug 5, 2009 @ 2:32

    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 Mar 14, 2011 @ 16:11

      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 Aug 24, 2011 @ 17:51

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

  • billyduc Sep 7, 2009 @ 6:26

    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 Sep 15, 2009 @ 10:27

    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 Sep 17, 2009 @ 12:19

    Thank you :)

  • friend Nov 15, 2009 @ 15:56

    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 Nov 18, 2009 @ 19:46

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

  • Dimitris Dec 3, 2009 @ 19:03

    yum install spawn-fcgi

    No package spawn-fcgi available.
    Nothing to do

    Centos 64


  • Vamsi Dec 31, 2009 @ 15:06

    Hi Vivek !
    when I sucessfully installed nginx with php..but when I open a directory without a forward redirects to

    • Jason May 6, 2010 @ 12:05

      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 Jan 6, 2010 @ 5:25

    Thank you for your great article i really appreciate it.

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

  • -Liju Jan 8, 2010 @ 12:14

    Hi Vivek,

    Good working and help it me today :-).

    Really it’s quick setup..

    keep it up

  • Nnyan Jan 15, 2010 @ 1:06

    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 Feb 22, 2010 @ 18:05

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

  • JaMuNix Mar 21, 2010 @ 4:01

    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 Apr 21, 2010 @ 18:33

    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 Jul 3, 2010 @ 0:11

    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 Jul 13, 2010 @ 4:23

    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 Aug 5, 2010 @ 17:14

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

  • Usha Iyer Aug 8, 2010 @ 15:37

    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

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


  • pablo Sep 22, 2010 @ 0:35


    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 Sep 22, 2010 @ 12:02

      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 Sep 22, 2010 @ 18:59


    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 Nov 11, 2010 @ 11:03

    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 Mar 21, 2011 @ 14:28

    I dont donwload the sh file.

    Connecting to||:80… connected.
    HTTP request sent, awaiting response… No data received.

  • Mike May 6, 2011 @ 17:26

    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 @ 18:35

      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 @ 0:32

        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

    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… error: skipping… – transfer failed – Unknown or unexpected error warning: u 0x1f876d0 ctrl 0x1f88a40 nrefs != 0 ( http)

    Cause: The filename is slightly off (probably was updated). It now needs “rpm -Uvh” (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 >”

    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 @ 14:18
    $ wget
    Connecting to||:80... connected.
    HTTP request sent, awaiting response... No data received.

    Download link works in browser

    • Coolmax May 19, 2011 @ 14:39

      Why you block wget user-agent string?

      • 🐧 nixCraft May 19, 2011 @ 18:55

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

        • killshot Jun 13, 2011 @ 19:49

          Having this problem too.

          • killshot Jun 13, 2011 @ 19:53

            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 Jun 13, 2011 @ 23:22

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

              --2011-06-14 04:52:00--
              Connecting to||:80... connected.
              HTTP request sent, awaiting response... 200 OK
              Length: 890 [application/zip]
              Saving to: `'
              100%[======================================>] 890         --.-K/s   in 0s      
              2011-06-14 04:52:01 (91.4 MB/s) - `' saved [890/890]
  • t0kneneng Jun 7, 2011 @ 6:48

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

  • t0kneneng Jun 7, 2011 @ 7:54

    i mean 504 gateway timeout

  • Danny Horne Jun 20, 2011 @ 14:57

    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 Aug 5, 2011 @ 4:13

    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 Oct 3, 2011 @ 11:31

    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 Oct 3, 2011 @ 14:57

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

      Kind regards, Coolmax

  • soufiane Oct 4, 2011 @ 8:21


    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 Mar 20, 2012 @ 11:39

    Resolving… failed: Name or service not known.

  • Hadi May 22, 2012 @ 21:28

    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

    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 Jul 5, 2012 @ 8:08

    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$1 ($1=searched word) . We need to display URL for our customers as$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 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
    curl: (22) The requested URL returned error: 404
    error: skipping – 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$(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.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum