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 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
apt-get install brotli
Install the cmake compiler:
yum install cmake -y
Download the brotli library:
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:
[[email protected]:~]brotli --version brotli 1.0.1 [[email protected]:~]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. [[email protected]:~]
Build the Brotli Apache module
Install the dev packages, which are needed to build the module:
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:
install -D .libs/mod_brotli.so /usr/lib/apache2/modules/mod_brotli.so -m 644
cp .libs/mod_brotli.so /usr/lib64/httpd/modules/mod_brotli.so -fv
Configure Brotli Module
Create a brotli.conf file
nano -w /etc/httpd/conf.d/brotli.conf
Place this inside:
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
nano -w brotli.conf
Paste this configuration inside:
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.
I tried to install it with apt-get install brotli and compiled mod_brotli from git but it doesen’t work
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.
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):
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:
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! 🙂
good tuto but missing small steps at the end for centos users
1 – cd /etc/httpd/conf.modules.d/
2 – nano 00-brotli.conf
add this line
3 – LoadModule brotli_module modules/mod_brotli.so
service httpd restart
Running ubuntu 20.04 Apache
followed steps but getting below error:
AH00526: Syntax error on line 9 of /etc/apache2/sites-enabled/**website**
Jan 29 13:45:47 apachectl: Unknown filter provider BROTLI_COMPRESS