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.
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+.
Step-1: Install mod_fastcgi
Make sure required packages are installed (httpd-devel and apr-devel required to compile mod_fastcgi), Issue the command
# yum install libtool httpd-devel apr-devel apr php php-fpm
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
Step-2: Configure PHP as FastCGI process
First, you need to disable mod_php5, enter:
# mv /etc/httpd/conf.d/php.conf /etc/httpd/conf.d/php.conf.disable
Create a file named fastcgi.conf
#vi /etc/httpd/conf.d/fastcgi.conf
Add the followings lines
####
LoadModule fastcgi_module modules/mod_fastcgi.so
#
# FastCGIExternalServer /usr/sbin/php-fpm -host 127.0.0.1:9000
FastCgiExternalServer /usr/sbin/php-fpm -socket /var/run/php-fpm/php-fpm.sock
AddHandler php-fastcgi .php
SetHandler php-fastcgi-virt
Action php-fastcgi-virt /usr/sbin/php-fpm.fcgi virtual
Action php-fastcgi /usr/sbin/php-fpm.fcgi
ScriptAlias /usr/sbin/php-fpm.fcgi /usr/sbin/php-fpm
Options ExecCGI FollowSymLinks
SetHandler fastcgi-script
Order allow,deny
Allow from all
#######################
Step-3: Configure php-fpm
#vi /etc/php-fpm.d/www.conf
Change the line below so that we can use socket instead of port
#listen = 127.0.0.1:9000
listen = /var/run/php-fpm/php-fpm.sock
#/etc/init.d/php-fpm restart
# service httpd restart