enable http2 in apache

How To Enable HTTP2 in Apache

Last updated on June 20th, 2024 at 09:18 am

Website administrators often face problem of speed and performance. Enabling HTTP2 on their web server is a simple way to speed up their site. Also, it is important to setup HTTP2 in Apache web server from a security point of view. HTTP/2 supports parallelism, header compression, binary data transfer, and also server push. It is way better than running your site on HTTP protocol. In this article, we will look at how to enable HTTP2 in Apache web server.

What is HTTP2?

HTTP2 is a major upgrade to the good old HTTP Protocol that was used to run most internet websites. It provides significant improvements in areas of speed and security. It is supported by all web browsers and servers. It is supported even by old browsers that support only HTTP protocol. It reduces site latency by simply changing the way data is transferred between client browsers and servers. So the user does not have to make any changes. In fact, it uses the same headers, data formats and status codes as in HTTP. That is why it works with all major web browsers and servers. It just changes the way data is transmitted over network. It is during connection establishment that the browser and server decide to communicate via HTTP2.

Why Enable HTTP2 in Apache?

There are several reasons why HTTP2 is better than HTTP. Here are some of the most prominent ones:

  1. Prioritization – HTTP2 allows developers to decide which page elements should be loaded first. For example, should visible elements such as text & images be loaded before background items like CSS files be loaded? By controlling item prioritization, developers can make their web pages load faster.
  2. Multiplexing – HTTP loads resources sequentially so if one resource takes a lot of time, all other resources are held up. HTTP2 sends multiple streams of data at once, so that web pages load faster.
  3. Header Compression – HTTP2 compresses request and response headers smartly, by eliminating redundant information, to improve performance and speed.
  4. Server Push – HTTP2 allows servers to push content before it is requested by client web browser, thereby saving a lot of time in the long run.
  5. Security – HTTP2 is more secure than HTTP since it is supported for SSL/TLS sites only.

How To Enable HTTP2 in Apache

Here are the steps to enable HTTP2 in Apache web server. Please note, HTTP/2 is supported for HTTPS/SSL websites only. So please enable SSL in Apache web server before proceeding.

1. Enable http2 module

On Ubuntu/Debian

Open terminal and run the following command to enable http2 module.

$ sudo a2enmod http2

On Redhat/CentOS/Fedora

Apache configuration file is located at one of the following locations, depending on your Linux distribution.

  • /etc/apache2/httpd.conf
  • /etc/apache2/apache2.conf
  • /etc/httpd/httpd.conf
  • /etc/httpd/conf/httpd.conf

Open terminal and run the following command to open Apache configuration page.

$ sudo vi /etc/httpd/conf/httpd.conf

Look for the following line

#LoadModule http2_module modules/mod_http2.so

Uncomment it by removing # at its beginning.

LoadModule http2_module modules/mod_http2.so

2. Add Protocols directive

Also add the following line to your Apache server configuration file. This needs to be done for both Ubuntu/Debian as well Redhat/CentOS linux.

Protocols h2 h2c http/1.1

Add the above line to Apache server configuration file. If you have setup virtual host in Apache, then open your virtual configuration file and add the above line in <VirtualHost *:443> tag.

3. Restart Apache Web Server

Restart Apache web server to apply changes.

# service httpd restart
OR 
# systemctl restart httpd
OR
# sudo service apache2 restart

4. Verify HTTP/2

Use a third party tools like KeyCDN HTTP2 checker that allow you to check if HTTP/2 is enabled on your website.

After you enter your website, with HTTPS URL, it will tell you if HTTP/2 is enabled in it or not.

As you can see, it is quite easy to enable HTTP/2 in Apache.

Conclusion

In this article, we have learnt what is HTTP2, why should enable it and how to enable it in Apache web server. HTTP2 is an essential upgrade for every website. If you do not enable it, your site will be unnecessarily slow. Also, it is important to note that HTTP3 is available and is also being adopted gradually across the web. Though it is not supported by all web browsers and servers, it is growing. So it is high time, you at least upgrade your site to use HTTP2.

Ubiq makes it easy to visualize data in minutes, and monitor in real-time dashboards. Try it today!

Also read :
How to Enable Apache Server Status dashboard
How to remove x-powered-by in Apache/PHP
How to Disable HTTP OPTIONS methods in Apache
How to Set Default Charset in Apache Server