Configure Apache to Use PHP-FPM

Apache can be configured to parse PHP scripts through an external FastCGI process, such as the PHP proces manager, php-fpm. This can increase the performance of PHP applications, Apache, and offer greater flexibility. Apache would be the frontend server handling requests and static content.

Prerequisites

To configure Apache to parse PHP scripts through the (php-fpm), the mod_fastcgi Apache module must be installed. As it is not available through yum, it must be compile from source. For this to be done, we must install the Apache development headers and a compiler.

yum install httpd-devel gcc automake

The latest source for mod_fastcgi can now be retrieved and compiled.

wget http://www.fastcgi.com/dist/mod_fastcgi-current.tar.gz
tar -zxvf mod_fastcgi-current.tar.gz
cd mod_fastcgi-2.4.6/
cp Makefile.AP2 Makefile

32-bit Server

make top_dir=/usr/lib/httpd
make install top_dir=/usr/lib/httpd

64-bit Server

make top_dir=/usr/lib64/httpd
make install top_dir=/usr/lib64/httpd

Apache must now be configured to load mod_fastcgi.

echo "LoadModule fastcgi_module modules/mod_fastcgi.so" > /etc/httpd/conf.d/mod_fastcgi.conf
/etc/init.d/httpd restart

The PHP Hanlder

Now that Apache support mod_fastcgi, requests to PHP files can be passed to an external process/server. Add the following code to a site's VirtualHost container.

# Associate an alias mapping for the 'fake' fcgi call.
Alias /php5.fcgi /var/www/php5.fcgi
# Assign the 'fake' fcgi to call to an 'external' FastCGI Server
FastCGIExternalServer /var/www/php5.fcgi -flush -host 127.0.0.1:9000

# Create the handler mappings to associate PHP files with a call to '/php5.fcgi'
AddType application/x-httpd-fastphp5 .php
Action application/x-httpd-fastphp5 /php5.fcgi

Replace '/var/www/' with the DocumentRoot of the VirtualHost, and '9000' with the port the desired php-fpm process is bound to.