Install Brotli in Apache 2.4 on Ubuntu 20.04
February 1, 2022Brotli is a new open-source compression format from google that improves on the performance of gzip in many cases, it only only works on an https connection. Enabling Brotli Compression means that your site files are shrinking and moving faster to visitor devices. Even someone with a slow internet connection or a mobile device with limited bandwidth can easily download your site.
Brotli has a general application so it can compress any type of file without destroying the data. However, it is not suitable for image files such as PNG, JPG, and GIF. These types of files are already compressed, and Brotli actually makes them bigger. You can try an image compression plugin instead.
Google’s Brotli is the new replacement for the old gzip and deflate compression. It is a revolutionary open source compression algorythm created by Google engineers. A Brotli study conducted by Gooogle confirmed that it can help to achieve up to 25% more compression ration for files than traditional gzip/deflate methods. It also uses less CPU, which helps to reduce server load significantly.
Prerequisites
Before installing getting started, you should already have install Ubuntu on your instance and have install Apache2.4 running as your web server with HTTPS. Apache has supported brotli since version 2.4.26 by way of the mod_brotli module. Check Apache version using this command on your terminal console.
apache2 -v
In Ubuntu 20.04 you will get an output similar to the one below.
Server version: Apache/2.4.41 (Ubuntu)
Server built: 2022-01-05T14:49:56
Update all available repositories on your system and install the Apache webserver using the apt command below.
sudo apt update && sudo apt upgrade -y
Installing Brotli binary
Run the following comnands
sudo apt-get install brotli -y
Download the brotli library
wget https://github.com/google/brotli/archive/master.zip
Extract it
unzip master.zip
cd brotli-master
Build and install
mkdir out && cd out
../configure-cmake
make
make test
make install
Build the Brotli Apache module
Install the dev packages, which are needed to build the module.
apt-get install apache2-dev -y
Get mod_brotli & compile
Let’s clone the apache-mod-brotli repo & compile the module, as you see in the next steps
git clone --depth=1 --recursive https://github.com/kjdev/apache-mod-brotli.git
cd apache-mod-brotli
./autogen.sh
./configure
make
Confirm by pressing ENTER
. The result will be a .so file located inside the .libs directory.
Let’s store this module inside Apache directory modules.
install -D .libs/mod_brotli.so /usr/lib/apache2/modules/mod_brotli.so -m 644
Configure Brotli Module
Ubuntu will require to use a2enmod module in order to activate this module inside Apache. Follow the next steps:
cd /etc/apache2/mods-available
echo "LoadModule brotli_module /usr/lib/apache2/modules/mod_brotli.so" > brotli.load
Create a brotli.conf file
nano -w brotli.conf
Paste this configuration inside the file
<IfModule brotli_module> AddOutputFilterByType BROTLI_COMPRESS text/html text/plain text/css text/xml AddOutputFilterByType BROTLI_COMPRESS text/css AddOutputFilterByType BROTLI_COMPRESS application/x-javascript application/javascript AddOutputFilterByType BROTLI_COMPRESS application/rss+xml AddOutputFilterByType BROTLI_COMPRESS application/xml AddOutputFilterByType BROTLI_COMPRESS application/json </IfModule>
Hit CTRL+X
followed by Y
and ENTER
to save and close the file.
Use a2enmod to enable the mod_brotli module and then restart Apache2.
a2enmod brotli
service apache2 restart
That’s all, at this time mod_brotli should be installed and working properly on Ubuntu Linux. We’ve used a basic brotli configuration that includes a good balance between compression level and accepted mime types.
Check mod_brotli is working
The best way to check if mod_brotli is working is by using this simple website tool at GiftOfSpeed.com.
That should do it!