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

apache http2

We’ll show you, How To Set Up Apache with HTTP/2. HTTP/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:

1. Add source URIs

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

2. Download the package lists from the repositories

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

3. Install pre-requisites

Install the required pre-requisites:

apt-get install curl devscripts build-essential fakeroot

4. Create new user

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

sudo adduser <username> sudo
su <username>

Replace <username> with an actual username.

5. Install libnghttp2-dev

Install the libnghttp2-dev package:

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

6. Download the apache2 sources

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

7. Load the HTTP/2 module.

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>

8. Enable and configure Apache HTTP2 module

Enable the http2 module:

Need a fast and easy fix?
✔ Unlimited Managed Support
✔ Supports Your Software
✔ 2 CPU Cores
✔ 2 GB RAM
✔ 50 GB PCIe4 NVMe Disk
✔ 1854 GeekBench Score
✔ Unmetered Data Transfer
NVME 2 VPS

Now just $43 .99
/mo

GET YOUR VPS
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

9. Restart Apache web server

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.


Apache HTTP/2Of course you don’t have to set up Apache with HTTP/2 on Ubuntu 16.04, if you use one of our High-Performance 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, on how to set up Apache with HTTP/2 Support on Ubuntu 16.04,  please share it with your friends on the social networks using the buttons below, or simply leave a reply below. Thanks.

 

17 thoughts on “How To Set Up Apache with HTTP/2 on Ubuntu 16.04”

  1. cp debian/apache2-bin/usr/lib/apache2/modules/mod_http2.so /usr/lib/apache2/modules/

    its where it all falls apart for me. can’t go past it.

    Reply
  2. debian/rules:69: recipe for target ‘binary’ failed
    make: *** [binary] Error 2

    I get this error after this step:
    [user]$ fakeroot debian/rules binary

    Not sure what is going on, been trying to update my cURL and openssl for 2 days now with no luck.

    Reply
  3. Don’t copy – too many typos in this.
    e.g. ‘[user]$ cd apache-2.4.18’
    and ‘[user]$ sudo cp debian/apache2-bin/usr/lib/apache2/modules/mod_http2.so /usr/lib/apache2/modules/’

    Reply
    • Hello,

      Yes, sometimes you cannot just copy/paste and you may need to adjust the commands depending on the system and the version of the software you are working on.

      Thanks.

      Reply
  4. I don’t usually bother posting on these but I just wanted to say how useful this was. Way easier than the way I had intended to handle it. Probably saved me an hour or two. Just setup a store for a customer using this and H2 Push. I just recently switched from Enterprise Linux (They’re STILL using a backport patched OpenSSL 1.0.1 which is completely unsupported. Which means 0days may be found in it that don’t even exist in newer versions, and they’ll never get patched. CentOS/RHEL is completely insecure right now). So I’m used to manually having to compile everything in if I want to statically compile against non-system libraries.

    Reply
  5. The same, recipe for target ‘binary’ failed. I can provide log though)

    checking for APR… configure: error: the –with-apr parameter is incorrect. It must specify an install prefix, a build directory, or an apr-config file.
    debian/rules:72: recipe for target ‘override_dh_auto_configure’ failed
    make[1]: *** [override_dh_auto_configure] Error 1
    make[1]: Leaving directory ‘/home/http2/apache2/apache2-2.4.18’
    debian/rules:69: recipe for target ‘binary’ failed

    Reply
  6. Solved.

    sudo apt-get install libapr1-dev libaprutil1-dev

    And also

    sudo apt install libpcre3-dev (if pcre-devel is not installed, which was my case with Ubuntu 16.04)

    Reply
  7. These steps broke my Apache server within a few hours!!..

    The mod_http2 you build in these steps should probably match the exact version of Apache you’re already running! Nowhere in the steps is a new apache installed.
    I installed from a ppa a pre-built apache with http2 and it’s working good now. Lost a lot of traffic due to this guide.

    Reply
    • Actually I was running Apache/2.4.18, but still did not work.
      It caused errors like this:
      [Sun Feb 04 06:25:52.481812 2018] [mpm_event:error] [pid 3920:tid 140595095226240] AH00485: scoreboard is full, not at MaxRequestWorkers

      Reply

Leave a Comment