Enable Brotli Compression for your Apache server in less than 5 minutes

Brotli is a compression algorithm developed by Google that offers lots of benefits. like improved website load times and reduced bandwidth usage.

Here's how you can enable Brotli compression on Apache - also known as apache2:

Make sure you have the latest Apache server version

Since version 2.4.26, Apache already comes with a built-in Brotli module. In case you have an older version, please make sure to upgrade your Apache server. If that is not possible for whatever reason, you might be able to install the Apache Brotli package via your package manager.

Enable HTTPS

Most modern browsers will only accept a brotli-encoded file when it will be served via HTTPS. This is only one of the few reasons why you really should enable HTTPs if you haven't done yet, including security, access to more web APIs and better performance overall.

In case you are testing a local server, browsers deliver "false positives" and it appears as if Brotli would not work. Our Brotli checker has no problems and will ask for brotli encoding even when using HTTPS.

Enable the Brotli module

It can happen that the Brotli module is not enabled out of the box. To make sure the Apache module is enabled, run the following command in the shell of your environment:

sudo a2enmod brotli

Configure Brotli Compression

Next, you'll need to configure Apache to use Brotli compression. To do this, you'll need to add the following code to your Apache configuration file:

<IfModule mod_brotli.c>
AddOutputFilterByType BROTLI_COMPRESS text/html text/plain text/xml text/css text/javascript application/javascript application/x-javascript application/json application/xml
BrotliCompressionQuality 4
</IfModule>

This code tells Apache to use the Brotli compression algorithm for the specified file types. It also specifies the Brotli compression quality, which is '5' by default, but '4' is faster and more efficient compared to GZIP.

If you're using a virtual host, you can add this code to the virtual host configuration file. Alternatively, you can add it to the global Apache configuration file to enable Brotli compression for all websites hosted on the server. An example for a virtual host configuration can look like this:

<VirtualHost *:80>
ServerAdmin webmaster@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 application/x-javascript application/json application/xml
BrotliCompressionQuality 4
</IfModule>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Restart the Apache server

To apply the changes, you now have to restart the server. It is enough to run the following bash command and the server will restart with the updated configurations:

sudo systemctl restart apache2
# or
sudo service apache2 restart

Apache now compresses all requests matching the passed MIME types (e.g. text/html) on the fly via Brotli.

Test Brotli Compression

Once you've installed the Brotli module and configured Apache to use it, you should test whether Brotli compression is working correctly. You can use our Brotli checker to check your website and see if it worked.