Red Hat / CentOS Install nginx PHP5 FastCGI Webserver

by on April 22, 2009 · 74 comments· LAST UPDATED June 2, 2009

in , ,

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:

Tweet itFacebook itG+ itDownload PDF versionFound an error/typo on this page?

{ 74 comments… read them below or add one }

1 Cursors April 22, 2009 at 6:29 pm

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


2 nixCraft April 22, 2009 at 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.


3 GaQuay April 23, 2009 at 6:29 am

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


4 nixCraft April 23, 2009 at 6:34 am

You must use correct documentroot for php.


5 GaQuay April 23, 2009 at 6:51 am
6 nixCraft April 23, 2009 at 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;


7 codex73 May 12, 2011 at 1:25 am

Worked perfectly! Thanks


8 tomboy July 2, 2014 at 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;



9 GaQuay April 23, 2009 at 7:13 am

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


10 nixCraft April 23, 2009 at 9:19 am

Check your log files for error or messages.


11 GaQuay April 23, 2009 at 10:21 am

no error in error log @@


12 _b1 June 19, 2010 at 9:09 am

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


13 RobertRade April 23, 2009 at 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.



14 nixCraft April 23, 2009 at 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


15 Balaviswanathan.V April 26, 2009 at 12:07 pm

Thanks a lot for the reply…….


16 BuLe June 2, 2009 at 11:00 am

Your shell script cannot be downloaded. 403 error….


17 nixCraft June 2, 2009 at 11:18 am


Thanks for the heads-up!

You can now download script:

$ wget 


18 adityo July 14, 2009 at 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.




19 nixCraft July 14, 2009 at 10:38 am

Open /etc/sysconfig/phpfastcgi and add



20 adityo July 14, 2009 at 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.



21 nixCraft July 14, 2009 at 12:34 pm

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


22 adityo July 14, 2009 at 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


23 Oscar August 5, 2009 at 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.


24 genarg March 14, 2011 at 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;



25 abbas August 24, 2011 at 5:51 pm

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


26 billyduc September 7, 2009 at 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 !


27 eyecool September 15, 2009 at 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.


28 justaname September 17, 2009 at 12:19 pm

Thank you :)


29 friend November 15, 2009 at 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?


30 Vamsi November 18, 2009 at 7:46 pm

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


31 Dimitris December 3, 2009 at 7:03 pm

yum install spawn-fcgi

No package spawn-fcgi available.
Nothing to do

Centos 64



32 Vamsi December 31, 2009 at 3:06 pm

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


33 Jason May 6, 2010 at 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;



34 Snake January 6, 2010 at 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 :)


35 -Liju January 8, 2010 at 12:14 pm

Hi Vivek,

Good working and help it me today :-).

Really it’s quick setup..

keep it up


36 Nnyan January 15, 2010 at 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)?


37 sam February 22, 2010 at 6:05 pm

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


38 JaMuNix March 21, 2010 at 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


39 aral April 21, 2010 at 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


40 Robert July 3, 2010 at 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.


41 tom3k July 13, 2010 at 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



42 Mohamed M. Hagag August 5, 2010 at 5:14 pm

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


43 Usha Iyer August 8, 2010 at 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

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



44 pablo September 22, 2010 at 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?


45 saurav September 22, 2010 at 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.



46 pablo September 22, 2010 at 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.-


47 Reza Salimian November 11, 2010 at 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.


48 Gokhan March 21, 2011 at 2:28 pm

I dont donwload the sh file.

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


49 Mike May 6, 2011 at 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.



50 nixCraft May 6, 2011 at 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.


51 Mike May 7, 2011 at 12:32 am

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


52 DJ May 15, 2011 at 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… 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.


53 Coolmax May 19, 2011 at 2:18 pm
$ wget
Connecting to||:80... connected.
HTTP request sent, awaiting response... No data received.

Download link works in browser


54 Coolmax May 19, 2011 at 2:39 pm

Why you block wget user-agent string?


55 nixCraft May 19, 2011 at 6:55 pm

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


56 killshot June 13, 2011 at 7:49 pm

Having this problem too.


57 killshot June 13, 2011 at 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.


58 nixCraft June 13, 2011 at 11:22 pm

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]


59 t0kneneng June 7, 2011 at 6:48 am

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


60 t0kneneng June 7, 2011 at 7:54 am

i mean 504 gateway timeout


61 Danny Horne June 20, 2011 at 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?


62 MCorona August 5, 2011 at 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?


63 soufiane October 3, 2011 at 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 ?


64 Coolmax October 3, 2011 at 2:57 pm

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

Kind regards, Coolmax


65 soufiane October 4, 2011 at 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;.


66 sanjay March 20, 2012 at 11:39 am

Resolving… failed: Name or service not known.


67 Hadi May 22, 2012 at 9:28 pm

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



68 Sam Anderson May 28, 2012 at 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…


69 manoj janardhan July 5, 2012 at 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$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


70 Ferdian July 26, 2012 at 6:24 am

Thank you. It works like a charm! :)


71 Cosmo December 31, 2012 at 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
curl: (22) The requested URL returned error: 404
error: skipping – transfer failed

Perhaps you could help me to solve this matter.


72 Paul January 4, 2013 at 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?


73 Kiran February 19, 2013 at 10:23 pm

Updated first link:

rpm -Uvh$(uname -m)/epel-release-5-4.noarch.rpm


74 shahzaibcb December 10, 2013 at 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.


Leave a Comment

Tagged as: , , , , , , , , , ,

Previous Faq:

Next Faq: