Setting up Apache2 with the Worker MPM on Ubuntu

I hit a bottle neck the other day during development (stress-testing) and I found by modifying my Apache2 MPM I could achieve better results. By default my installation of Apache2 on Ubuntu had the Prefork Multi-Processing module configured which is great for single or dual core systems where one process handles one request at a time (no threading) and is thread safe. I wanted to utilize the resources on the box so I reconfigured my Apache2 install to use the Worker Multi-Processing Module. The Worker MPM is generally better for multi core systems.

This Multi-Processing Module (MPM) implements a hybrid multi-process multi-threaded server. By using threads to serve requests, it is able to serve a large number of requests with less system resources than a process-based server. Yet it retains much of the stability of a process-based server by keeping multiple processes available, each with many threads.

Lets begin Check what version you have by running the following command

$ /usr/sbin/apache2ctl -V
Server version: Apache/2.2.17 (Ubuntu)
Server built:   Feb 22 2011 18:35:11
Server's Module Magic Number: 20051115:25
Server loaded:  APR 1.4.2, APR-Util 1.3.9
Compiled using: APR 1.4.2, APR-Util 1.3.9
Architecture:   64-bit
Server MPM:     Worker
  threaded:     yes (fixed thread count)
    forked:     yes (variable process count)
Server compiled with....
 -D APACHE_MPM_DIR="server/mpm/worker"
 -D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
 -D HTTPD_ROOT="/etc/apache2"
 -D SUEXEC_BIN="/usr/lib/apache2/suexec"
 -D DEFAULT_PIDLOG="/var/run/"
 -D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
 -D DEFAULT_ERRORLOG="logs/error_log"
 -D AP_TYPES_CONFIG_FILE="mime.types"
 -D SERVER_CONFIG_FILE="apache2.conf"

This tells you the Server MPM being used amongst other things. Make sure the Worker MPM is right for you and your environment before going any further.

#01 - Install Apache MPM Worker Note: Before proceeding with this step, if you have made any changes to the php.ini then I suggest you back it up because the file will be overwritten and you will have to make those changes again. I found this out the hard way. Thankfully I didn’t have many differences from the vanilla file. The new file is located here: /etc/php5/cgi/ after the install.

$ sudo apt-get install apache2-mpm-worker php5-cgi
Reading state information... Done
The following packages were automatically installed and are no longer required:
The following packages will be REMOVED apache2-mpm-prefork libapache2-mod-php5
The following NEW packages will be installed apache2-mpm-worker
0 upgraded, 1 newly installed, 2 to remove and 46 not upgraded.
Need to get 0B/259kB of archives. After this operation, 6193kB disk space will be freed.

As you may have noticed by installing apache2-mpm-worker, apache2-mpm-prefork and libapache2-mod-php5 are removed. The PHP5 library is removed because apache2-mpm-worker is not thread safe, scripts in the old library which use the PHP fork(); function would error. php5-cgi was included in the install command for this reason.

#02 - Enable necessary modules

$ a2enmod cgi
$ a2enmod cgid

This does nothing else but creates the proper links to the module .load and .conf files, if you wanted to disable these at any point do so by running a2dismod

#03 - Activate the mod_actions apache modules

$ cd /etc/apache2/mods-enabled
$ sudo ln -sf ../mods-available/actions.load
$ sudo ln -sf ../mods-available/actions.conf

#04 - Modify configuration options to enable the worker module to use the newly installed php5-cgi

$ sudo vi /etc/apache2/mods-available/actions.conf

Add the following to the file:

Action application/x-httpd-php /cgi-bin/php5

#05 - (Optional) Configure your module settings

$ sudo vi /etc/apache2/apache2.conf

Change any of the following to the desired value

StartServers 2
MaxClients 150
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestsPerChild 0

#06 - Verify install/configuration

$ /usr/sbin/apache2ctl -t
Syntax OK

If you got something other than Syntax OK go over the setup and fix any necessary errors.

#07 - Restart Apache to apply changes

$ sudo /etc/init.d/apache2 restart

Happy keyboard mashing,


18 May 2011