HTTP/2 is the latest version of the Hypertext Transfer Protocol, designed to improve website loading speed and overall performance. With multiplexed streams, header compression, and server push features, HTTP/2 can significantly enhance user experience. In this guide, we’ll walk you through the steps to enable HTTP/2 on your Linux server, covering installation and configuration for both Apache and Nginx.
Prerequisites
Before you start, ensure you have the following:
- Linux Server: A running instance of a Linux distribution (Ubuntu, CentOS, etc.).
- Root Access: You need root privileges to install and configure packages.
- Web Server: Either Apache or Nginx installed and configured for your website.
- SSL Certificate: HTTP/2 requires HTTPS. You can obtain a free SSL certificate from Let’s Encrypt.
Step 1: Update Your System
Before making any changes, it’s crucial to update your package manager:
sudo apt update && sudo apt upgrade -y # For Debian/Ubuntu
sudo yum update -y # For CentOS
Step 2: Install/Verify SSL Certificates
Your web server needs a valid SSL certificate. For simplicity, you can use Certbot to obtain a free SSL certificate:
# Install Certbot
sudo apt install certbot python3-certbot-apache -y # For Apache
sudo apt install certbot python3-certbot-nginx -y # For Nginx
# Obtain a certificate (replace example.com with your domain)
sudo certbot --apache -d example.com # For Apache
sudo certbot --nginx -d example.com # For Nginx
Follow the prompts to confirm your email and agree to the terms of service.
Step 3: Enable HTTP/2 on Apache
3.1 Check Your Apache Version
Ensure that your Apache version is 2.4.17 or later, as HTTP/2 support is included from this version onwards:
apache2 -v # For Debian/Ubuntu
httpd -v # For CentOS
3.2 Enable the mod_http2
Module
Run the following commands to enable HTTP/2 support:
sudo a2enmod http2
3.3 Configure HTTP/2 in Your Apache Config
Open your Apache configuration file or the specific virtual host file (e.g., /etc/apache2/sites-available/example.conf
):
sudo nano /etc/apache2/sites-available/example.conf
Add the following line inside the <VirtualHost *:443>
block:
Protocol h2 http/1.1
After editing, your virtual host configuration should look like this:
<VirtualHost *:443>
ServerName example.com
DocumentRoot /var/www/html
# ... Other directives
Protocol h2 http/1.1
</VirtualHost>
3.4 Restart Apache
To apply your changes:
sudo systemctl restart apache2
Step 4: Enable HTTP/2 on Nginx
4.1 Check Your Nginx Version
Ensure your Nginx version is 1.13.0 or later:
nginx -v
4.2 Configure HTTP/2 in Your Nginx Config
Open your Nginx configuration file or the specific server block file (e.g., /etc/nginx/sites-available/example
):
sudo nano /etc/nginx/sites-available/example
Locate the line that begins with listen 443
and modify it to include http2
:
server {
listen 443 ssl http2;
server_name example.com;
root /var/www/html;
# ... Other directives
}
4.3 Test Your Configuration
Before restarting Nginx, test your configuration for syntax errors:
sudo nginx -t
4.4 Restart Nginx
If there are no errors, restart Nginx to apply the changes:
sudo systemctl restart nginx
Step 5: Verify HTTP/2 is Working
To check if HTTP/2 is enabled, you can use a browser developer tool or an online service like tools.keycdn.com/http2-test.
Alternatively, use cURL:
curl -I -s --http2 https://example.com | grep HTTP
If successful, you should see HTTP/2
in the output.
Conclusion
Congratulations! You’ve successfully enabled HTTP/2 on your Linux server. This upgrade can enhance your website’s performance and provide a better user experience. Regularly monitor your server and application performance to reap the full benefits of HTTP/2.
For further reading and advanced configuration, check out the official documentation for Apache and Nginx.