Most tutorials we’ve written about PHP-FPM have been done with support for Nginx. This brief tutorial is going to show you how to integrate PHP-FPM and Apache2 to improve your website performance.

Nowadays website performance is key. If your website or WordPress blog isn’t fast enough, you may be losing audience to your competitors with faster websites and blogs. That’s why you should always focus on adding features that will help speed up your blogs.

PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with additional features useful for websites based on PHP, like those built with WordPress or Drupal and Joomla support.

This setup will be carried out on Ubuntu 14.04 servers with Apache2 and PHP5 support. When you run Apache2 with PHP-FPM support, noticeable improvements will your visitor experience.

To get started with installing Apache2, PHP-FPM on Ubuntu 14.04, run continue below.

 

  • Installing Apache2 on Ubuntu 14.04

First things first. Go and install Apache2 with fastcgi module. To do that on Ubuntu, run the commands below.

sudo apt-get update && sudo apt-get install apache2-mpm-worker

 

  • Installing PHP5 with PHP5-FPM support

The next step is installing PHP5 with PHP5-FPM support. To do that, run the commands below

sudo apt-get install libapache2-mod-fastcgi php5-fpm php5

 

The next step is to enable PHP5-FPM module. To do that, run the commands below

sudo a2enmod actions fastcgi alias

 

After that, restart Apache2 so that the newly enabled modules are applied.

sudo service apache2 restart

 

The configuration block for PHP5-FPM that work with Apache2 is the one below. The PHP5-FPM block can be added to the entire server configuration file so it be applied to all VirtualHosts.

If you only want to enable PHP5-FPM for individual VirtualHost, then you must add it to each VirtualHost configuration file.

To apply it globally you can create a php5-fpm.conf file in /etc/apache2/conf-available.  Then link it symbolically to /ectc/apache2/conf-enabled

Since we’re not going to apply PHP5-FPM globally in this post, let’s put it in Apache2 default VirtualHost file. To do that, run the commands below to open the file.

sudo vi /etc/apache2/sites-available/000-default.conf

 

Then copy and paste this PHP5-FPM block in the file just before the closing </VirtualHost>

[....]
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
<IfModule mod_fastcgi.c>
AddHandler php5-fcgi .php
Action php5-fcgi /php5-fcgi
Alias /php5-fcgi /usr/lib/cgi-bin/php5-fcgi
FastCgiExternalServer /usr/lib/cgi-bin/php5-fcgi -socket /var/run/php5-fpm.sock -pass-header Authorization
</IfModule>
[....]

 

Save the file and restart Apache and PHP5-FPM.

sudo service apache2 restart
sudo apt-get php5-fpm restart

That’s it!

Enjoy!