HTTP/2 with WordPress and Apache

Published by Tobias Hofmann on

6 min read

My web site contains great content however is not fully optimized for speed. From time to time, I have to deal with problems caused by the cache plugin. Therefore, caching is sometimes even deactivated. Serving content slow is still better than not serving content at all. Another performance hit is caused by my site not using HTTP/2 (or rather: wasn’t). In Chrome developer tools, the protocol served by the web server can be seen HTTP/1.1 is used.

Ein Bild, das Text enthält.

Automatisch generierte Beschreibung

Serving a web site via HTTP/1.1 isn’t as bad as it sounds. The web site will be delivered without problems. But Google is the problem here. They are using the protocol to decide if your page gets a higher ranking or not. Their tools even recommend using HTTP/2. For instance, using Google PageSpeed Insights for my site gives the following result.

Using HTTP/2 is given as one of the recommendations.

Pre-requisites

To be able to serve a web site with HTTP/2 and Apache, version 2.4.17 is needed. The module used is named mod_http2. To find out if the currently installed Apache matches that version requirement, run:

apachectl -v

As long as the version reported is at least 2.4.17, http/2 is supported.

Configuration

Base configuration for Apache 2 to activate HTTP/2 is to load the module and adjust the configuration to activate http/2. After Apache is ready the correct PHP engine must be activated. Then WordPress will be accessed via http/2.

Load module

The module library is available in the directory modules.

Ein Bild, das Text, Gerät, Anzeige enthält.

Automatisch generierte Beschreibung

To be able to use the http2 module, load and activate it in the Apache configuration. In my case, the config file is: 00-base.conf. Add or uncomment the line:

LoadModule http2_module modules/mod_http2.so

Maybe the module is already added to the list.

Activate protocol

After loading the http2 module, activate the protocol inside Apache. This is done best in the ssl configuration of Apache. In my case:

conf.d/0000-ssl.conf

Adding the following line activates HTTP2 as a protocol.

Protocols h2 h2c http/1.1

In the virtual host for your SSL site (or your general SSL configuration) you add the instruction to activate HTTP2.

<VirtualHost _default_:443>
  ServerName www.itsfullofstars.de
  Protocols h2 h2c http/1.1
  ProtocolsHonorOrder Off
Ein Bild, das Text enthält.

Automatisch generierte Beschreibung

PHP configuration

Activating HTTP2 module does not mean that your web site is now supporting H2. If H2 is used depends on the Apache prefork module. MPM Prefork is not compatible with H2 and must be replaced by e.g. mpm_event.

Install php-fpm

yum install php73-fpm
Ein Bild, das Text enthält.

Automatisch generierte Beschreibung

Check that PHP-FPM can start without errors.

/etc/init.d/php-fpm start

If it fails, check the error message. In the above case, the user nginx cannot retrieved. The server is running Apache2 and not NGINX, therefore no nginx user exists in the system.

grep nginx /etc/alternatives/php-fpm.d/*

Change the line to

listen.acl_users = apache

Starting php-fpm must work.

Activate MPM Event

Deactivate the mpm module. Comment out the MPM Prefork module

vim conf.modules.d/00-mpm.conf

Now activate MPM Event. With this, the PHP engine is changed.

Restart Apache

Result

This concludes the configuration of Apache to run WordPress and serve it via http/2. Open a browser and access the website and look at the protocol reported by the developer tools. If all worked fine, the Apache web server will now serve the website with h2.

Let the world know
Categories: Technology

Tobias Hofmann

Doing stuff with SAP since 1998. Open, web, UX, cloud. I am not a Basis guy, but very knowledgeable about Basis stuff, as it's the foundation of everything I do (DevOps). Performance is king, and unit tests is something I actually do. Developing HTML5 apps when HTML5 wasn't around. HCP/SCP user since 2012, NetWeaver since 2002, ABAP since 1998.

0 Comments

Leave a Reply

Avatar placeholder

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.