Enable HTTP/2 with Apache in Ubuntu 2023

ubuntu

Prerequisites

  1. Apache version greater or equal to 2.4.26.
  2. SSL installed and working.Also, read Setup GeoIP Block using Apache 2023

Make sure you have the above 2 requirements in place to have HTTP/2 working.

Check the Apache version using this command.

apache2 -v

In Ubuntu 20.04 you will get an output similar to the one below.

Server version: Apache/2.4.41 (Ubuntu)
Server built: 2021-02-08T08:16:15

Enable HTTP/2 Apache Module

You can enable HTTP/2 module using the a2enmod command.

sudo a2enmod http2

Configure Apache virtual host to use HTTP/2.

Edit your HTTP virtual host config and the Protocol directive.

sudo nano /etc/apache2/sites-available/ssl.conf

Repace ssl.conf with your file name of yours.

Add the following below <VirtualHost *:443>

Protocols h2 http/1.1

Your configuration should look like below.

<VirtualHost *:443>
     Protocols h2 http/1.1
...

Hit CTRL+X followed by Y and ENTER to save and close the file.

Restart Apache for the changes to take effect.

sudo service apache2 restart

Configure Apache to use HTTP/2 for PHP

By default, Apache uses mod_php with MPM. HTTP/2 does not support prefork module. So you need to use Event MPM which is not compatible with mod_php. So you need to configure PHP-FPM.

Disable PHP module.

sudo a2dismod php8.0

Disable prefork MPM module.

sudo a2dismod mpm_prefork

Enable Event MPM, Fast_CGI and setenvif module.

sudo a2enmod mpm_event proxy_fcgi setenvif

Install PHP-FPM.

sudo apt install php8.0-fpm

Start PHP-FPM.

sudo systemctl start php8.0-fpm

Enable PHP-FPM config in Apache.

sudo a2enconf php8.0-fpm

Restart Apache for the changes to take effect.

sudo systemctl restart apache2

Now HTTP/2 should be enabled on your server.

Test configuration

If you open console in your inspect element and load your website you will see the protocol as h2 which confirms HTTP/2 is running.

http 2

That’s it. Now you have HTTP/2 enabled with Apache.

Conclusion

Now you have learned how to enable HTTP2 with Apache on Ubuntu 20.04.

AmritMatti

I’m the owner of “DevOpsTechy.online” and been in the industry for almost 5 years. What I’ve noticed particularly about the industry is that it reacts slowly to the rapidly changing world of technology. I’ve done my best to introduce new technology into the community with the hopes that more technology can be utilized to serve our customers. I’m going to educate and at times demonstrate that technology can help businesses innovate and thrive. Throwing in a little bit of fun and entertainment couldn’t hurt right?

AmritMatti

I’m the owner of “DevOpsTechy.online” and been in the industry for almost 5 years. What I’ve noticed particularly about the industry is that it reacts slowly to the rapidly changing world of technology. I’ve done my best to introduce new technology into the community with the hopes that more technology can be utilized to serve our customers. I’m going to educate and at times demonstrate that technology can help businesses innovate and thrive. Throwing in a little bit of fun and entertainment couldn’t hurt right?

View all posts by AmritMatti →

Leave a Reply

Your email address will not be published. Required fields are marked *