This post shows students and new users the step to add Brotli support to Apache when using Ubuntu Linux. Brotli (br for short), is an open source compression algorithm developed by Google that can be used as an alternative to Gzip, Zopfli and Deflate. In some studies, data can be compressed 10-20% more than current compression algorithms.

If you want to use Brotli with Apache, you will need to enable the use of Apache brotli module. Unlike Nginx, Brotli is fully compatible with the Apache web server.

This post was tested on Ubuntu Linux, but should apply to other Linux distributions. The reason I chose Ubuntu is that it is easy to use, especially for new users.

Also, for students and new users learning Linux, the easiest place to start learning is Ubuntu Linux. Ubuntu is the modern open source Linux operating system for desktops, servers, and other devices.

When you’re ready to include Brotli support with Apache, follow the steps below:

How to install Let’s Encrypt SSL on Ubuntu Linux

Brotli requires SSL. Apache must have SSL support before you can use Brotli. Since Let’s encrypt is easy to install and use with Ubuntu, we have written a great post on how to install the free Let’s Encrypt SSL certificate.

Click on the post below to learn how to install Let’s Encrypt free SSL certificates on Ubuntu Linux.

How to install Let’s Encrypt on Ubuntu Linux with Apache

How to install Apache on Ubuntu Linux

If you don’t already have the Apache server installed, follow the steps below to download and install it from the Ubuntu repository.

Installing Apache on Ubuntu Linux is pretty straightforward and simple. The Apache package is in the Ubuntu repositories, so all you have to do is run the apt get command to install it.

We have written an excellent post on how to install Apache on Ubuntu Linux. Click on the post below to learn how to install and configure Apache on Ubuntu Linux.

How to install Apache on Ubuntu Linux

How to install and enable the Apache Brotli module

At this point, you should have Apache installed with Let’s Encrypt support. If you haven’t already, see the steps above.

Before you can enable the Brotli module for Apache, you must first install it. The Brotli package is available in the default Ubuntu repository.

Just run the following commands to install Brotli

sudo apt install brotli

Once Brotli is installed, run the following commands to enable the Apache Brotli module.

sudo a2enmod brotli

Then add the directive to the Apache virtual host to enable Brotli support. You need to add the following code in the virtual host configuration file.

<IfModule mod_brotli.c>
    AddOutputFilterByType BROTLI_COMPRESS text/html text/plain text/xml text/css text/javascript application/javascript
</IfModule>

An example example.com.conf Your virtual host configurations should look similar to the one shown below.

<VirtualHost *:80>
      ServerAdmin admin@example.com
      ServerName example.com
      DocumentRoot /var/www/
 
      <IfModule mod_brotli.c>
            AddOutputFilterByType BROTLI_COMPRESS text/html text/plain text/xml text/css text/javascript application/javascript
      </IfModule>
 
      ErrorLog ${APACHE_LOG_DIR}/error.log
      CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

When adding Brotli support above, run the following commands to restart Apache.

sudo systemctl restart apache2

You can send a text message to Brotli support using the curl command against your domain.

curl -I -H 'Accept-Encoding: br' http://example.com

You should do that!

Conclusion:

In this tutorial we have seen how to enable Brotli support in Apache. If you find any errors above or have something to add, please use the comment form below.

Write A Comment