in ,

Optimizing Apache2

Optimizing Apache2


Apache is described as the worlds most popular webserver, and part of the reason for that is the extensive flexibility it offers.

The flexibility allows many interesting, and useful, things to be done with the server, but the cost is a higher overhead in processing requests. This has lead to the rise of several alternative web-servers aiming to be more lightweight and flexible. (These include products such as nginx and lighttpd.)

Choosing the correct worker

The Apache webserver comes with several different “Multi-Processing Modules”, or MPMs. These are server-cores which work in a slightly different fashion.

NOTE: You can only use one MPM.

Although there are more, traditionally you have three main options to choose between:

  • apache2-mpm-prefork
    • This is stable and reliable, working well with extensions which are not thread-safe.
    • It performs reasonably well, at low-levels of concurrency.
  • apache2-mpm-worker
    • This module uses threading, which improves concurrency.
    • A new thread is opened for new connections, so long-lived connections tend to tie up threads.
  • apache2-mpm-event
    • This is similar to the worker MPM, but uses a single dedicated thread to cope with KeepAlive-requests, rather than allowing all threads to be tied up in that fashion, via long-lived requests.

Although apache2-mpm-event is relatively new, and thus less well known than the alternatives, it is the fastest of the big-three MPMs.

The most important caveat with the different MPM modules is that you cannot use mod_php with either apache-mpm-worker, or apache2-mpm-event, instead you must handle your PHP code via a different mechanism (which turns out to be a speedup in its own right).

Disabling .htaccess files

If you allow configuration files to be present beneath your DocumentRoot directory you cause Apache to constantly test for them.

For example if you have a DocumentRoot set to /var/www and a client requests the URL Apache will look for each of the following files:

  • /var/www/a/long/path/.htaccess
  • /var/www/a/long/.htaccess
  • /var/www/a/.htaccess

This is clearly a lot of needless searching, and it is needless searching which will be constantly repeated by future HTTP-requests.

To disable this behaviour you need to do two things:

  • Make any configuration settings which were previously present in a .htaccess file inside your main server configuration files – be that in a virtual host file, or in the global section.
  • Disable the lookup of .htaccess files.

To disable this behaviour simply set:

<Directory />
  AllowOverride None

#  Assuming your DirectoryRoot is set to /var/www
<Directory /var/www/>
  AllowOverride None

Tuning for PHP

As mentioned above the fastest general-purpose worker is the apache2-mpm-event, and this isn't compatible with PHP.

To solve this the recommended approach is to deploy the php5-fpm module.

On a Debian system you can install the PHP5 FPM module via:

# apt-get install php5-fpm

To further increase performance it is recommended that you connect to FPM via a Unix socket, rather than a network socket. So you should change /etc/php5/fpm/pool.d/www.conf from :

listen =


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

Once you've made that change you can restart via:

# /etc/init.d/php5-fpm restart

With the PHP-FPM part configured you now need to wire up Apache to use it, via the fastcgi module. Again on a Debian system you can install that via:

# apt-get install libapache2-mod-fastcgi
# a2enmod libapache2-mod-fastcgi

Once installed you can update the configuration file /etc/apache2/mods-available/fastcgi.conf to read:

<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

General Webserver Notes

We also have a page of general notes for webservers.


Author: Steve Kemp, of

What do you think?

Leave a Reply

Your email address will not be published. Required fields are marked *

      Optimizing DNS

      Optimizing DNS

      PHP Lib cURL workshop and action tips

      PHP Lib cURL workshop and action tips