Setting up PHP-FPM allows for an easy way to provide for isolation of PHP running under different users. Previously, suPHP offered a way to do this, but has been unmaintained since 2013. suPHP suggests PHP-FPM as an alternative.

PHP-FPM, or PHP FastCGI Process Manager, utilizies FastCGI as the communication mechanism that Apache uses to talk to your PHP processes. The Process Manager will ensure that processes are running (restarting them if they crash), and spawning additional processes to handle load as necessary, based on your configuration.

Installation & PHP-FPM Configuration

Ubuntu

apt-get install php7.0-fpm
systemctl start php7.0-fpm
systemctl enable php7.0-fpm

Ubuntu stores PHP-FPM pool configuration files under /etc/php/7.0/fpm/pool.d/ (7.0 is the PHP version installed, this structure allows you to have multiple PHP versions running) for different users or websites.

General and default PHP-FPM configuration is located at /etc/php/7.0/fpm/php-fpm.conf, this file allows you to configure the logging mechanism, process limits, eventing method, etc. You should review this file to ensure the defaults make sense for you.

The php.ini file for PHP-FPM is located at /etc/php/7.0/fpm/php.ini.

Managing Pool Instances

There is no requirement for the naming of PHP-FPM pool configuration files, but we suggest utilizing either the username or domain name based on your requirements for site isolation. In our example, we’re basing the pool name off the user (svwh):

[svwh]
user = svwh
group = svwh
listen = /run/php/php7.0-fpm-svwh.sock
listen.owner = www-data # This should match the user that Apache is running as, www-data is the Ubuntu default
listen.group = www-data # This  should match the group that Apache is running as, www-data is the Ubuntu default
pm = dynamic # You can choose 'dynamic', 'ondemand', or 'static' as the Process Management method
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
# If you wish to chroot to limit what files PHP can see, you can utilize the chroot configuration setting
chroot = /home/svwh/public_html/

Once you’ve configured your FPM pool(s) you’ll need to reload PHP-FPM service php7.0-fpm reload

Configuring Apache

You’ll need to ensure that you have both mod_proxy and mod_proxy_fcgi enabled in Apache to tie into PHP-FPM. If they are not enabled you can enable them as such:

ln -s /etc/apache/mods-enabled/proxy.load /etc/apache2/mods-available/proxy.load
ln -s /etc/apache/mods-enabled/proxy_fcgi.load /etc/apache2/mods-available/proxy_fcgi.load

Once that’s done just reload apache service apache2 reload

Now that you’ve got the appropriate Apache proxying functionality enabled, you just need to modify your virtual host configuration file to use PHP-FPM at the unix socket you configured in the pool.d file.

Your Virtual Host configuration, located in /etc/apache2/sites-enabled/ should look similar to the following:

<VirtualHost *:80>
  ServerName www.svwh.net
  ServerAlias svwh.net

  ## Vhost docroot
  DocumentRoot "/home/svwh/public_html"

  ## PHP-FPM
  ProxyPassMatch ^/(.*\.php(/.*)?)$ unix:/run/php/php7.0-fpm-svwh.sock|fcgi://localhost/home/svwh/public_html/
  ProxyPassReverse ^/(.*\.php(/.*)?)$ unix:/run/php/php7.0-fpm-svwh.sock|fcgi://localhost/home/svwh/public_html/
</VirtualHost>

The ProxyPassMatch and ProxyPassReverse lines are for PHP-FPM.

Once your Virtual Hosts are properly configured just reload apache once again: service apache2 reload

That’s it, you’ve now got Apache using FastCGI with PHP-FPM