Nov 272012
Article Apache

An Apache web server can employ different multiprocessing strategies to benefit from the CPU and RAM resources available at the system where it runs. Among them, the most common ones are the “prefork” and “worker” multiprocessing modules (mpm).

Prefork – prefork is the most common mpm strategy used in WordPress installations. In this mode, the main apache process runs a set of children processes, and distributes to them the client requests received.

Worker – If apache is running with worker mpm enabled, the main process also runs a set of children processes, but each of them is also executing a set of execution threads. In this way the processing power increases, and the amount of RAM required is reduced. Running Apache with worker mpm improves the scalability of the service, that is able to process a higher volume of requests than running an apache server with prefork mpm and the same hardware resources.

But in order to use the worker mode, all the modules used by the server must be thread-safe. A notable exception is the case of the mod_php module used to run PHP scripts under apache. This module is not thread-safe, forcing a typical WordPress installation to configure apache in prefork mpm mode.

However, it is possible to use mod_fcgid (Fast CGI) to run a WordPress site with an apache server executing worker mpm, as explained in this post. The apache server will handle all non-php requests, including requests for static content (css, javascript, images,…). When a request for a php page comes in, apache will pass the request to an external process running the PHP interpreter. In this way, the number of apache processes and threads per process can be configured independently from the number of external PHP processes, optimizing the usage of available resources.

How to check the multiprocessing mode of an apache server

In a server running debian or ubuntu, the name of the apache executable is “apache2”. In other servers, such as CentOS, the name is “httpd”. To list the modules compiled in, this executable can be given the “-l” or “-V” switches:

$ /usr/sbin/apache2 -l
Compiled in modules:

In the example above, the “prefork.c” module is in the list, and therefore the server is using prefork mpm. Otherwise, the module “worker.c” would be listed.

Using the “-V” switch, we get the output:

$ /usr/sbin/apache2 -V
Server version: Apache/2.2.16 (Debian)
Server built:   Aug 29 2011 19:19:32
Server's Module Magic Number: 20051115:24
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:     Prefork
  threaded:     no
    forked:     yes (variable process count)
Server compiled with....
 -D APACHE_MPM_DIR="server/mpm/prefork"
 -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_LOCKFILE="/var/run/apache2/accept.lock"
 -D DEFAULT_ERRORLOG="logs/error_log"
 -D AP_TYPES_CONFIG_FILE="mime.types"
 -D SERVER_CONFIG_FILE="apache2.conf"

How to set up apache and PHP with worker mpm and mod_fcgid

The first step is to disable the mod_php4, mod_php5 apache modules, because these are not thread-safe, and therefore they are not compatible with worker mpm:

$ sudo a2dismod php4
ERROR: Module php4 does not exist!
dev@squeeze:/web/blog$ sudo a2dismod php5
Module php5 disabled.
Run '/etc/init.d/apache2 restart' to activate new configuration!

Next, we must make sure that we have the following packages installed:

  • apache2
  • apache2-mpm-worker
  • libapache2-mod-fcgid
  • php5-cli
  • php5-cgi

In Debian, the apt-get command is used to install the missing packages. For instance:

$ sudo apt-get install apache2-mpm-worker libapache2-mod-fcgid php5-cgi

When apache2-mpm-worker is installed, apt-get uninstalls apache2-mpm-prefork, and also updates other related packages to their thread-safe versions:

The following packages will be REMOVED:
  apache2-mpm-prefork libapache2-mod-php5 mysql-client-5.1 mysql-server-5.1 mysql-server-core-5.1
The following NEW packages will be installed:
  apache2-mpm-worker libaio1 liblzma5 libmysqlclient18 mysql-client-5.5 mysql-server-5.5
The following packages will be upgraded:
  apache2 apache2.2-bin apache2.2-common libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap
  libmysqlclient-dev libssl1.0.0 libxml2 libxml2-dev mysql-client mysql-common mysql-server
php5-cli php5-common php5-curl php5-gd php5-mysql psmisc
19 upgraded, 7 newly installed, 6 to remove and 1254 not upgraded.

Then, the “actions” and “fcgid” modules are enabled:

$ sudo a2enmod actions
Enabling module actions.
$ sudo a2enmod fcgid
Module fcgid already enabled

Now we create a configuration file “/etc/apache2/conf.d/php.conf” with the following content:

<Directory /usr/share>
  AddHandler fcgid-script .php
  FCGIWrapper /usr/lib/cgi-bin/php5 .php
  Options ExecCGI FollowSymlinks Indexes

<Files ~ (.php)>
  AddHandler fcgid-script .php
  FCGIWrapper /usr/lib/cgi-bin/php5 .php
  Options +ExecCGI
  allow from all

and finally, the web server is restarted:

$ sudo /etc/init.d/apache2 restart



 Posted by at 11:50 am

 Leave a Reply