Red Hat / CentOS Apache 2 FastCGI PHP Configuration

Posted on in Categories Apache, CentOS, Howto, Networking, package management, php, RedHat/Fedora Linux, Security, Tips last updated December 30, 2008

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>
        ServerAdmin [email protected]
        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

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

36 comment

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

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

  3. 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 ?

  4. Hi,
    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

  5. Hi,

    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.

  6. 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?

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


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


  9. 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?

  10. 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”

  11. [email protected] [/opt/mod_fastcgi-2.4.6]# set top_dir=/usr/lib64/httpd
    [email protected] [/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
    [email protected] [/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.
    [email protected] [/opt/mod_fastcgi-2.4.6]#

    I use centos 5.5 64bit
    What is the problem?

  12. 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…


  13. 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!!

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

  15. 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”

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

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

  18. 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)

Leave a Comment