How To Set Up Apache with HTTP/2 Support on Ubuntu 16.04


apache with http2 supportHTTP/2 is the first major HTTP protocol update since 1997. The main goal of HTTP/2 is to decrease latency, reduce total number of TCP connections thus improve page load speed in web browsers. HTTP/2 is backwards-compatible with HTTP/1.1, all application semantics of HTTP are the same, except the way of transmitting data via TCP connection. HTTP/2 leverages multiplexing and allows asynchronous (parallel) requests and provides data compression of HTTP headers. Also, the server push method in HTTP/2 allows server to send multiple responses for a single request.

HTTP/2 support is not included in Ubuntu 16.04, neither with nginx nor with Apache because this feature is considered as experimental by the Apache httpd upstream project, so it is not possible to enable it on an Ubuntu 14.04 VPS using the a2enmod command:

a2enmod http2
ERROR: Module http2 does not exist!

HTTP/2 support should be enabled in Ubuntu 16.04 after the Apache httpd upstream considers this support to be non-experimental.
Luckily, the Http2 module is included in the source, so follow these steps to enable HTTP/2 support on Apache web server:
Add source URIs to the sources.list file if it is not already done so:

vi /etc/apt/sources.list

add the following lines at the end:

deb-src http://archive.ubuntu.com/ubuntu/ xenial main universe restricted multiverse
deb-src http://security.ubuntu.com/ubuntu xenial-security main universe restricted multiverse
deb-src http://archive.ubuntu.com/ubuntu/ xenial-updates main universe restricted multiverse

Run ‘apt-get update’ command to download the package lists from the repositories and update them to get information on the newest versions of packages, then update the Ubuntu 16.04 OS packages using the ‘apt-get upgrade’ command:

apt-get update
apt-get upgrade

Install the required pre-requisites:

apt-get install curl devscripts build-essential fakeroot

Create a new user your VPS, or switch to an existing user account:

sudo adduser <username> sudo
su <username>

Replace <username> with an actual username.
Install the libnghttp2-dev package:

[user]$ cd ~
[user]$ sudo apt-get install libnghttp2-dev
[user]$ mkdir apache2
[user]$ cd apache2

Download the apache2 sources on your server and build them:

[user]$ apt-get source apache2
[user]$ sudo apt-get build-dep apache2
[user]$ cd apache-2.4.18
[user]$ sudo apt-get install curl devscripts build-essential fakeroot
[user]$ fakeroot debian/rules binary
[user]$ sudo cp debian/apache2-bin/usr/lib/apache2/modules/mod_http2.so /usr/lib/apache2/modules/
[user]$ exit

Load the HTTP/2 module. Create a file /etc/apache2/mods-available/http2.load and add the following lines:

LoadModule http2_module /usr/lib/apache2/modules/mod_http2.so

<IfModule http2_module>
LogLevel http2:info
</IfModule>

Enable the http2 module:

a2enmod http2

Add ‘Protocols h2 http/1.1’ to each SSL based virtual hosts. For example, edit the default-ssl configuration file:

vi /etc/apache2/sites-enabled/default-ssl.conf
<IfModule mod_ssl.c>
 <VirtualHost _default_:443>
 ServerAdmin webmaster@localhost
 ServerName your-domain.com
 Protocols h2 http/1.1
 DocumentRoot /var/www/html
 
 ErrorLog ${APACHE_LOG_DIR}/error.log
 CustomLog ${APACHE_LOG_DIR}/access.log combined
 
 SSLEngine on

 SSLCertificateFile /etc/ssl/certs/ssl-cert-snakeoil.pem
 SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key

 <FilesMatch "\.(cgi|shtml|phtml|php)$">
 SSLOptions +StdEnvVars
 </FilesMatch>
 <Directory /usr/lib/cgi-bin>
 SSLOptions +StdEnvVars
 </Directory>

 </VirtualHost>
</IfModule>

Or, add ‘Protocols h2 http/1.1’ to main Apache configuration to enable http2 on all websites:

vi /etc/apache2/apache2.conf

Add this line at the end:

Protocols h2 http/1.1

Restart Apache for the changes to take effect:

service apache2 restart

That is it. The HTTP/2 support has been enabled on your Apache web server.


Of course you don’t have to do any of this if you use one of our Linux VPS Hosting services, in which case you can simply ask our expert Linux admins to set up Apache with HTTP/2 support for you. They are available 24×7 and will take care of your request immediately.
PS. If you liked this post please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.

 

Tutorials
How to install and integrate SpamAssassin with Postfix on a CentOS 6 VPS
Tutorials
How to install Zenphoto on Ubuntu 14.04
Tutorials
Install an SSL certificate on a Linux VPS with the DirectAdmin control panel
  • Hayden

    HaydenHayden

    Author Reply

    Thank you sir. Works perfectly. I assume this needs to be re-done every apache update?


    • admin

      adminadmin

      Author Reply

      You shouldn’t implement the steps again after Apache update.