The HyperText Transfer Protocol version 2 (HTTP/2) is a significant improvement over its predecessor, HTTP/1.1. With features like multiplexing, header compression, and server push, HTTP/2 offers faster web page load times and a better overall user experience.

HTTP/2 does not modify the application semantics of HTTP in any way. Instead, it changes how the data is framed and transported between the server and client, making the exchanges more efficient. Many modern browsers support HTTP/2, so enabling it on your server can result in improved performance for your visitors.

To enable HTTP/2 in the Apache web server, you need the module mod_http2. It comes with Apache version 2.4.17 and later. If you're using an older version, you may need to upgrade your Apache instance.

Steps to configure HTTP/2 for Apache:

  1. Launch terminal.
  2. Ensure you have Apache version 2.4.17 or newer installed.

    $ apachectl -v
    Server version: Apache/2.4.46 (Unix)
    Server built: Jul 1 2020 09:13:15

    This command outputs the version of your Apache server. Ensure it's 2.4.17 or newer.

    Use httpd -v if you're using CentOS or other Red hat variants.

  3. Enable http2 module for Apache.

    $ sudo a2enmod http2 # Ubuntu, Debian and SUSE
    Enabling module http2.
    To activate the new configuration, you need to run:
      systemctl restart apache2
    • Distribution with a2enmod support can simply run the command above without having to manually enable the required modules.
    • Fedora, CentOS and Red Hat enables the module by default so requires no manual action to enable the modules.
    Options Debian, Ubuntu openSUSE and SLES Fedora Core, CentOS, RHEL macOS homebrew xampp
    a2enmod support yes yes no no no no
    Modules to install none
    Module name n/a http2
    Loadmodule directive n/a LoadModule http2_module <module_locations>/mod_http2.so
  4. Open the configuration file for your Apache website using your preferred text editor.

    $ sudo vi /etc/apache2/sites-available/000-mysite.conf
  5. Add relevant Protocols directive in main Apache configuration file or on a specific virtual server configuration.

    <VirtualHost *:80>
            # .....
            # ....
            Protocols h2 h2c http/1.1
    </VirtualHost>
  6. Restart Apache to apply the changes.

    $ sudo systemctl restart apache2 # Ubuntu and Debian
    $ sudo systemctl restart httpd # CentOS and Red Hat
  7. Confirm that HTTP/2 is enabled by checking the response header of your HTTP request.

    $ curl --http2 --head  onlineweb.tools
    HTTP/1.1 101 Switching Protocols
    Upgrade: h2c
    Connection: Upgrade
    
    HTTP/2 200
    date: Sun, 00 Jan 1900 00:00:00 GMT
    server: Apache/2.4.41 (Ubuntu)
    last-modified: Sat, 08 Feb 2020 14:15:13 GMT
    etag: W/"2aa6-59e11227347f6"
    accept-ranges: bytes
    content-length: 10918
    vary: Accept-Encoding
    content-type: text/html