install brotli for apache in ubuntu

How to Install Brotli for Apache in Ubuntu

Brotli is a data compression algorithm developed by Google. It is a good alternative to gzip and bzip2, and provides 10-20% more compression than gzip & bzip2, in some cases. It is supported by all major browsers like Chrome, Firefox, Opera, Safari. So you can safely enable it on your web server to improve your website speed & bandwidth consumption. In this article, we will learn how to install Brotli for Apache in Ubuntu.


How to Install Brotli for Apache in Ubuntu

Here are the steps to install Brotli for Apache in Ubuntu. You need to be logged into Ubuntu as a user with sudo privileges to be able to install Brotli.


1. Install Brotli

Open terminal and run the following command to install Brotli.

$ sudo apt install brotli -y 


2. Enable Brotli

Apache server contains Brotli module by default. We just need to enable it with the following command.

$ sudo a2enmod brotli 


3. Enable Virtual Host

Next, we need to configure Apache virtual host to perform compression using Brotli algorithm. For this, open virtual host configuration file in a text editor. You will find the file in /etc/apache2/sites-available folder. If you have not created separate virtual host file for your website, open the default virtual host file.

$ sudo vi /etc/apache2/sites-available/000-default.conf

Add the following lines to it.

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

Your virtual host configuration file will look something like.

<VirtualHost *:80>
      ServerAdmin webmaster@localhost
      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> 

Save and exit the file.


4. Restart Apache Server

Restart Apache server to apply changes.

$ sudo systemctl restart apache2 


5. Test Compression

You can test the compression by opening your website in browser and checking response headers in browser console. You can also retrieve these values using curl command from terminal.

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

You will get the following output. Check the value of ‘Content-Encoding’ header. It should read ‘br’ indicating Brotli algorithm.

HTTP/1.1 200 OK
Date: Thu, 22 Sep 2021 06:26:54 GMT
Server: Apache/2.4.41 (Ubuntu)
Upgrade: h2,h2c
Connection: Upgrade
Last-Modified: Fri, 05 Feb 2021 08:55:44 GMT
ETag: "33-5ba92fc4cecdf-br"
Accept-Ranges: bytes
Vary: Accept-Encoding
Content-Encoding: br
Content-Length: 46
Content-Type: text/html

That’s it. In this article, we have learnt how to install Brotli for Apache server in Ubuntu. You can use these steps to install Brotli for your website.

Also read:

How to Uninstall Docker in CentOS
How to Uninstall Asterisk in Ubuntu
Json.dump vs Json.dumps in Python
How to Uninstall Docker in Ubuntu
How to Become Root User in Ubuntu

Leave a Reply

Your email address will not be published. Required fields are marked *