How To Enable Brotli Compression on Apache

Enable Brotli Compression on Apache

In this tutorial, we will show you how to enable brotli compression. For those of you who didn’t know, Brotli is a new compression format launched by Google which claims to save 20%-28% over their previous gzip or and mod_deflate compatible implementation, for similar speeds. Brotli has the advantage that for the same amount of CPU work a smaller compressed file is created. This magnifies the advantages of using compression to serve site content. Brotli compression is supported by all the major browsers like Chrome, Firefox, Safari, and Microsoft Edge.

This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo‘ to the commands to get root privileges. I will show you through the step-by-step enabling Brotli Compression on Apache webserver. You can follow the same instructions for Ubuntu 18.04, 16.04, and any other Debian-based distribution like Linux Mint.

Enable Brotli Compression on Apache

Step 1. First, make sure that all your system packages are up-to-date by running the following apt commands in the terminal.

sudo apt update
sudo apt upgrade

Step 2. Installing Brotli on Ubuntu.

Now run the following command below to install Brotli on your system:

sudo apt install brotli

Step 3. Enable Brotli Compression on Apache.

Next step we enable the brotli module in Apache with the following command:

sudo a2enmod brotli

After that, configure the Apache virtual host by adding the following code to 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>

Example VirtualHost file:

<VirtualHost *:80>      ServerAdmin admin@your-domain.com      ServerName your-domain.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>

Save the file and restart the Apache service to apply changes:

sudo systemctl restart apache2

Step 4. Test Brotli.

Once successfully install and setup, you can check your server headers with curl -I -H 'Accept-Encoding: br' http://your-domain.com

Output:

HTTP/1.1 200 OK
Date: Thu, 01 Sep 2021 06:26:54 GMT
Server: Apache/2.4.46 (Ubuntu)
Upgrade: h2,h2c
Connection: Upgrade
Last-Modified: Fri, 10 Mei 2021 22:46:36 GMT
ETag: "33-5ba9m3ilanacdf-br"
Accept-Ranges: bytes
Vary: Accept-Encoding
Content-Encoding: br
Content-Length: 36
Content-Type: text/html

Congratulations! You have successfully installed the Brotli module. Thanks for using this tutorial to enable Brotli on Ubuntu 20.04 LTS (Focal Fossa) system. For additional help or useful information, we recommend you check the official Apache website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!