How Migrating Apache MPM from Prefork to Event-based using PHP-FPM saved our day…

pushpendra chauhan
Dec 10, 2018 · 3 min read

We were using Apache2 in prefork mode for our backend API server based on PHP, suddenly we started getting issue related to scalability and found that Apache’s prefork is the main culprit, So instead of scaling vertically we decided to solve this problem by migrating to Event-based MPM and this article describes how to migrate from process-based Prefork to the thread based Event-based MPM using PHP-fpm on Ubuntu 12.04.

By default, Apache comes with the Prefork MPM which is Non-threaded, process-based and consumes more resources than the other ones like (Worker, Event)

Understanding How things work under the hood Apache Prefork vs Apache Event for your Php application

When Apache is using Prefork MPM, it will create a new process for each request and then that process executes your PHP code, on the other side when event-based MPM is used , A pool of PHP thread is maintained by php-fpm server which is responsible for serving your request forwarded by apache , basically Php-fpm server uses sockets or TCP port to listen for new requests and Apache forwards new requests to this port or socket.

This Article will describe the details of steps which we had taken to migrate from Prefork to Event-based MPM rather than describing details of the module, for more details description of each module you can refer to https://httpd.apache.org/docs/2.4/mpm.html

First, we need to all install essential packages

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

Now we need to create a configuration file for our PHP5-FPM in apache configuration directory , for this we need to change our directory to

cd /etc/apache2/conf-available/

now create a new file and name it to php5-fpm.conf

sudo nano /etc/apache2/conf-available/php5-fpm.conf

You need to add following details to this conf file

<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

<Directory /usr/lib/cgi-bin>
Require all granted
</Directory>

</IfModule>

Above configuration is using the socket-based connection to connect with fast CGI server, if you want to use TCP based connection, you can use the following configuration, though TCP based connection is recommended for production.

<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 -host 127.0.0.1:9999 -pass-header Authorization
<Directory /usr/lib/cgi-bin>
Require all granted
</Directory>

</IfModule>

Now we need to enable fast CGI mode and enable config for php-fpm in apache, we can do it by the following command.

sudo a2enmod actions fastcgi alias
sudo a2enconf php5-fpm

Once done check for the Server API in apache PHP info, you can create following PHP file to see PHP info.

<?php phpinfo(); ?>

If server API is still pointing to Apache 2.0 Handler then disable the old PHP mod by using the following command

sudo a2dismod php5

and restart the apache2

sudo service apache2 restart

By this time Apache mode must be changed from Prefork to Mpm, you can check this by the following command.

sudo apachectl -V | grep MPM

During this in anytime you feel like you have messed up the config, you can revert back to the older configuration (Prefork ) by using the following command

sudo a2dismod actions fastcgi
sudo a2disconf php5-fpm
sudo a2dismod mpm_event
sudo a2enmod mpm_prefork
sudo a2enmod php5
sudo service apache2 restart

Bonus Tips for production

  1. For setting up mpm_event.conf refer https://medium.com/@sbuckpesch/apache2-and-php-fpm-performance-optimization-step-by-step-guide-1bfecf161534
  2. Enable opcode caching for production.
  3. We faced a major set back when we figured out that .htaccess were not working with event MPM with our version of Ubuntu and we were heavily using it to set up headers, which we solved by setting up headers using Nginx web server which works as a proxy server in our case.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade