Get Free Linux Server Security, Performance & Troubleshooting Tips

Install Brotli (mod_brotli) on Apache Web Server

Brotli compression is the new replacement for Gzip and mod_deflate. Days ago we wrote a tutorial on how to install brotli on Nginx web server, and today we will try to explore a way to install brotli compression on Apache web server.

Brotli support for Apache web servers is done by using mod_brotli, an official HTTP module that provides BROTLI_COMPRESS output filter which will let you serve brotli compressed data over the network to any clients who are using a brotli compatible browser.

However, due to incompatibility issues with my current httpd testing version, I will use a 3rd party repo that works great: kjdev/apache-mod-brotli.

mod_brotli uses the Brotli library that can be found at https://github.com/google/brotli, but that’s also explained in the instructions you will see down below.

Let’s move to the fun part.

install mod_brotli on apache web server

Install Apache Mod_Brotli on Apache

In order to have brotli installed and working on your server, you must have this requirements present on your server:

  • Plain CentOS/RHEL or Ubuntu box (no control panel installed).
  • Apache installed and working normally.
  • HTTPS enabled on your websites, as Brotli doesn’t work o http based websites.
  • Brotli linux binary installed on your server.

Installing Brotli binary

For Ubuntu

apt-get install brotli

For CentOS/RHEL

Install the cmake compiler:

yum install cmake -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

This will install the brotli binary, as you see below:

[root@server.nixcp.com:~]brotli --version
brotli 1.0.1
[root@server.nixcp.com:~]brotli --help
Usage: brotli [OPTION]... [FILE]...
Options:
 -# compression level (0-9)
 -c, --stdout write on standard output
 -d, --decompress decompress
 -f, --force force output file overwrite
 -h, --help display this help and exit
 -j, --rm remove source file(s)
 -k, --keep keep source file(s) (default)
 -n, --no-copy-stat do not copy source file(s) attributes
 -o FILE, --output=FILE output file (only if 1 input file)
 -q NUM, --quality=NUM compression level (0-11)
 -t, --test test compressed file integrity
 -v, --verbose verbose mode
 -w NUM, --lgwin=NUM set LZ77 window size (0, 10-24) (default:22)
 window size = 2**NUM - 16
 0 lets compressor choose the optimal value
 -S SUF, --suffix=SUF output file suffix (default:'.br')
 -V, --version display version and exit
 -Z, --best use best compression level (11) (default)
Simple options could be coalesced, i.e. '-9kf' is equivalent to '-9 -k -f'.
With no FILE, or when FILE is -, read standard input.
All arguments after '--' are treated as files.
[root@server.nixcp.com:~]

Build the Brotli Apache module

Install the dev packages, which are needed to build the module:

On Ubuntu:

apt-get install apache2-dev

CentOS  RHEL users should use:

yum install httpd-devel

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

The result will be a .so file located inside the .libs directory.

Let’s store this module inside Apache directory modules:

Ubuntu users:

install -D .libs/mod_brotli.so /usr/lib/apache2/modules/mod_brotli.so -m 644

CentOS/RHEL users:

cp .libs/mod_brotli.so /usr/lib64/httpd/modules/mod_brotli.so -fv

Configure Brotli Module

For CentOS/RHEL

Create a brotli.conf file

nano -w /etc/httpd/conf.d/brotli.conf

Place this inside:

<IfModule brotli_module>
 BrotliCompressionLevel 10
 BrotliWindowSize 22
 AddOutputFilterByType BROTLI text/html text/plain text/css text/xml
 AddOutputFilterByType BROTLI text/css
 AddOutputFilterByType BROTLI application/x-javascript application/javascript
 AddOutputFilterByType BROTLI application/rss+xml
 AddOutputFilterByType BROTLI application/xml
 AddOutputFilterByType BROTLI application/json
 </IfModule>

Save the file and restart httpd:

systemctl start httpd.service

That’s all for CentOS/RHEL users.

For Ubuntu users

Ubuntu will require to use a2enmod module in order to activate this module inside Apache. Follow the next steps:

Create a load file by running this comnands:

cd /etc/apache2/mods-available
echo "LoadModule brotli_module /usr/lib/apache2/modules/mod_brotli.so" > brotli.load

Configure Brotli

nano -w brotli.conf

Paste this configuration inside:

<IfModule brotli_module>
 BrotliCompressionLevel 10
 BrotliWindowSize 22
 AddOutputFilterByType BROTLI text/html text/plain text/css text/xml
 AddOutputFilterByType BROTLI text/css
 AddOutputFilterByType BROTLI application/x-javascript application/javascript
 AddOutputFilterByType BROTLI application/rss+xml
 AddOutputFilterByType BROTLI application/xml
 AddOutputFilterByType BROTLI application/json
 </IfModule>

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 both CentOS / RHEL, but also on Ubuntu Linux.

We’ve used a basic brotli configuration that includes a good balance between compression level and accepted mime types.

you can always extend the configuration to match your needs by reading the official Apache mod_brotli docs.

Check mod_brotli is working

The best way to check if mod_brotli is working is by using this simple Brotli Test, introduce the URL and wait for the result.

Otherwise, you can also check your server headers with curl -I www.yoursite.com to see if the Accept-encoding: br appear at the end.

Our Rating

About the Author: Esteban Borges

Experienced Sr. Linux SysAdmin and Web Technologist, passionate about building tools, automating processes, fixing server issues, troubleshooting, securing and optimizing high traffic websites.

3 Comments

  1. Debian options?

    I tried to install it with apt-get install brotli and compiled mod_brotli from git but it doesen’t work

  2. Hello. I followed this post, and despite having everything done correctly I see no effect and I still see gzip as Encoding.

    I also opened a discussion in Reddit to give more visibility. Thanks for such a great entry by the way.

    https://www.reddit.com/r/apache/comments/9yhlrp/centos_7_httpd_2435_ius_i_compiled_configured_and/

  3. If it doesn’t work then make sure you follow all the steps on the account “root”. First, enter the following in a command line on your server (or via an SSH-connection to your server):

    sudo su

    Then type the password of your current account and click on “Enter” on your keyboard.
    After that you are logged in as “root”, the only thing to do is to type:

    cd

    in the command line and click “Enter” on your keyboard to have all the commands being executed from the root of your server (this is necessary to be able to follow these steps).

    Now only follow the steps of the guide which are suited for your server.

    Good luck! 🙂

Leave a Reply

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