≡ Menu

Red Hat / CentOS Apache 2 FastCGI PHP Configuration

FastCGI is a protocol for interfacing interactive programs with a web server. FastCGI's main aim is to reduce the overhead associated with interfacing the web server and CGI programs, allowing a server to handle more web page requests at once.

Also, PHP is not recommended with multithreaded Apache2 (worker MPM) because of performance and some 3rd party PHP extensions are not not guaranteed thread-safe.

nginx and lighttpd has inbuilt support for FastCGI. For Apache web server you need to use either mod_fastcgi or mod_fcgid.

Why use mod_fastcgi instead of mod_perl / mod_php?

From the wikipedia article:

Instead of creating a new process for every request, FastCGI can use a single persistent process which handles many requests over its lifetime. Processing of multiple requests simultaneously is achieved either by using a single connection with internal multiplexing (ie. multiple requests over a single connection) and/or by using multiple connections. Many such processes can exist, something that can increase stability and scalability. FastCGI also allows programs to get the web server to do certain simple operations, like reading in a file, before the request is handed over. Environment information and page requests are sent from the web server to the process over a TCP connection (for remote processes) or Unix domain sockets (for local processes). Responses are returned from the process to the web server over the same connection. The connection may be closed at the end of a response, but the web server and the process are left standing.

Many web site administrators and programmers are finding that the separation of web applications from the web server in FastCGI (and the simpler SCGI) has many desirable advantages over embedded interpreters (mod_perl, mod_php, etc.). This separation allows server and application processes to be restarted independently -- an important consideration for busy web sites. It also facilitates per-application security policies -- important for ISPs and web hosting companies.

In this quick tutorial, you will learn about Apache 2 + mod_fastcgi + PHP installation and configuration under Red Hat Enterprise Linux / CentOS Linux version 5.x+.

Install mod_fastcgi

Make sure required packages are installed (httpd-devel and apr-devel required to compile mod_fastcgi), enter:
# yum install libtool httpd-devel apr-devel apr
Next, grab the latest mod_fastcgi source code, enter:
# cd /opt
# wget http://www.fastcgi.com/dist/mod_fastcgi-current.tar.gz

Untar tar ball, type:
# tar -zxvf mod_fastcgi-current.tar.gz
# cd mod_fastcgi-2.4.6/

Make a copy of Makefile.AP2, enter:
# cp Makefile.AP2 Makefile
Compile and install mod_fastcgi for 32 bit system, enter:
# make top_dir=/usr/lib/httpd
# make install top_dir=/usr/lib/httpd

Compile and install mod_fastcgi for 64 bit system, enter:
# make top_dir=/usr/lib64/httpd
# make install top_dir=/usr/lib64/httpd

Sample output:

make install top_dir=/usr/lib64/httpd
make[1]: Entering directory `/tmp/mod_fastcgi-2.4.6'
/usr/lib64/apr-1/build/libtool --silent --mode=install cp mod_fastcgi.la /usr/lib64/httpd/modules/
make[1]: Leaving directory `/tmp/mod_fastcgi-2.4.6'

Configure mod_fastcgi

Open /etc/httpd/conf.d/mod_fastcgi.conf file
# vi /etc/httpd/conf.d/mod_fastcgi.conf
Add an entry to it like this:
LoadModule fastcgi_module modules/mod_fastcgi.so
Save and close the file. Restart httpd, enter:
# service httpd restart
Sample output:

[Mon Dec 29 23:24:44 2008] [notice] caught SIGTERM, shutting down
[Mon Dec 29 23:24:44 2008] [notice] suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)
[Mon Dec 29 23:24:44 2008] [notice] Digest: generating secret for digest authentication ...
[Mon Dec 29 23:24:44 2008] [notice] Digest: done
[Mon Dec 29 23:24:44 2008] [notice] FastCGI: process manager initialized (pid 4785)
[Mon Dec 29 23:24:44 2008] [notice] Apache/2.2.3 (CentOS) configured -- resuming normal operations

How do I configure PHP as FastCGI process under RHEL / CentOS Linux?

First, you need to disable mod_php5, enter:
# mv /etc/httpd/conf.d/php.conf /etc/httpd/conf.d/php.conf.disable

Create a shell script in cgi-bin directory called php.fcgi

Create a script as follows in /var/www/cgi-bin/php.fcgi (or put in your virtual domain cgi-bin directory)

# Shell Script To Run PHP5 using mod_fastcgi under Apache 2.x
# Tested under Red Hat Enterprise Linux / CentOS 5.x
### Set PATH ###
### no editing below ###
exec $PHP_CGI

Set permission, type:
# chmod +x /var/www/cgi-bin/php.fcgi
Finally, modify documentroot directory permission as follows. You need to use AddHandler and Action directives for mod_fastcgi:

 <Directory "/var/www/html">
   Options -Indexes FollowSymLinks +ExecCGI
    AllowOverride AuthConfig FileInfo
    AddHandler php5-fastcgi .php
    Action php5-fastcgi /cgi-bin/php.fcgi
    DirectoryIndex index.php index.html
    Order allow,deny
    Allow from all


  • AddHandler php5-fastcgi .php : To configure Apache to handle php files (within the scope of the directive) with the specified extension(s) as FastCGI applications.
  • Action php5-fastcgi /cgi-bin/php.fcgi : This directive adds an action, which will activate cgi-script when action-type is triggered by the request. The cgi-script is the URL-path to a resource that has been configured as a CGI script using ScriptAlias. In our case, requests for files with a .php file extension of are handled instead by the specified cgi script /cgi-bin/php.fcgi.

Save and close the file. Restart httpd:
# service httpd restart

mod_fastcgi virtual hosting configuration

  • Domain name: nixcraft.com
  • DocumentRoot: /websites/home/nixcraft.com/http
  • cgi-bin directory: /websites/home/nixcraft.com/cgi-bin
  • php.fcgi path: /websites/home/nixcraft.com/cgi-bin/php.fcgi
  • Logs file directory: /websites/home/nixcraft.com/logs

Based upon above settings your virtualhosting configuration for nixcraft.com domain should look like as follows:

<VirtualHost *:80>
        ServerAdmin webmaster@nixcraft.com
        DocumentRoot "/websites/home/nixcraft.com/http"
        ServerName nixcraft.com
        ServerAlias www.nixcraft.com
        ErrorLog "/websites/home/nixcraft.com/logs/error.log"
        CustomLog "/websites/home/nixcraft.com/logs/access.log" common
        ScriptAlias /cgi-bin/ "/websites/home/nixcraft.com/cgi-bin/"
	<Directory "/websites/home/nixcraft.com/http">
		Options -Indexes FollowSymLinks +ExecCGI
		AllowOverride AuthConfig FileInfo
		AddHandler php5-fastcgi .php
		Action php5-fastcgi /cgi-bin/php.fcgi
		Order allow,deny
		Allow from all
	<Directory "/websites/home/nixcraft.com/cgi-bin">
		AllowOverride None
		Options None
		Order allow,deny
		Allow from all

Create a /websites/home/nixcraft.com/cgi-bin/php.fcgi as follows:

exec $PHP_CGI

Set permission and restart httpd:
# chmod +x /websites/home/nixcraft.com/cgi-bin/php.fcgi
# service httpd restart

Tweet itFacebook itGoogle+ itPDF itFound an error/typo on this page?

{ 36 comments… add one }

  • solshark January 9, 2009, 2:39 pm

    Thx. Simple and clean. Have just tested on my live CentOS server and all works fine.

  • omega13a January 20, 2009, 3:52 am

    I tried that but now all I get is a blank page on my website. I followed all the instructions… Maybe it has something to do with the fact I have Plesk also installed?

  • jason March 19, 2009, 6:36 pm

    i cant compile mod_fastcgi on centos 4!
    Makefile:13: /usr/local/apache2/build/special.mk: No such file or directory
    make: *** No rule to make target `/usr/local/apache2/build/special.mk’. Stop.
    Whats the problem :S

  • nixCraft March 19, 2009, 6:51 pm

    Set top_dir=/usr/lib/httpd and install httpd-devel package.

  • Tomcat May 12, 2009, 2:01 pm

    Anyone got this working with Plesk 8.x on CentOS? I am getting a PHP source dump on accessing PHP files. Came to know that Plesk 9.2 supports PHP as FastCGI on Linux. But I need to get it working on Plesk 8.x.

  • Caspar Schutte June 5, 2009, 2:54 pm

    Thank you!!! These instructions worked for me on CentOS 5.2. You saved me many hours of frustration.

  • celik August 25, 2009, 8:43 am

    hi all of this ok . it is run in console but not in browser

    get a message
    “The requested URL /var/www/cgi-bin/php.fcgi/index.php was not found on this server.”

    conf ->

    DirectoryIndex index.php index.htm index.html
    Options Indexes FollowSymLinks +ExecCGI
    AllowOverride None
    AddHandler php5-fastcgi .php
    Action php5-fastcgi /var/www/cgi-bin/php.fcgi
    Order allow,deny
    Allow from all

    can u help me ?

  • ecmidas September 12, 2009, 7:25 am

    First of all, thank you for the helpful article.
    and i have a question about Zend Optimizer, in these combination, can Zend Optimizer be used together?
    Any helpful comment would be great.
    Thanks in advance.

  • mvug October 16, 2009, 8:11 am


    thanx for the tutorial! But, I am getting the error:
    command not in docroot (/home/website/cgi-bin/php.fcgi)

    What can I do? thanx!

  • juggs October 30, 2009, 7:14 pm

    Nice tutorial, php /w fastcgi is up and running except for one thing, no php-cgi processes are spawned:

    root 27780 0.0 0.0 183296 3420 ? Ss 15:09 0:00 /usr/sbin/httpd
    apache 27781 0.0 0.0 183208 2292 ? S 15:09 0:00 \_ /usr/sbin/fcgi-
    apache 27782 0.0 0.0 183428 3084 ? S 15:09 0:00 \_ /usr/sbin/httpd
    apache 27783 0.0 0.0 183428 3076 ? S 15:09 0:00 \_ /usr/sbin/httpd

    I set PHP_FCGI_CHILDREN=4. Not sure where to start troubleshooting

  • Kok Wen Yen December 2, 2009, 3:47 am


    Are you sure the above example is executed via mod_fastcgi? The “Action php5-fastcgi /cgi-bin/php.fcgi” will fire the php-cgi to handle all incoming php script, i.e. only using CGI instead of FastCGI. Isn’t it?

    Nowhere in your example shows the use of FastCgiServer directive which is the one that starts the fast cgi server, right? I’m a bit confused here.

  • Thomas December 4, 2009, 10:33 pm

    Kok Wen Yen is right, the configuration is only using CGI and NOT FastCGI.

  • Kok Wen Yen March 8, 2010, 12:10 pm

    My bad. It is indeed Fast CGI. Only thing is th in this case, Apache is the one spawning the fastcgi processes. Also, does that mean there is no need to install the mod_fastcgi module? Simply using PHP’s php-cgi is sufficient?

  • Carlos August 6, 2010, 10:37 am

    Well, this is an old post but I will try, anyway:
    We are sufering problems with headers not properly delivered to PHP so authorization is not working.
    We had tried with the FastCgiConfig -pass-header HTTP_AUTHORIZATION

    option without success
    Any Idea about whos solving this problem?


  • expssh August 10, 2010, 11:49 am

    Super! Thank you!

  • Krish August 25, 2010, 4:47 pm

    Hi Guys,

    Does anybody tried this in linux. I have apache 2.2 with php5. Currently i am using prefork module and the performance is not good. Any help would be appreciated.


  • Chris August 27, 2010, 1:09 pm

    Thanks for Installing FascCGI guide.

  • Mark September 22, 2010, 11:38 pm

    this doesn’t work on 64-bit. Wrong mod file.

  • fawkstrot October 4, 2010, 6:36 pm

    I’m getting odd Gateway Timeout errors using this method. It will serve out 100 pages properly, then one of the 504’s. Sometimes they’ll happen a lot, sometimes they won’t happen for quite some time. It only happens when using fastcgi. I’ve tried everything I can think of, including scouring Google for information on how to increase the fcgi timeout, etc. Nothing seems to be working.

    Any help?

  • Josh Jameson October 14, 2010, 3:23 pm

    If you want to load a php.ini file, i found the following works. Thank you for the excellent tutorial.

    # Shell Script To Run PHP5 using mod_fastcgi under Apache 2.x
    # Tested under Red Hat Enterprise Linux / CentOS 5.x
    ### Set PATH ###
    ### no editing below ###
    bash -l -c “$PHP_CGI –php-ini=/etc/php.ini”

  • Ali January 9, 2011, 9:15 pm

    root@server [/opt/mod_fastcgi-2.4.6]# set top_dir=/usr/lib64/httpd
    root@server [/opt/mod_fastcgi-2.4.6]# yum install httpd-devel
    Loaded plugins: fastestmirror, securitynstall httpd-devel packageyum install htt
    Loading mirror speeds from cached hostfile
    * addons: mirror.sov.uk.goscomb.net
    * base: mirror.sov.uk.goscomb.net
    * extras: mirror.netrino.co.uk
    * updates: mirror.netrino.co.uk
    Excluding Packages in global exclude list
    Setting up Install Process
    No package httpd-devel available.
    Nothing to do
    root@server [/opt/mod_fastcgi-2.4.6]# make top_dir=/usr/lib64/httpd
    Makefile:12: /usr/lib64/httpd/build/special.mk: No such file or directory
    make: *** No rule to make target `/usr/lib64/httpd/build/special.mk’. Stop.
    root@server [/opt/mod_fastcgi-2.4.6]#

    I use centos 5.5 64bit
    What is the problem?

    • Matthew Schwartz March 30, 2015, 5:46 pm

      This is where I am stuck as well, using Centos 6.5 x64

  • Levente Peres January 19, 2011, 10:33 am

    Hi there,

    Useful tutorial.

    However, SELinux will throw this utterly off the chart. It’s not enough to…

    setsebool -P httpd_unified=0

    SELinux will still continue to prevent your apps from working somehow – at least if it’s anything more complex than a phpinfo(); For example, try to run a wordpress on it in name based virtualhost and you will see… I know it’s SELinux because if I do

    setenforce 0

    Everything immediately starts working just fine. But this is not very desirable.

    I’d really like to see a tutorial which covers this issue taking into consideration SELinux + CentOS 5.5… Or at least some more info on this topic…


  • A.Jesin February 3, 2011, 6:03 pm

    Should php scripts be chmodded +x to make them executable ?

  • cjay February 22, 2011, 4:09 pm

    Hi. I have just followed these instructions to install FastCGI on CentOS, but when I load the page, the styling has vanished. Also, when I click on a .PHP link, I see the PHP code, not the page. Hope you can help!!

  • asdf June 2, 2011, 11:13 am

    My Apache serves html files which are in files without extension (i.e. no .html or .php). How do I tell apache to process these as PHP files?

  • Rob Freeman August 1, 2011, 11:24 pm

    Don’t forget, if you’re doing Virtual Host configuration, you’ll need to add the below between the

    DirectoryIndex index.php index.html

  • la3des February 13, 2012, 8:05 pm

    I think you don’t need to install fastcgi separately. Because this conf. uses only php-cgi.

    look this


  • gezginrocker February 16, 2012, 12:59 pm

    I’m a complete linux newbie and I am trying to setup fastcgi on scientific linux.

    I did everything, but now when I open my site I’m getting the error below. can anyone help me please? thanks

    You don’t have permission to access /cgi-bin/php.fcgi/index.php on this server.

  • gezginrocker March 4, 2012, 2:13 pm

    I have found out my problem above is related to .htaccess file.

    If it is empty, everything is working fine. If it is populated, I’m getting either “Forbidden
    You don’t have permission to access /cgi-bin/php.fcgi/index.php on this server.” or “500 Internal Server Error”

  • gezginrocker March 5, 2012, 11:24 pm

    I’m sorry for triple post, but I found a solution to my problem, and I wanted to share it in case it helps other people. I changed “Options None” to “Options +ExecCGI” at the virtual host example below:

    AllowOverride None
    Options None
    Order allow,deny
    Allow from all

  • Fred April 13, 2012, 6:05 am

    (8)Exec format error: exec of ‘/var/www/html/cgi-bin/php.fcgi’ failed

    Anyone know what’s going on there?

  • nadlerz May 14, 2012, 3:02 pm

    Tested working under CentOS 6.2 ?

  • Durga Prasad July 29, 2012, 2:48 pm

    Thank You very much.
    I was almost there but not making headway for a few hours of continuous working.
    my modperl got burned and was trying to get fastcgi work in a hurry.
    your article helped me complete the installation of RT – Request tracker .
    thanks again/Durga Prasad

  • Peter November 5, 2012, 6:49 pm

    For those of you receiving an error like ->

    The requested URL /var/www/cgi-bin/php.fcgi/index.php was not found on this server.

    Check your server error logs, mine showed the following info:

    /var/www/cgi-bin/php.fcgi: line 9: exec: /usr/bin/php-cgi: cannot execute: No such file or directory

    Which means that a simple: yum install php-cgi resolved all of my issues (make sure to restart apache after installation)

  • Nio Michêlenguez April 28, 2014, 12:24 pm

Leave a Comment