Linux

How to Enable HTTP/2 with Apache in Ubuntu

How to configure or enable or setup HTTP/2 with Apache in Ubuntu. HTTP/2 is a protocol developed to reduce latency, minimize protocol overhead and many more efficient features compared to HTTP/1.

In this guide you are going to learn how to enable HTTP/2 with Apache. This setup is tested on a Google cloud compute engine instance with Ubuntu 20.20 OS and Apache 2.4.41.

Choose Best Hosting for your Business

PlatformReviewsPricing
Siteground★★★★★$3.95
Kinsta – Google Cloud★★★★★$30

Prerequisites

  1. Apache version greater or equal to 2.4.26.
  2. SSL installed and working. Learn how to configure free SSL using Let’sEncrypt.

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

Check 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 the 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.

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.

Thanks for your time. If you face any problem or any feedback, please leave a comment below.

Cloudbooklet builds a large collection of Linux based guides and tutorials on Cloud platforms like Google Cloud, AWS, Azure, DigitalOcean and more

Write A Comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

If you find this helpful? Support me!
Buy me a coffee Donation Please buy me a coffee