The Apache HTTP web server has evolved through the years to work in different environments and solve different needs. One important problem Apache HTTP has to solve, like any web server, is how to handle different processes to serve an http protocol request. This involves opening a socket, processing the request, keeping the connection open for a certain period, handling new events occurring through that connection, and returning the content produced by a program made in a particular language (such as PHP, Perl, or Python). These tasks are performed and controlled by a Multi-Processing Module (MPM).
Apache HTTP comes with three different MPM:
The MPM Event module is a fast multi-processing module available on the Apache HTTP web server.
PHP-FPM is the FastCGI Process Manager for PHP. The FastCGI protocol is based on the Common Gateway Interface (CGI), a protocol that sits between applications and web servers like Apache HTTP. This allows developers to write applications separately from the behavior of web servers. Programs run their processes independently and pass their product to the web server through this protocol. Each new connection in need of processing by an application will create a new process.
By combining the MPM Event in Apache HTTP with the PHP FastCGI Process Manager (PHP-FPM) a website can load faster and handle more concurrent connections while using fewer resources.
In this tutorial you will improve the performance of the LAMP stack by changing the default multi-processing module from pre-fork to event and by using the PHP-FPM process manager to handle PHP code instead of the classic
mod_php in Apache HTTP.
Before you begin this guide you’ll need the following:
Ubuntu inherits scripts to enable or disable Apache HTTP modules from its parent distribution, Debian. You’ll use this toolset in this step to disable the Pre-fork module and enable the Event module.
In this step you will stop Apache HTTP, disable the
PHP 7.2 module linked to the Pre-fork module, and then disable Pre-fork to immediately enable the Event module.
First you’ll stop the Apache HTTP service:
- sudo systemctl stop apache2
Now you can disable the
PHP 7.2 module, which is related to the Pre-fork module:
- sudo a2dismod php7.2
Then disable the Pre-fork MPM module:
- sudo a2dismod mpm_prefork
Now enable the Event MPM module:
- sudo a2enmod mpm_event
You’ve switched the MPM from pre-fork to event and removed the
PHP 7.2 module connection between PHP and Apache HTTP. In the next step you’ll install the
php-fpm module, as well as the related libraries and proxy modules. You’ll configure Apache HTTP so that it can communicate with PHP too.
At this stage you’ve switched the way Apache HTTP processes connections by moving from the Pre-fork MPM to Event. However along the way you’ve disabled the PHP module that connected Apache HTTP with any program running on PHP.
In this step you’ll install the PHP-FPM processor so Apache HTTP is again able to process PHP programs. And you’ll also install the dependency libraries and enable the modules so both can cooperate smoothly and quicker than before.
php-fpm. The following command will install the PHP-FPM package and it will automatically enable the
php7.2-fpm service integrated with systemd, so the service is started at boot time:
- sudo apt install php-fpm
In order to communicate, Apache HTTP and PHP need a library enabling that capacity. You’ll now install
libapache2-mod-fcgid, which is able to serve as an interface between programs with web servers, and it’s specific to Apache HTTP. This communication will happen through a UNIX socket.
Install this library:
- sudo apt install libapache2-mod-fcgid
php-fpm and the
libapache2-mod-fcgid, but neither are enabled yet.
First enable the
php-fpm module with the following command:
- sudo a2enconf php7.2-fpm
Second enable Apache HTTP
- sudo a2enmod proxy
Third enable the FastCGI
proxy module in Apache HTTP:
- sudo a2enmod proxy_fcgi
Note: You can read the configuration of this interaction between PHP programs and Apache HTTP through a UNIX socket with the following:
- cat /etc/apache2/conf-enabled/php7.2-fpm.conf
Everything is now in place so you can start Apache HTTP. You’ll make a configuration check first:
- sudo apachectl configtest
After that you can proceed to restart Apache HTTP, since it was automatically started when installing the FastCGI library
- sudo systemctl restart apache2
You’ve installed the
php-fpm module, configured Apache HTTP to work with it, enabled the necessary modules for the FastCGI protocol to work, and started the corresponding services.
Now that Apache has the Event MPM module enabled and PHP-FPM is present and running, it is time to check everything is working as intended.
In order to check that the configuration changes have been applied you’ll run some tests. The first one will check what multi-processing module Apache HTTP is using. The second will verify that PHP is using the FPM manager.
Check the Apache HTTP server by running the following command:
- sudo apachectl -M | grep 'mpm'
Your output will be as follows:
You can repeat the same for the
proxy module and FastCGI:
- sudo apachectl -M | grep 'proxy'
The output will show:
If you would like to see the entire list of the modules, you can remove the the second part of the command after
It is now time to check if PHP is using the FastCGI Process Manager. To do so you’ll write a small PHP script that will show you all the information related to PHP.
Run the following command to write a file named as follows:
- sudo nano /var/www/your_domain/info.php
Add the following content into the
<?php phpinfo(); ?>
Now visit your server’s URL and append
info.php at the end like so:
The server API entry will be FPM/FastCGI.
info.php file after this check so no information about the server is publicly disclosed:
- sudo rm /var/www/yourdomain.com/info.php
You’ve checked the working status of the MPM module, the modules handling the FastCGI and the handling of PHP code.
You’ve optimized your original LAMP stack, so the number of connections to create new Apache HTTP processes has increased, PHP-FPM will handle PHP code more efficiently, and overall resource utilization has improved.
See the Apache HTTP server project documentation for more information on the different modules and related projects.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.