Apache 2 Event MPM and PHP 5 FastCGI on Raspberry Pi

I have been running my blog on Apache 2 with Event MPM for a while and thought I should share my setup on Raspberry Pi 2 Model B with Raspbian for the benefit of others.


Previously, I blogged about my LAMP setup. I have since optimised my Apache 2.4 and PHP 5 settings to make them utilise less memory. This is crucial when my Pi has just 1GB RAM.


You have Apache 2.4 currently setup with default MPM of Prefork and mod_php enabled.

Switching Apache MPM from Prefork to Event and use PHP FastCGI

Long story short, switching Apache 2 MPM from Prefork to Event helps to reduce memory usage. MPM is acronym for Multi-Processing Module.

Firstly, we need to install Apache FastCGI Module, PHP 5 FastCGI Process Manager (PHP5-FPM) and Apache 2 Event MPM:

sudo apt-get install libapache2-mod-fastcgi php5-fpm apache2-mpm-event

Next, disable Apache Prefork and mod_php:

sudo a2dismod mpm_prefork php5

Then, enable the following Apache modules:

sudo a2enmod mpm_event actions fastcgi rewrite

If you are running a database server and some other memory hungry programs, you may want to tune down (for stability; prevent crashes) the Event MPM defaults a little at /etc/apache2/mods-enabled/mpm_event.conf:

# event MPM
# StartServers: initial number of server processes to start
# MinSpareThreads: minimum number of worker threads which are kept spare
# MaxSpareThreads: maximum number of worker threads which are kept spare
# ThreadsPerChild: constant number of worker threads in each server process
# MaxRequestWorkers: maximum number of worker threads
# MaxConnectionsPerChild: maximum number of requests a server process serves
<IfModule mpm_event_module>
  StartServers             2
  MinSpareThreads          25
  MaxSpareThreads          75
  ThreadLimit              64
  ThreadsPerChild          25
  #MaxRequestWorkers       150
  MaxRequestWorkers        100
  MaxConnectionsPerChild   0

After enabling FastCGI, create a new folder: mkdir -p /var/www/fastcgi

Edit FastCGI configuration at /etc/apache2/mods-enabled/fastcgi.conf:

#<IfModule mod_fastcgi.c>
#  AddHandler fastcgi-script .fcgi
#  #FastCgiWrapper /usr/lib/apache2/suexec
#  FastCgiIpcDir /var/lib/apache2/fastcgi

<IfModule mod_fastcgi.c>
  AddHandler fastcgi-script .fcgi
  #FastCgiWrapper /usr/lib/apache2/suexec
  FastCgiIpcDir /var/lib/apache2/fastcgi

  Alias /php5.fastcgi /var/www/fastcgi/php5.fastcgi
  AddHandler php-script .php
  FastCGIExternalServer /var/www/fastcgi/php5.fastcgi -socket /var/run/php5-fpm.sock
  Action php-script /php5.fastcgi virtual

  # This part is not necessary to get it to work but it stops anything else from being
  # accessed from it by mistake or maliciously.
  <Directory "/var/www/fastcgi">
    Order allow,deny
    <Files "php5.fastcgi">
      Order deny,allow

Now, restart Apache:

sudo service apache2 restart

Verify that Apache is indeed using Event MPM:

sudo a2query -M

The output should be event.

Verify that PHP is indeed in FastCGI mode through phpinfo. Server API should be FPM/FastCGI.

Uninstalling Apache mod_php

I realised that upon upgrading libapache2-mod-php5, my Apache will revert from Event MPM to Prefork. To make sure this does not recur, I had to purge it from my Pi:

sudo apt-get purge libapache2-mod-php5

Tip: Caching static pages in WordPress

Linux uses unused memory for disk caching. If you are using WordPress cache plugins, you do not need to cache static pages in memory explicitly. Instead, cache to disk and let Linux decide what to cache in memory.