HTTP/1 to HTTP/2 on Apache

HTTP/1 to HTTP/2 on Apache

In this article we will see how we can create a new site in Apache and convert it to HTTP2

But what is HTTP/2

HTTP/2 is a modern web protocol designed to enhance browsing speed and efficiency. Unlike HTTP/1, it enables multiplexing, allowing multiple requests and responses to be transmitted simultaneously over a single connection, leading to faster page loads and improved user experiences.

But Why do we need to convert?

Converting to HTTP/2 is essential to unlock faster and more efficient web experiences. With its multiplexing, header compression, and prioritization features, HTTP/2 significantly reduces page load times, minimizes latency, and enhances overall performance, ensuring a smoother and more responsive user interaction.

Install Apache: If you haven't already done so, install Apache using the following command

sudo apt-get update
sudo apt-get install apache2 -y

Create a new directory for your site under the /var/www directory. For example, if you want to create a site called example.com, you can create a directory called /var/www/example.com

sudo mkdir /var/www/example.com

Create an index.html file inside the new directory with the following command


sudo vim /var/www/example.com/index.html

Add the below content inside index.html

<!DOCTYPE html>
<html>
<head>
    <title>Hello World!</title>
</head>
<body>
    <h1>Hello World!</h1>
    <p>This is a test page.</p>
</body>
</html>

Set the correct file permissions for the new directory and file

sudo chown -R www-data:www-data /var/www/example.com
sudo chmod -R 755 /var/www/example.com

Create a new virtual host configuration file for your site

sudo vim /etc/apache2/sites-available/example.com.conf

Inside the file, type the following configuration:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Enable the new virtual host configuration and restart Apache2

sudo a2ensite example.com.conf
sudo systemctl restart apache2

Finally, add an entry to your Domain provider to the server's public IP address, if you are doing this in your lab environment open /etc/hosts file to map the domain name (example.com) to the IP address of your local machine,

# on the machine you have browser access ,
sudo vim /etc/hosts
# add the below line by replacing with your local server IP address
[your-IP] example.com www.example.com

Note: If you have a real server and Domain name you don't need the above step, you just have to Add a DNS record to your domain name provider

currently, this site is serving with http1

let's convert this site to http2 and see the results

for HTTP2, we require SSL, since we are in a lab environment we can create from OpenSSL

The below command will create a private key server.key and certificate(will share to browser or client) server.crt

# this command will create private key server.key and certificate(will share to browser or client) server.crt
openssl req -new -newkey rsa:2048 -days 365 -nodes -x509 -keyout server.key -out server.crt

enable the following modules

  • ssl

  • http2

sudo a2enmod ssl
sudo a2enmod http2

modify the existing example.conf file and add block for https traffic

open file sudo vim /etc/apache2/sites-available/example.com.conf

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:443>
ServerName example.com
DocumentRoot /var/www/example.com
# SSL config for file , replace location with your key and certificate
SSLEngine on
SSLCertificateFile /home/usama/server.crt
SSLCertificateKeyFile /home/usama/server.key
# Telling to use http2
Protocols h2 http/1.1
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

finally, if we want to redirect all traffic of HTTP to HTTPS we will need to also enable the rewrite module

sudo a2enmod rewrite

and add the below lines

RewriteEngine On
RewriteRule ^(.)$ https://%{HTTP_HOST}$1 [R=301,L]

your apache config file will look like this

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

RewriteEngine On
RewriteRule ^(.)$ https://%{HTTP_HOST}$1 [R=301,L]
</VirtualHost>

<VirtualHost *:443>
ServerName example.com
DocumentRoot /var/www/example.com
# SSL config for file , replace location with your key and certificate
SSLEngine on
SSLCertificateFile /home/usama/server.crt
SSLCertificateKeyFile /home/usama/server.key
# Telling to use http2
Protocols h2 http/1.1
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Did you find this article valuable?

Support Muhammad Usama by becoming a sponsor. Any amount is appreciated!