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+.
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
Untar tar ball, type:
# tar -zxvf mod_fastcgi-current.tar.gz
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
Compile and install mod_fastcgi for 64 bit system, enter:
# make top_dir=/usr/lib64/httpd
make install top_dir=/usr/lib64/httpd
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
[Mon Dec 29 23:24:44 2008] [notice] caught SIGTERM, shutting down
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)
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:
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
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:
Create a /websites/home/nixcraft.com/cgi-bin/php.fcgi as follows:
Set permission and restart httpd:
# chmod +x /websites/home/nixcraft.com/cgi-bin/php.fcgi
CentOS Apache 2 FastCGI