Running PHP through PHP-FPM is pretty easy in Apache 2.4 (or Nginx for that matter) with ProxyPass, however Apache 2.2 has no built-in convenient way to do it.
There’s a number of solutions that exist to accomplish this, but it seems that the ambiguously named mod_fastcgi.c is the least bad. One of the biggest issues I ran into while setting it up, was allowing for multiple fpm pools for the different virtual hosts, and the specific set of configurations that I’ve figured out allowed me to do this quite easily.
There’s are number of intricate things which must be done to get it working correctly, and while I hope that I nor anyone else ever has to do this again – let’s face the bleak reality, it’s not entirely unlikely that we will… so here’s how we get it going;
- Download and compile (yep) the apache module from their website. I’ll spare the installation instructions since they are described in detail in the module files. Ensure you have the httpd-devel or equivalent package installed before commencing.
- In your httpd.conf, surround the configuration include directives with the following code:
1234567891011121314151617181920Include conf.d/modules/*.conf<IfModule mod_fastcgi.c>FastCgiIpcDir /var/lib/httpd/fastcgi</IfModule>## Load config files from the config directory "/etc/httpd/conf.d".#Include conf.d/*.conf<IfModule mod_fastcgi.c>AddHandler php-fastcgi .phpAction php-fastcgi /usr/sbin/php-fpm.fcgi<FilesMatch \.php$>SetHandler php-fastcgi</FilesMatch></IfModule>
- Inside your virtual host definition file, but before and outside of the definition itself, define the fpm server like so:
123<IfModule mod_fastcgi.c>FastCGIExternalServer /var/lib/php-fpm/php-fpm-prod -host 127.0.0.1:9001 -idle-timeout 900</IfModule>
- Then define it’s use inside the virtual host definition:
123<IfModule mod_fastcgi.c>ScriptAlias /usr/sbin/php-fpm.fcgi /var/lib/php-fpm/php-fpm-prod</IfModule>
Hopefully those configurations are all that you need. As always, different pools will require different names (in this case mine is called prod) and ports. Also note the directories, and ensure they exist – particularly /var/lib/php-fpm and /var/lib/httpd/fasicgi .