How To Enable HTTP 2 On Apache Web Server

By bhagwatchouhan
How To Enable HTTP 2 On Apache Web Server

Hypertext Transfer Protocol Version 2 (HTTP/2) is the latest version of the HTTP protocol. It adds several new features (Binary Protocols, Multiplexing, Header Compression, Server Push, Security) as compared to the previous version of the HTTP protocol i.e. HTTP/1.1. The most important feature is the usage of binary format instead of plain text format i.e. it encapsulates all the messages in the binary format following the HTTP semantics including verbs, methods, and headers. This makes sure that the servers and clients can continue with the existing applications. Also, HTTP/2 makes a persistent connection to the server which allows clients to send multiple requests without waiting for the response. The server can push resources to the client since the connection remains open. The HTTP/2 protocol allows only encrypted connections which further increases the application security.

 

The Apache Web Server supports HTTP/2 protocol since version 2.4.17. The current version of Apache 2 on Ubuntu 18.04 LTS repositories is 2.4.29 and Ubuntu 20.04 LTS repositories is 2.4.41. This tutorial provides the steps to configure the Apache Web Server to use HTTP/2 protocol to send and receive the HTTP messages. This tutorial provides all the steps for Ubuntu 20.04 LTS. The steps should be similar on other versions of Ubuntu and Linux systems.

 

Prerequisites

 

This tutorial assumes that the virtual hosts are configured to allow only HTTPS requests. You can follow Configure Virtual Host On Apache, How To Install Let's Encrypt For Apache On Ubuntu, and Redirect HTTP to HTTPS on Apache to enable HTTPS only.

 

It also assumes that the Apache Web Server is configured to use TLS >= 1.2. You can follow How To Enable TLS 1.2 and TLS 1.3 in Apache Web Server. Your client or the browser should also support HTTP/2.

 

We can check the Apache and OpenSSL version as shown below.

 

# Apache Version
apache2 -version

# Output
Server version: Apache/2.4.41 (Ubuntu)
Server built: 2020-04-13T17:19:17

# OpenSSL Version
openssl version

# Output
OpenSSL 1.1.1f 31 Mar 2020

 

Switch To PHP-FPM

 

This section provides the steps to disable PHP module and enable PHP-FPM.

 

We can disable PHP Module and enable PHP-FPM on Ubuntu 18.04 LTS as show below.

 

# Install PHP-FPM
sudo apt-get install php7.2-fpm

# Disable PHP Module
sudo a2dismod php7.2

# Enable PHP-FPM
sudo a2enconf php7.2-fpm

# Enable FastCGI Module
sudo a2enmod proxy_fcgi

 

We can disable PHP Module and enable PHP-FPM on Ubuntu 20.04 LTS as show below.

 

# Install PHP-FPM
sudo apt-get install php7.4-fpm

# Disable PHP Module
sudo a2dismod php7.4

# Enable PHP-FPM
sudo a2enconf php7.4-fpm

# Enable FastCGI Module
sudo a2enmod proxy_fcgi

 

Disable Prefork MPM and Enable Event MPM

 

Now disable the Prefork MPM and enable Event MPM on the Apache Web Server as shown below.

 

# Disable Prefork MPM
sudo a2dismod mpm_prefork

# Output
Module mpm_prefork disabled.
To activate the new configuration, you need to run:
systemctl restart apache2

# Enable Event MPM
sudo a2enmod mpm_event

# Output
Considering conflict mpm_worker for mpm_event:
Considering conflict mpm_prefork for mpm_event:
Enabling module mpm_event.
To activate the new configuration, you need to run:
systemctl restart apache2

# Restart Apache
sudo systemctl restart apache2
# OR
sudo service apache2 restart

 

Now print the output of phpinfo(). It should show the PHP FPM as shown in Fig 1.

 

Enable HTTP 2 On Apache - PHP FPM

Fig 1

 

Now write a simple program and check the HTTP version by inspecting the page as shown in Fig 2.

 

Enable HTTP 2 On Apache - HTTP/1

Fig 2

 

Enable HTTP/2

 

This section provides the steps to enable HTTP/2 on Apache Web Server. We need to enable the SSL and HTTP2 modules as shown below.

 

# Enable SSL Module
sudo a2enmod ssl

# Enable HTTP2 Module
sudo a2enmod http2

# Restart Apache
sudo systemctl restart apache2
# OR
sudo service apache2 restart

 

Now update the virtual host as shown below.

 

# Update Virtual Host
sudo nano /etc/apache2/sites-available/example.com-le-ssl.conf

# Update
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName example.com
ServerAlias www.example.com
ServerAdmin admin@example.com

DocumentRoot /var/www/example.com/html
<Directory /var/www/example.com/html>
Options -Indexes +FollowSymLinks
DirectoryIndex index.html
AllowOverride All
Require all granted
</Directory>

SSLEngine on
SSLProtocol -all +TLSv1.2 +TLSv1.3
Protocols h2 http/1.1
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>

# Save and Exit - Ctrl + o -> Enter + Ctrl + x

# Reload Apache
sudo systemctl reload apache2
# OR
sudo service apache2 reload

 

Instead of specifying the SSL Protocol in virtual host, we can also update the SSL configuration of Apache for all the virtual hosts as shown in How To Enable TLS 1.2 and TLS 1.3 in Apache Web Server.

 

Now again open the demo page. It should reflect HTTP/2 as shown in Fig 3.

 

Enable HTTP 2 On Apache - HTTP/2

Fig 3

 

This is how we can enable the HTTP 2 protocol on Apache Web Server.

 

Summary

 

This tutorial provided the steps required to enable the HTTP 2 protocol on the Apache Web Server.

Share this blog:

Profile picture for user bhagwatchouhan
bhagwatchouhan