How to Enable HTTP2.0 in Apache on Ubuntu?


HTTP/2.0 is the latest version of the HTTP protocol that offers significant performance improvements over its predecessor, HTTP/1.1. Enabling HTTP/2.0 on your Apache web server can enhance the speed and responsiveness of your website, resulting in a better user experience for your visitors. In this article, we will guide you through the steps to enable HTTP/2.0 on Apache on Ubuntu.

Step 1: Check Apache Version

Before enabling HTTP/2.0, it is essential to ensure that your Apache version is compatible with HTTP/2.0. To check your Apache version, run the following command in your terminal −

apache2 -v

The output will display your Apache version. If your Apache version is 2.4.17 or later, you can proceed to enable HTTP/2.0.

Step 2: Install SSL Certificate

HTTP/2.0 requires a secure SSL/TLS connection. Hence, you need to have an SSL certificate installed on your web server. If you don't have an SSL certificate installed, you can obtain a free Let's Encrypt SSL certificate by following the instructions in this article: How to Install Let's Encrypt SSL on Ubuntu.

Step 3: Enable HTTP/2.0

To enable HTTP/2.0 on your Apache server, follow these steps −

  • Install the required module −

sudo apt-get install libapache2-mod-http2
  • Edit the Apache configuration file −

sudo nano /etc/apache2/sites-available/your-site.conf
  • Add the following lines to your VirtualHost block −

Protocols h2 http/1.1
  • Save and close the file.

Restart Apache

sudo service apache2 restart

That's it! You have successfully enabled HTTP/2.0 on your Apache web server.

Step 4: Test HTTP/2.0

To verify that HTTP/2.0 is enabled on your server, you can use the HTTP/2.0 test tool at https://tools.keycdn.com/http2-test.

Enter your website URL and click on the Test HTTP/2.0 button. If the test shows that your site is using HTTP/2.0, congratulations! Your site is now faster and more secure.

While enabling HTTP/2.0 on your Apache web server, you should keep in mind that not all browsers support this protocol. Some older browsers may not support HTTP/2.0, and they will fall back to using HTTP/1.1. Therefore, it is recommended that you also enable HTTP/1.1 to ensure that your website is accessible to all visitors.

To enable both HTTP/2.0 and HTTP/1.1, you need to modify the VirtualHost block in your Apache configuration file as follows −

Protocols h2 http/1.1

Enabling HTTP/2.0 on your Apache server can also enhance the security of your website. HTTP/2.0 requires the use of SSL/TLS encryption, which provides an extra layer of security for your website. It is recommended that you use a strong SSL/TLS configuration to protect your website and your visitors' data.

In addition to enabling HTTP/2.0 on your Apache server, you can also optimize your website for HTTP/2.0. For example, you can reduce the number of HTTP requests by combining multiple resources into a single file, using server push to pre-load resources, and optimizing the size of your images and other media files.

Another important consideration when enabling HTTP/2.0 on your Apache server is the server load. HTTP/2.0 requires more resources than HTTP/1.1, which means that your server may experience a higher load when serving HTTP/2.0 requests. Therefore, it is recommended that you monitor your server's resource usage and adjust the configuration as needed.

You can use tools like Apache's built-in mod_status module to monitor your server's resource usage. This module provides real-time information on server activity, including the number of requests, the status of each request, and the server's CPU and memory usage.

To enable mod_status, you need to add the following lines to your Apache configuration file −

<Location /server-status>
   SetHandler server-status
   Require all granted
</Location>

After enabling mod_status, you can access the server status page by visiting http://your-server-ip/server-status in your web browser. The page will display a summary of the server's current activity, including the number of active connections, the number of requests per second, and the server's CPU and memory usage.

By monitoring your server's resource usage and adjusting your configuration as needed, you can ensure that your server can handle the increased load when serving HTTP/2.0 requests.

Another aspect to consider when enabling HTTP/2.0 on your Apache server is the use of virtual hosts. If you are hosting multiple websites on the same server, you can enable HTTP/2.0 for each virtual host separately.

To enable HTTP/2.0 for a specific virtual host, you need to add the following lines to the virtual host configuration file −

Protocols h2 http/1.1

For example, if you have two virtual hosts named "example.com" and "example.net", you can enable HTTP/2.0 for each virtual host as follows −

<VirtualHost *:443>
   ServerName example.com
   Protocols h2 http/1.1
   SSLCertificateFile /path/to/certificate.pem
   SSLCertificateKeyFile /path/to/private/key.pem
   ...
</VirtualHost>

<VirtualHost *:443>
   ServerName example.net
   Protocols h2 http/1.1
   SSLCertificateFile /path/to/certificate.pem
   SSLCertificateKeyFile /path/to/private/key.pem
   ...
</VirtualHost>

By enabling HTTP/2.0 for each virtual host separately, you can provide a faster and more responsive user experience for each website.

Finally, it is important to keep your Apache server up-to-date to ensure that you have the latest security patches and performance improvements. You can use the following command to update Apache and its modules −

sudo apt-get update
sudo apt-get upgrade

By regularly updating your Apache server, you can ensure that your website remains secure and performs at its best.

Another important aspect to consider when enabling HTTP/2.0 on your Apache server is the use of caching. HTTP/2.0 has built-in support for server push, which allows the server to push resources to the client without waiting for a request. However, server push may not be the best solution for all resources, especially for large files that are not needed on every page load.

To optimize the performance of your website with HTTP/2.0, you can use caching to reduce the number of requests and improve the speed of your website. Apache supports various caching mechanisms, including mod_cache and mod_cache_disk.

Mod_cache is an in-memory caching mechanism that stores frequently accessed content in memory. This can significantly reduce the server load and improve the response time for frequently accessed resources.

Mod_cache_disk, on the other hand, stores frequently accessed content on disk, which can improve the response time for resources that are not frequently accessed. This can also reduce the server load by reducing the number of requests to the server.

To enable caching on your Apache server, you need to add the following lines to the virtual host configuration file −

<IfModule mod_cache.c>
   CacheQuickHandler on
   CacheLock on
   CacheLockPath /tmp/mod_cache-lock
   CacheLockMaxAge 5
   CacheIgnoreCacheControl On
   CacheIgnoreNoLastMod On
   CacheIgnoreHeaders Set-Cookie
   CacheStorePrivate On
   CacheStoreNoStore On
</IfModule>

These lines enable caching and specify the cache lock path and maximum age. You can customize these settings to suit your website's needs.

In addition to enabling caching, you can also optimize the size of your resources by compressing them. Apache supports gzip compression, which can significantly reduce the size of your resources and improve the speed of your website.

To enable gzip compression on your Apache server, you need to add the following lines to the virtual host configuration file −

<IfModule mod_deflate.c>
   AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/javascript application/x-javascript
   DeflateCompressionLevel 9
   DeflateWindowSize 15
   DeflateBufferSize 8096
</IfModule>

These lines enable gzip compression for the specified content types and specify the compression level, window size, and buffer size. You can customize these settings to suit your website's needs.

Conclusion

Enabling HTTP/2.0 on your Apache web server can significantly improve the speed and performance of your website. By following the steps outlined in this article, you can easily enable HTTP/2.0 on your Ubuntu server and enjoy the benefits of this new protocol.

Updated on: 15-May-2023

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements