HTTP / 2 configuration using Apache 2.4, PHP 7 and Ubuntu 18.04 LTS as an example

I understand that, perhaps, Apache is not currently the preferred choice for launching new projects on it, nevertheless, it exists, and projects are working on it. Choosing it may fall for some personal preference, for compatibility requirements, or some other considerations ... not the point. In this article I want to describe in points how to configure support for the HTTP / 2 protocol on the Apache web server, because I use it myself and need such an article I need , and I hope that it will also come in handy for someone in practice.

What is HTTP / 2? As the name implies, this is the second version of the HTTP protocol. More information about the benefits you can read at least on Wikipedia . From myself I can only say that if you are hosted not on shared hosting, then you should support this protocol, like, for example, HTTPS. Yes, in order for HTTP / 2 to work for you, you will need full access to the machine console (albeit virtual) via ssh or in some other way, as well as the already configured HTTPS (TLS / SSL). Well, let's get down to business.

Step one. Apache update

The HTTP / 2 protocol is supported by the Apache server since version 2.4.24, so if you have an older version installed, it's time to update it. But first, check:

apache -v

This command will produce something like this:

Server version: Apache/2.4.37 (Ubuntu)
Server built:   2018-10-28T15:27:08

The first line indicates the server version. If it is greater than or equal to 2.4.24, then we can safely move on to the second step. Otherwise, you need to update Apache, the latest versions of which you can find in the PPA of a famous (if you already had to install something fresh on Debian / Ubuntu) developer - Ondřej Surý. PPAs are personal packages that are not included in the official distribution repositories. Therefore, in order to use them, you first need to teach the system what to get from and where:

sudo add-apt-repository ppa:ondrej/apache2

The guy does a good job, and do not mind getting a small donate, which he very unequivocally states when connecting to his turnips ...

Then, in fact, we update the packages:

sudo apt update
sudo apt upgrade

And again, check the version of Apache. If everything went well, Apache will be installed on your server, where you can configure HTTP / 2. If everything went bad - I'm afraid this is not the topic of this article. Now go to the second step.

Step two. Using FastCGI

What is FastCGI is better to read, as I said, at least in Wikipedia , because you can not tell it shortly. You need to switch to PHP running in FastCGI mode (php-fpm). How it works - a good answer is given on the Toaster . Here I will not go into such nuances, but proceed directly to the installation:

sudo apt install php-fpm

After installation, we will literally be told the following: to enable PHP 7.2 FPM in Apache2, do:

sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php7.2-fpm

We do. After that, you need to disable mod_php, because instead of it you now have php-fpm.

sudo a2dismod php7.2

Generally speaking, the PHP version before you could have been different. Look for the modules you have installed in the system can be in the directory / etc / apache2 / mods-available / , and which are active in / etc / apache2 / mods-enabled

Next, restart Apache

sudo service apache2 restart

and move on to the third step.

Step three. Switch from Prefork to Event

What is MPM and what is the difference between prefork, event, worker - you can read here in this wonderful article ... but now it is really important to know only one thing: the “standard” prefork is not very compatible with HTTP / 2, therefore you need to use more suitable . Turn off one, turn on the other, restart Apache.

sudo a2dismod mpm_prefork
sudo a2enmod mpm_event
service apache2 restart

Step Four. Turn on HTTP / 2

Either in the configuration file of the host, which can be found in the / etc / apache2 / sites-enabled / directory , or in the config of the server /etc/apache2/apache2.conf itself, we say that we need support for the new protocol:
Protocols h2 h2c http/1.1

What is really important is h2 . Two other points - at your discretion. h2c is HTTP / 2 support over TCP (and not TLS). http / 1.1 - support for the old version of HTTP.

We enable the http2 module and reboot the server:

sudo a2enmod http2
service apache2 restart


From now on everything. Your resource should start working under the HTTP / 2 protocol. If you use Chrome, go to the resource, you will see how the lightning icon in the upper right corner of the browser is lit up in blue. Hovering over it with a cursor, you will see the HTTP / 2-enabled (h2) hint. In Firefox, go to the developer panel and on the Network tab, enable the Protocol column — for the resources from your site should be HTTP / 2.0. You can also check if your resource supports this protocol on one of the many sites on the web. But you shouldn't relax, because HTTP / 3 is coming on the heels :)

Also popular now: