How to enable the HTTP/2 protocol on our Apache based web server

botond published 2022/12/20, k - 02:50 time

Content

 

Introductory

A HTTP / 2 protocol enables faster web service with less data traffic. Although the protocol created in 2015 has been widespread for quite a few years, it is not turned on by default in the majority of web servers due to compatibility reasons. Among other things, it does not support the already outdated mod_php Apache module, which still operates many old websites, so enabling and enabling it is left to system administrators. In this short description, we will see how we can enable the use of the HTTP/2 protocol on Apache web servers that still use the old HTTP/1.1 protocol.

 

 

basic Conditions

HTTP/2 support is Apache 2.4.17was included in version 2015 in October 9, which in the case of the Debian distribution was first available in Debian XNUMX (Stretch) version of Apache In version 2.4.25.

In this description, it is primarily ISPConfig, since some basic conditions are already met on these servers. In the case of the HTTP/2 protocol, the basic requirement is a HTTPS connection, therefore the HTTP/2 protocol On LAMP servers setting requires an intermediate step in which we set a SSL certificate, which can be a copy certified by a CA (Certificate Authority) or even a "home" generated (self-signed) certificate. Setting up SSL is not part of this description, we will review this in a separate guide (when this description is ready, I will link it here). However, if we have a LAMP server on which an SSL certificate is already configured, we can proceed with that as well.

There is also an "intermediate" h2c standard that builds on HTTP/1.1 via the HTTP/2 connection and does not require SSL encryption, but it is rarely used because it is not secure and not supported by all devices. So we won't deal with that here.

In accordance with the above, the following servers can serve as the basis for this description:

I am doing this tutorial on Debian 11 (Bullseye) v1.0.

 

Checking the existence of the HTTP/2 protocol

Before starting any configuration, it is advisable to first check whether HTTP/2 is really not already enabled in our Apache web server, because it may have been activated during the installation of a new Operating System or ISPConfig, the chances of which increase over time.

To check, open a website or any web interface that runs on the server. I use the Chrome browser for this, as well as ISPConfig and phpMyAdmin interfaces, as there are no other websites on this server.

When the interface is loaded, click anywhere on the page with the right mouse button, and a menu will appear. Here, select the bottommost "Test" menu item (in other browsers, of course, this menu item may be in other places). Then the web development tools will appear attached to the right edge of the screen or the bottom (depending on the setting). There is a horizontal menu bar here, click on the "Network" menu. Then a blank page appears, where it says to refresh the page with the Ctrl + R keys so that it can collect new data. So let's refresh the page, but we can initiate this simply by pressing the F5 key. You will then see an interface similar to the one below:

Check HTTP1.1 protocol

Check HTTP1.1 protocol

If you have not used this section yet, the "Protocol" column may not appear in the list by default. To do this, right-click in the header and check the Protocol column until it appears in the list.

The Protocol column shows us the HTTP protocol used by the website we are currently examining. We can see them here:

  • http/1.1: This is the old HTTP/1.1 protocol. If we see this next to our own resources (e.g. JS, CSS files), then it is necessary to execute this description
  • h2: This is the HTTP/2 protocol, we'll enable it if we don't already see it here.
  • h2c: The aforementioned h2c protocol standard, which does not require SSL encryption, is, however, rarely used.
  • h3: HTTP/3 protocol, this is still a very new standard (2022), for now Google, Facebook and possibly a few larger companies use them (therefore resources loaded from these sites can also be loaded with this protocol), and most browsers also support it already, that's why it can appear in this field, but we won't deal with it here.
  • chrome extension: Chrome extension, this loads differently.
  • date: Also a resource loaded in other ways (not with HTTP protocols), eg browser cache, etc.
  • etc. ...

If we see the majority of http/1.1 states here, then we can continue with the next chapter.

What I mean by "mostly" here is that since many other external resources can be loaded on a website, things loaded from other places can also come through other protocols. For example, a CSS or JS file or other resource loaded from another server can also be loaded with a different protocol than our own, so if you scroll down to the bottom, there is a good chance that you will see a mixture of several protocols. That's why we watch things loaded from our own server between the lines. E.g. own JS, CSS files, images, etc.

 

 

Enable the HTTP/2 protocol

There are currently three types of Apache web server MPM (Multi Processing Module) contains (prefork, worker, event), not all of which support the HTTP/2 protocol. Therefore, we first check which MPM is currently active in the system. We can do this with the following command:

apache2ctl -V | grep -i "mpm"

Prefork MPM check

By default, the "prefork" MPM is active, which is the most outdated of the three, and therefore does not support HTTP/2, as it can only receive and process requests on one thread at a time, while this is one of the great advantages of the HTTP/2 protocol , that it can connect on several threads at the same time, within a single identification frame (SSL handshake).

To switch the MPM module, you have to turn off other Apache module(s), as well as turn on others, and then you can switch to the most efficient MPM for the event.

First, let's enable a http2 Apache module. This may already be enabled in some Linux versions, but let's run its activation anyway. Hereinafter rootlet's move on as

To enable the http2 module, issue the following command.

a2enmod http2
Interestingly, if we look at the beginning of the http2 Apache module configuration file (/etc/apache/mods-available/http2.conf), it starts like this:
1
2
3
4
5
6
7
8
9
# mod_http2 doesn't work with mpm_prefork
<IfModule !mpm_prefork>
    Protocols h2 h2c http/1.1
 
    # # HTTP/2 push configuration
    #
    # H2Push          on
 
[...]

Here we can gain two useful pieces of information:

  • One is that the http2 module does not work together with the prefork MPM module (this is described as well as bound in the configuration block condition), so they are mutually exclusive.
  • The other is the Protocols line, in which the configuration determines the priority of the protocols. In this case, the first priority is to use h2, i.e. the HTTP/2 protocol. If the client program (web browser) does not support it, h2c, if it does not support it either, the "fallback" option is to use the traditional HTTP/1.1 standard. This is supported by everything that can connect to the web.
    This part is also important because this is the point of our Apache web server that determines whether the protocol is used, not just installed. Without it, the clients' own policies and settings dictate what they use. Thus, if HTTP/2 is not prioritized there, the connection will not be made on it.

Then if there is one running as a module PHP our instance, then we turn it off as well. We can do this with the module name corresponding to the PHP version. For PHP 7.4, for example:

a2dismod php7.4
PHP runs as a module if the libapache2-mod-php7.4 package (for example, for Debian 11's default PHP version 7.4) and we did not switch PHP-FPMto. If, on the other hand, we only installed PHP-FPM packages for the various PHP versions, then of course there is nothing to disable in this case.
But if we don't know which version we need to turn off here, that is apache2ctl -M command, we can query the list of activated modules, or we can go further, because during the activation and deactivation of the modules, the system also indicates that if some module collides with another and it should be switched off.
In light of the above, it is advisable to keep in mind that running PHP as a module and using the HTTP/2 protocol - as already mentioned above - are mutually exclusive. So if for compatibility reasons we need PHP to run as an Apache module, then don't implement this description.
Of course, this is not an irreversible operation, but we can restore our system to its previous state at any time.

In the next step, we turn off the prefork MPM module:

a2dismod mpm_prefork

Then turn on the event MPM module:

a2enmod mpm_event

Then enable the proxy_fcgi module:

a2enmod proxy_fcgi

If you are already using PHP-FPM, then this module is already enabled, because it needs it. So, depending on our situation, this module can already be turned on.

Finally, restart Apache:

systemctl restart apache2

So the whole thing looks like this:

Enable HTTP/2.0 protocol

Then we can check our MPM module again:

apache2ctl -V | grep -i "mpm"

Event MPM check

The event MPM module is already active here.

 

 

Checking the HTTP/2 protocol

If we have completed the settings, we check our websites or other web interfaces again in the already familiar way:

Check HTTP/2.0 protocol

Check HTTP/2.0 protocol

If we did everything right, the h2 marks will appear in the Protocol column.

Another way to check a website's HTTP protocol is to curl command use:

curl -I --http2 -s <webcím> | grep HTTP

Where the switches are:

  • -IN: It performs a HEAD request, so it only retrieves the header of the requested website.
  • --http2: Use HTTP/2 protocol
  • -s: Silent mode, does not display any traffic statistics.

Checking the HTTP/2 protocol using the curl command

The command returns the protocol and the response code received from the server.

 

 

Conclusion

The HTTP/2 protocol provides a more modern, efficient and faster web service than its predecessor, so it is worth switching to this standard if your system meets the requirements of HTTP/2. It is not complicated to set up, and you can save server and network resources during its use.