How to Enable HTTP/2 on an Apache Web Server?

1 Star2 Stars3 Stars4 Stars5 Stars (14 votes, average: 5.00 out of 5)
Loading...
Enable HTTP 2 in Apache

Apache HTTP Server is a strong web server and has a large user base, which has a potential for improvement by enabling HTTP/2. HTTP/2 is an enhancement on HTTP/1.1 and the relativity of the two protocols is based on the loopholes that HTTP/1.1 has in its operations for web communications.

Also Read: HTTP 1 Vs. HTTP 1.1 Vs. HTTP 2: A Detailed Analysis

As you learn in this article, HTTP/2 on an Apache server is a little complex if you are running your server in an unmanaged environment or if you are using interfaces apart from cPanel or WHM.

These steps include how to enable HTTP/2 on Apache, how to test it once enabled, how to prepare your server for HTTP/2 and how to prepare for HTTP/3 in order to keep your website running fast.

How to Enable HTTP/2 on an Apache Server?

Check Apache Version

You need to determine what version of Apache you are operating, since not all of them support DVD. In fact, HTTP/2 was first introduced in Apache version 2.4.17. If your build is earlier than 2.4.17 then you are going to need to upgrade if you would like HTTP/2 capabilities.

To find the Apache version:

  • SSH into your server: Use your preferred SSH client to log in to a server as the root user. If you are using a Unix-like operating system, this can be done through Terminal; if you are on Windows, an application like PuTTY can be used.
ssh root@your_server_ip
  • Run your version checking command:
apachectl -v

The previous command will provide output that includes information about the version of Apache and a little more.  There will be a line in this output that looks something like ‘Server version:’ You want to make sure the number shown is 2.4.17 or above; if not, you need to upgrade Apache.

Ensure SSL/TLS is Enabled

HTTP/2 is an encrypted protocol and requires SSL/TLS. Therefore, if you want to implement HTTP/2, you will have to install a certificate on your server. SSL/TLS will provide encryption of the traffic between a client and the server and be sure that when the data leaves a server and goes to the client, it is completely private. 

Steps to ensure SSL/TLS is initiated:

  • Get an SSL/TLS certificate: You can buy an SSL/TLS certificate from a trusted Certificate Authority (CA) or use a non-domain tool such as Certbot for obtaining Let’s Encrypt certificates.
  • If you are using Ubuntu 18.04 or other similar distributions, then there is the possibility of using PIP (Python’s package installer) to install Certbot if snap does not work.
  • Install Certbot with PIP:
sudo apt-get install python3-pip
sudo pip3 install certbot

This installs Certbot and you can use it to obtain and manage your SSL certificates.

  • Configure SSL/TLS in Apache: You need to make sure that your Apache configuration has the right directives for SSL/TLS. Configuring Apache for SSL/TLS typically entails telling the Apache Configuration file of the paths to your SSL certificate and Private Key:

For example, the configuration in the virtual host configuration file may be:

<VirtualHost *:443>
            ServerName yourdomain.com
            SSLEngine on
            SSLCertificateFile /etc/ssl/certs/your_certificate.crt
            SSLCertificateKeyFile /etc/ssl/private/your_private_key.key
            …
</VirtualHost>

Enable the HTTP/2 Module

With the Apache version confirmed and SSL/TLS configured, the next step is to enable the HTTP/2 module. This module allows Apache to use the HTTP/2 protocol.

To enable the HTTP/2 module:

  • Run the command to enable HTTP/2: a2enmod http2

This command activates the HTTP/2 module, preparing Apache to support the new protocol.

Also Read: How to Enable HTTP/2 on IIS?

Configure Apache Virtual Hosts for HTTP/2

After enabling the HTTP/2 module, you need to update your virtual host configurations to use HTTP/2. This involves editing the Apache configuration files to include the necessary directives.

Steps to configure virtual hosts:

  • Locate your virtual host configuration file: For SSL-enabled sites, the configuration file typically ends with -le-ssl.conf or a similar suffix. Open this file using a text editor.
  • For example, with Nano:
sudo nano /etc/apache2/sites-enabled/000-default-le-ssl.conf
  • Replace 000-default-le-ssl.conf with the actual filename of your configuration file.
  • Add the HTTP/2 directive: Within the <VirtualHost *:443> block (which handles HTTPS traffic), add the following line to enable HTTP/2:
Protocols h2 http/1.1

This directive tells Apache to use HTTP/2 (h2) and fall back to HTTP/1.1 if necessary. Save your changes and exit the text editor.

Restart Apache

To apply the configuration changes, you need to restart Apache.

To restart Apache: Run the restart command:

apachectl restart

This command will reload Apache with the updated configuration, enabling HTTP/2.

How to Verify HTTP/2 is Being Used?

Using Google Chrome Browser

Google Chrome provides built-in tools to inspect HTTP/2 usage.

  • Open the website in Chrome: Go to the location where you enabled HTTP/2, for instance, go to the website you own or the website of the host you are managing.
  • Open Chrome DevTools: To do so, right click on the page and click on “Inspect,” or use the keyboard shortcut of Ctrl+Shift+I in Windows/Linux or Cmd+Opt+I in Mac to open the Chrome DevTools.
  • Go to the Network tab: Tab over to the “Network” option within the DevTools pane of the browser’s window.
  • Check the Protocol column: If you do not find the “Protocol” column, right click somewhere at the top, for instance on the ‘A’ header and then option the “Protocol”.

The Protocol field will contain the protocol that the real module used to complete each request. In order to verify that HTTP/2 is enabled, search for entries marked with “h2”.

Using Mozilla Firefox Browser

Firefox offers an add-on for checking HTTP/2 status.

  • Install the HTTP/2 Indicator add-on: To use HTTP/2 indicators you have to go to the Firefox Add-ons website and download HTTP/2 Indicator extension.
  • Visit your website: Click on the ‘File’ tab and select ‘Open Website’ from the drop down list and click Firefox.
  • Check for the HTTP/2 icon: The icon that will be presented in the address bar is the lightning bolt if HTTP/2 is on. Move the mouse cursor over this icon to receive a tooltip message about the server supporting HTTP/2.

Using Command Line Interface (CLI)

KeyCDN offers a tool to quickly check HTTP/2 status.

Steps to verify using KeyCDN:

Run the curl command:

curl --http2 domain.com -I

Replace domain.com with your actual domain name. The response should start with HTTP/2 200, indicating that HTTP/2 is being used.

Using KeyCDN HTTP/2 Test Tool

KeyCDN offers a tool to quickly check HTTP/2 status.

Steps to verify using KeyCDN:

  • Visit the KeyCDN HTTP/2 Test Tool: Open KeyCDN HTTP/2 Test Tool website.
  • Enter your URL: Put your website’s URL in the box that is provided.
  • Check the results: It will show whether this is for your site or not: HTTP/2.

Optimize for HTTP/2

Remove Inline CSS and JS

With HTTP/2’s ability to handle multiple simultaneous requests efficiently, the need for inline CSS and JavaScript is reduced. Inline CSS and JS were previously used to minimize requests, but with HTTP/2, you can now separate these files.

Action Steps:

  • Move inline styles and scripts: Refactor your HTML to include external CSS and JavaScript files rather than embedding them directly in the HTML. This will make your code cleaner and easier to maintain.

Eliminate Image Spriting

Image spriting, which involves combining multiple images into a single file, was used to reduce HTTP requests in HTTP/1.1. HTTP/2 can handle multiple image requests simultaneously, making spriting less necessary.

Action Steps:

  • Use individual image files: Serve images as separate files rather than combining them into sprites. This simplifies your image management and can improve load times with HTTP/2.

Avoid Concatenating CSS and JS Files

Combining multiple CSS and JavaScript files into single files. Reducing the number of HTTP requests. You may no longer need to concatenate because you can take advantage of parallel requests.

Action Steps:

  • Serve separate files: Maintain individual CSS and JavaScript files and link to them in your HTML. HTTP/2 will handle these requests more efficiently.

Stop Using Domain Sharding

Domain sharding involves using multiple domains to serve static files to overcome HTTP/1.1 limitations. HTTP/2 allows for multiple parallel requests over a single connection, making domain sharding unnecessary.

Action Steps:

  • Use a single domain: Serve static files from a single domain. This simplifies your setup and can reduce complexity.

Stay Ahead with HTTP/3

In the future, the HTTP protocol will be HTTP/3 over the QUIC transport layer protocol to ensure providing even better performance and even lower latencies compared to HTTP/2.

Browser Support

Major browsers, including Firefox, Chrome, and Edge, can already run over HTTP/3. Safari does too, with some minor required configuration.

Apache and HTTP/3

The work on HTTP/3 support within Apache is under development for the next release. Stay tuned and start planning on how you will activate HTTP/3.

Preparing for HTTP/3

To prepare for HTTP/3, monitor Apache updates and experiment with HTTP/3 implementations as they become available. Ensuring that your server can smoothly transition to HTTP/3 will help you stay at the forefront of web performance.

Conclusion

May it be a small blog or an e-commerce website with thousands of sales, even with the big corporate portal, CheapSSLWEB has something to offer for every SSL need. Our certificates are fully installable; they feature strong encryption, and top-of-the-line customer support is there to guide you step by step.

Janki Mehta

Janki Mehta

Janki Mehta is a Cyber-Security Enthusiast having 7+ years of experience and knowledge about Encryption, Digital Certificates and Online Security, She helps online users to stay safe and protect their online presence. Explore SSL Errors, Installation Guide and Security Tutorials for Safe Browsing and Web Security Experience.