HTTP2 on Apache2

HTTP2 allows web servers to serve multiple content streams on the same connection. This can reduce server and network load. I have updated my Ubuntu Apache servers to enable HTTP2.

After installing the latest Ubuntu release, I needed to redo the switch to the event Multi-Processing Module (MPM) and switch PHP content delivery back to the php-fpm daemon. I had to repeat the process I originally used as documented here.

I serve PHP content so need an appropriate handler. The Apache2 PHP module does not work with the Event MPM. I believe the installation reactivated the PHP module causing Apache to switch MPM modules. I have purged the libapache2-mod-php and libapache2-mod-php8.1 modules to prevent this from reoccurring.

These instructions are for Ubuntu/Debian-based servers, but should work for other systems that use configuration trees, and have the apache configuration tools like a2enmod and a2dismod tools.

The event MPM conflicts with the Apache PHP module, so an alternate PHP handler is required. The php-fpm daemon provides a performant service. For content development servers consider the php-cgi interpreter to get immediate visibility of PHP code changes.

Switching PHP to PHP-FPM

The php-fpm daemon spawns processes to handle PHP requests. This separates the PHP overhead from the web-server threads. Usually, the php-fpm daemon requires fewer resources to handle PHP requests than the alternate mechanisms.

Installing and Configuring php-fpm

On Ubuntu/Debian the php-fpm module is installed by the php-fpm package. It will be dependent on the appropriate package appropriate for the current release. The PHP version may change when a new release is installed.

The php-fpm daemon has its own configuration tree within the /etc/php directory, for example /etc/php/8.1/fpm. There is a separate PHP configuration tree for PHP configuration. The daemon is configured using the pool.d/php-www.conf file in this directory tree. This allows you to tune the server pool. This is done with the pm parameters. The default configuration is for a fairly large dynamic pool with servers always running. For a small test server, I use these on-demand parameters.


pm = ondemand
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3

Once you have the configuration done, restart the php-fpm service. If you don’t the service will continue running with the default settings.

Enabling php-fpm in Apache2

One the php-fpm daemon is installed, configured, and running, Apache can be configured to use php-fpm instead of the default php module. This is simply done by disabling one module and enabling the other within the configuration.

sudo a2dismod php8.1
sudo a2enconf php-fpm8.1
sudo apache2ctl configtest
sudo systemctl restart apache2.service
sudo systemctl status apache2.service

Switching to mpm-event

To switch to the Event MPM, the Worker (or Prefork) MPM needs to disabled first. Then the event MPM can be enabled. I use an mpm-overrides.conf file in the /etc/apache2/conf-available directory once this is used the a2enconf command can be used to enable it. This file can contain configuration for multiple MPMs and will survive release upgrades. On one server the MPM overides configuration contains the following settings:

<IfModule mpm_event_module>
        StartServers             1
        ServerLimit              8
        MinSpareThreads          2
        MaxSpareThreads          16
        ThreadLimit              50
        ThreadsPerChild          8
        MaxRequestWorkers        64
        MaxConnectionsPerChild   1000
</IfModule>

Once this file is ready I am ready to finish the configuration. These are the commands I use.

sudo a2dismod mpm-worker
sudo a2enmod mpm-event
sudo a2enmod http2
sudo a2enconf mpm-overrides
sudo apache2ctl configtest
sudo systemctl restart apache2.service
sudo systemctl status apache2.service

Once this is completed apache2 should be converted to the Event MPM and be able to fully process http/2 requests.

Leave a comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Cookie Consent with Real Cookie Banner