How to Enable HTTP/2 in Apache 2.4 on Ubuntu 20.04?
Last Updated :
05 Jul, 2024
Apache 2.4 with HTTP/2 enabled on Ubuntu 20.04 will greatly enhance your web server's performance. Compared to HTTP/1.1, HTTP/2 has several benefits, such as server push, header compression, and multiplexing.
Prerequisites
- Ubuntu 20.04
- Apache 2.4.17 or later (HTTP/2 functionality is available beginning with Apache 2.4.17).
- Your Apache server has to have SSL/TLS enabled (HTTP/2 requires HTTPS).
Steps to Enable HTTP/2 in Apache 2.4 on Ubuntu 20.04
Step 1: Update Your System
Before starting, it’s a good idea to ensure your system is up to date:
sudo apt update
sudo apt upgrade
Step 2: Install Apache 2.4
If you don't have Apache installed, you can install it using:
sudo apt install apache2
Step 3: Enable SSL Module and Install Certbot
HTTP/2 requires SSL/TLS, so you need to enable the SSL module and install Certbot to obtain an SSL certificate from Let’s Encrypt:
sudo a2enmod ssl
sudo apt install certbot python3-certbot-apache
Step 4: Obtain an SSL Certificate
Use Certbot to obtain an SSL certificate for your domain:
sudo certbot --apache
Step 5: Enable the HTTP/2 Module
Enable the HTTP/2 module in Apache:
sudo a2enmod http2
Step 6: Configure Apache to Use HTTP/2
Edit your Apache configuration to enable HTTP/2. Open the SSL configuration file:
sudo nano /etc/apache2/sites-available/your_domain.conf
Add the following lines inside the <VirtualHost *:443> block:
Protocols h2 http/1.1
Your configuration should look something like this:
<VirtualHost *:443>
ServerAdmin webmaster@your_domain
ServerName your_domain
DocumentRoot /var/www/your_domain
Protocols h2 http/1.1
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/your_domain/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/your_domain/privkey.pem
<Directory /var/www/your_domain>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
Step 7: Test Your Configuration
Check the configuration for any syntax errors:
sudo apache2ctl configtest
You should see Syntax OK. If there are errors, fix them before proceeding.
Step 8: Restart Apache
Restart Apache to apply the changes:
sudo systemctl restart apache2
Step 9: Verify HTTP/2 is Enabled
You can use online tools like KeyCDN HTTP/2 Test or HTTP/2 and SPDY Indicator for Chrome to verify that HTTP/2 is enabled on your server.
Alternatively, you can use curl to check:
curl -I -k --http2 https://your_domain
check for HTTP/2 in the output.
Troubleshooting
If HTTP/2 is not working as expected, check the following:
1. Apache Error Logs: Check the Apache error logs for any issues:
sudo tail -f /var/log/apache2/error.log
2. Module Status: Ensure that the HTTP/2 and SSL modules are loaded:
sudo apachectl -M | grep http2
sudo apachectl -M | grep ssl
3. Protocol Support: Ensure your browser supports HTTP/2 and that you are accessing the site via HTTPS.
Conclusion
By following these steps, you can enable HTTP/2 on your Apache 2.4 server running on Ubuntu 20.04. This will help improve your website's performance by increasing the advanced features of the HTTP/2 protocol. Remember to keep your server and packages updated to maintain security and performance.