Apache2 MPM Worker with PHP-FPM

Written by
Date: 2012-12-26 11:13:00 00:00

Spanish version

In the previous post I have talked about Apache MPM worker and prefork mode

Today in this post I will show you how to install and configure a LAMP stack under Ubuntu using Apache MPM Worker instead of Prefork.

This is the list of what we are going to install

  • Apache 2.2 MPM Worker
  • MySQL server

We will assume you already have an Ubuntu server running, in my case is an Ubuntu server version 12.10 running on a Linode VPS.

Installing Apache

If you install Apache alone, it seems that Ubuntu install the worker pre-build version by default. So this line should install Apache.

 sudo apt-get install apache2

Check that this package is being installed: apache2-mpm-worker. Or you can force Ubuntu to install it by running:

 sudo apt-get install apache2-mpm-worker

Installing PHP

 sudo apt-get install libapache2-mod-fastcgi php5-fpm php5 php5-curl php5-gd php5-imagick php-apc php5-mysql

This packages php5-curl php5-gd php5-imagick php-apc are needed to install Wordpress in the next post, if you do not need for your installation you can leave them out.

Enable Apache modules

 sudo a2enmod actions fastcgi alias

Restart Apache

 sudo service apache2 restart

Installing MySQL

 sudo apt-get install mysql-server mysql-client

We now have the LAMP stack installed, time to configure it.

Configuring Apache to use PHP

I am hosting static and dynamic sites on the same Apache server, because of that I am going to configure PHP in a per-virtual-site basis instead of having it configured system-wide. You can choose whatever method you prefer.

PHP-FPM on Apache per Virtual Site

Just edit the coresponding configuration file in /etc/apache2/sites-enabled/ folder and add this line just before the closing </VirtualHost> tag.

 <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

PHP-FPM on Apache system-wide

To have PHP enabled for the whole server and all virtual sites, add the same above lines in a file in this folder /etc/apache2/conf.d/. You will need to create a file there named something like php-fpm.conf, anything you may want is good, as long as it has the .conf extension.


You now have a very efficient Apache server, capable to manage big loads of traffic to static files, and still able to manage PHP. Why not to use NGINX and PHP-FPM instead? That is up to you, I have gone this way because I wanted to be able to use .htaccess, and be able to follow standard instructions to install software like Wordpress, Wiki, Drupal and others, without the need to adapt everything to Nginx.

If you prefer NGINX is OK, I love NGINX too, and is the server I usually choose for my setups, I just like playing with other options.

Final note

In case you get errors while running PHP, check that this file /etc/php5/fpm/pool.d/www.conf Have this line:

 listen = /var/run/php5-fpm.sock

That is the default in Ubuntu 12.10, but may change in future versions.