How to enable HTTP2 in Apache

  1. Why should you move to HTTP2
  2. How to enable HTTP2 support in Apache

Why you should move to HTTP2

Because HTTP 1.1 was drafted 18 years ago. No further comments needed 🙂 the web evolved, so the HTTP protocol needs to evolve to keep up with security advancements, speed and efficiency. And so it does the improvements of the protocol are HUGE. Just to name a few:

  • It supports multiple requests at the same time(Multiplexing), on HTTP 1.1 requests were blocking, meaning a transfer have to wait for other transfer to complete in order to start.
  • Another massive improvement is the ability to send resources to the client before he requested them(Server Push) so the user doesn’t have to wait the load time when he needs the resource.
  • One connection to the server is used to load all resources from a website instead of multiple TCP connections.
  • Header delivery is optimized by using HPACK compression to reduce overhead

Keep in mind however that most used browsers only support HTTP2 over SSL, So go ahead and setup that Let’s encrypt certificate first!

How to enable HTTP2 support in Apache

If you have Apache 2.4.* enabling HTTP2 will be very easy. However if you don’t you have to update your web-server first. How to update your web-server on Ubundu/Debian/Mint:

#Utilities which will help adding remote repositories
apt-get install software-properties-common python-software-properties
#Adding the ondrej/apache2remote repository
add-apt-repository ppa:ondrej/apache2
#Updating the sources
apt-get update
#Install the latest Apache2
apt-get install apache2
apachectl -v

After the above commands you will now be proud owner of a server running Apache 2.4.29 or above. Now you have to enable the HTTP2 module using the following command:

 a2enmod http2

Now in order to take advantage of the HTTP2 upgrade you have to add “Protocols h2 http/1.1” to the needed vhosts. They should look similarly to this one:

<VirtualHost *:443>
    Protocols h2 http/1.1
    ServerName codingstories.net
    ServerAlias www.codingstories.net
    ...
</VirtualHost>

After the modifications are done, reboot/reload Apache using “sudo service apache2 restart” and enjoy the boost!

About Pavel Petrov 2 Articles |  21 How-tos
Pavel is a senior developer for the last 7 years, with extended interest in Linux administration, WordPress and Symfony.

Be the first to comment

Leave a Reply

Your email address will not be published.


*