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

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 /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/
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:
  • DocumentRoot: /websites/home/
  • cgi-bin directory: /websites/home/
  • php.fcgi path: /websites/home/
  • Logs file directory: /websites/home/

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

<virtualHost *:80>
        DocumentRoot "/websites/home/"
        ErrorLog "/websites/home/"
        CustomLog "/websites/home/" common
        ScriptAlias /cgi-bin/ "/websites/home/"
	<directory "/websites/home/">
		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/">
		AllowOverride None
		Options None
		Order allow,deny
		Allow from all

Create a /websites/home/ as follows:

exec $PHP_CGI

Set permission and restart httpd:
# chmod +x /websites/home/
# service httpd restart

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

🐧 36 comments so far... add one
CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat tree
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
36 comments… add one
  • solshark Jan 9, 2009 @ 14:39

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

  • omega13a Jan 20, 2009 @ 3:52

    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 Mar 19, 2009 @ 18:36

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

  • 🐧 nixCraft Mar 19, 2009 @ 18:51

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

  • Tomcat May 12, 2009 @ 14:01

    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 Jun 5, 2009 @ 14:54

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

  • celik Aug 25, 2009 @ 8:43

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

    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 Oct 16, 2009 @ 8:11


    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 Oct 30, 2009 @ 19:14

    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 Dec 2, 2009 @ 3:47


    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 Dec 4, 2009 @ 22:33

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

  • Kok Wen Yen Mar 8, 2010 @ 12:10

    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 Aug 6, 2010 @ 10:37

    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 Aug 10, 2010 @ 11:49

    Super! Thank you!

  • Krish Aug 25, 2010 @ 16:47

    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 Aug 27, 2010 @ 13:09

    Thanks for Installing FascCGI guide.

  • Mark Sep 22, 2010 @ 23:38

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

  • fawkstrot Oct 4, 2010 @ 18:36

    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 Oct 14, 2010 @ 15:23

    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 Jan 9, 2011 @ 21:15

    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:
    * base:
    * extras:
    * updates:
    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/ No such file or directory
    make: *** No rule to make target `/usr/lib64/httpd/build/’. Stop.
    root@server [/opt/mod_fastcgi-2.4.6]#

    I use centos 5.5 64bit
    What is the problem?

    • Matthew Schwartz Mar 30, 2015 @ 17:46

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

  • Levente Peres Jan 19, 2011 @ 10:33

    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 Feb 3, 2011 @ 18:03

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

  • cjay Feb 22, 2011 @ 16:09

    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 Jun 2, 2011 @ 11:13

    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 Aug 1, 2011 @ 23:24

    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 Feb 13, 2012 @ 20:05

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

    look this


  • gezginrocker Feb 16, 2012 @ 12:59

    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 Mar 4, 2012 @ 14:13

    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 Mar 5, 2012 @ 23:24

    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 Apr 13, 2012 @ 6:05

    (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 @ 15:02

    Tested working under CentOS 6.2 ?

  • Durga Prasad Jul 29, 2012 @ 14:48

    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 Nov 5, 2012 @ 18:49

    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 Apr 28, 2014 @ 12:24

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