Configuring HTTP/2 for WordPress on Ubuntu

I moved my website to Ubuntu just recently and big part of that was due to desire for HTTP/2. However, if you have a legacy site configuration, things might not be as straightforward as one would hope.

The first step, of course, is enabling HTTP/2 module:

a2enmod http2

Second step is adding HTTP/2 protocol definition to /etc/apache2/apache2.conf:

Protocols h2 h2c http/1.1
H2Direct on
H2ModernTLSOnly on

Followed by Apache’s restart:

systemctl restart apache2

In ideal world this would be it. But, despite Apache starting without error, a check via Developer Tools in browser showed I was still stuck with HTTP 1.1. Yep, as many other sites, mine used mpm_prefork module. Unfortunately, prefork is not compatible with HTTP/2 and PHP 7.2 (needed for WordPress) is not compatible with anything else.

The only additional package we need is PHP with FastCGI support:

apt-get install php7.2-fpm

Furthermore, we need some modules enabled and disabled:

a2dismod php7.2
a2dismod mpm_prefork
a2enmod mpm_event
a2enmod proxy_fcgi
a2enconf php7.2-fpm

Of course, addition to /etc/apache2/apache2.conf is needed too:

<Files "*.php">
   SetHandler "proxy:unix:/var/run/php/php7.2-fpm.sock|fcgi://localhost/"
</Files>

If you configured prefork same as me, you also need to remove it’s configuration. In my case StartServers, MinSpareServers, MaxSpareServers, MaxClients, and MaxRequestsPerChild settings had to go.

Of course, another Apache restart is upon us:

systemctl restart apache2

Congratulations! HTTP/2 should be working now.