Enable HTTP/2 on an Apache Virtual Host & Ubuntu 22.04

Posted
Comments 0

Enable HTTP/2 on an Apache Virtual Host & Ubuntu 22.04

Today, I’ll show you how to enable the new HTTP/2 protocol on an Apache server running Ubuntu 22.04, although this will work for any server running Apache version 2.4.26 or higher. HTTP/2 delivers web content in binary format as opposed to HTTP/1.1 that uses ASCI text. If you want to take a deep dive into the HTTP/2 protocol, I have given some links at the end of this guide.

This article is part of my series The Ultimate Web Server.

Key Points on HTTP/2
  • Headers are compressed, reducing load time
  • Multiplexing allows for multiple requests over a single TCP connection
  • Considered more mobile-friendly
  • Improved security as HTTP/2 is only delivered over a HTTPS connection

Contents

Prerequisites

Check Apache Version

To see what version of Apache web server you’re using, login to your web server as your non-root user and use the following command:

apache2 -v

If your version of Apache is 2.4.26 or higher, you’re good to go.

Enable HTTP/2 on Apache

Let’s enable the Apache HTTP/2 module:

sudo a2enmod http2

If you’ve been following my guide The Ultimate Web Server then you’ll already have an Apache Virtual Host file for your website. If not, replace the file name below with default-ssl.conf.

Open your website’s configuration file (replace ricbre.com with your domain name):

sudo nano /etc/apache2/sites-enabled/ricbre.com-le-ssl.conf

Just below the <VirtualHost *:443> tag, type or copy/paste the following code:

Protocols h2 http/1.1
Apache Virtual Host conf file

Then save ctrl + o, ENTER and exit nano ctrl + x.

To apply the changes, restart Apache web server:

sudo systemctl restart apache2

Configure PHP Modules

We need to find what PHP version you’re running. Enter the following and take note of the result (mine is version 8.1):

php -v

Now we install the php-fpm module:

sudo apt-get install php-fpm

We need to disable mod_php, so enter the following but replace 8.1 with your PHP version:

sudo a2dismod php8.1

Enable the PHP-FPM mod but again, replace 8.1 with your version:

sudo a2enconf php8.1-fpm

Then we enable the proxy_fcgi mod:

sudo a2enmod proxy_fcgi

Enable MPM EVENT

By default, Apache will use the mpm_prefork which is not compatible with HTTP/2, so we need to replace it with the newer mpm_event module:

sudo a2dismod mpm_prefork
sudo a2enmod mpm_event

Then we restart Apache:

sudo systemctl restart apache2

Test if HTTP/2 is Enabled

Let’s check if HTTP/2 was successfully enabled by using the curl command (replace ricbre.com with your domain name):

curl -I --http2 -s https://www.ricbre.com/ | grep HTTP
HTTP/2 OK Status

If you see status HTTP/2 200 then your server is fully configured and working with the HTTP/2 protocol.

Conclusion

Hopefully everything went well and your LAMP server is running on HTTP/2 protocol. If you have any questions or need help, hit me up on socials, email or comments.

See Also

Further Reading

Author
Categories Ubuntu

Comments

There are currently no comments on this article.

Comment

Enter your comment below. Fields marked * are required. You must preview your comment before submitting it.