HTTP 2 on Ubuntu 18.04 with Apache and PHP

I recently got h2 (HTTP 2.0) running on my server. The new protocol should speed up page loads on my website. I’ve been wanting to get H2 going for a while, but was reluctant to use a third party repo on Ubuntu 16.04. That was one of the reasons I upgraded to 18.04 recently, and I’m glad I’m finally taking advantage of it.

I couldn’t find detailed instructions of what all needed to be done on 18.04 with Apache and PHP to make this happen, but pieced it together from various sites, such as this one with ‘php-fpm’ and this with ‘mpm_event’. I guess neither ‘mpm_prefork’ nor ‘mod_php’ work with h2, and have to be replaced with alternatives.

The overview of what I needed to do was:

  • Replace ‘mpm_prefork’ with ‘mpm_event’
  • Enable ‘mod_http2’
  • Specify h2 in the Protocols Apache directive
  • Replace ‘mod_php’ with ‘php-fpm’ / ‘proxy_fcgi’

I use Ansible to configure my server, and you can see the relevant configuration changes in this commit. Via command line, that would look something like:

sudo su
apt remove libapache2-mod-php  ## assuming it is installed
apt install php-fpm
a2dismod mpm_prefork php7.2
a2enmod http2 mpm_event proxy_fcgi
a2enconf php7.2-fpm
echo 'Protocols h2 h2c http/1.1' >> /etc/apache2/apache2.conf
service apache2 stop && service apache2 start  ## `graceful` / `restart` seem to fail when changing mpm

It took a little while to figure out, but each change didn’t take long to apply. It is running nicely now.