Prerequisites
- Apache version greater or equal to 2.4.26.
- 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.
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.