LinuxTutorialsUbuntu

How To Enable Brotli Compression on Apache

Enable Brotli Compression on Apache

In this tutorial, we will show you how to enable Brotli compression on Apache. For those of you who didn’t know, Brotli is a new compression format launched by Google that claims to save 20%-28% over their previous gzip or 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 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 the step-by-step enabling of Brotli Compression on the Apache webserver. You can follow the same instructions for Ubuntu 22.04, 20.04, and any other Debian-based distribution like Linux Mint.

Prerequisites

  • A server running one of the following operating systems: Ubuntu 22.04, 20.04, and any other Debian-based distribution like Linux Mint.
  • It’s recommended that you use a fresh OS install to prevent any potential issues.
  • SSH access to the server (or just open Terminal if you’re on a desktop).
  • You’ll need an active internet connection.
  • A non-root sudo user or access to the root user. We recommend acting as a non-root sudo user, however, as you can harm your system if you’re not careful when acting as the root.

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

For additional resources on installing Apache, read the post below:

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 2023 06:26:54 GMT
Server: Apache/2.4.46 (Ubuntu)
Upgrade: h2,h2c
Connection: Upgrade
Last-Modified: Fri, 10 Mei 2023 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!

r00t

r00t is a seasoned Linux system administrator with a wealth of experience in the field. Known for his contributions to idroot.us, r00t has authored numerous tutorials and guides, helping users navigate the complexities of Linux systems. His expertise spans across various Linux distributions, including Ubuntu, CentOS, and Debian. r00t's work is characterized by his ability to simplify complex concepts, making Linux more accessible to users of all skill levels. His dedication to the Linux community and his commitment to sharing knowledge makes him a respected figure in the field.
Back to top button