Previously we talked about Brotli compression in context of Nginx web server. Here is an all in one guide to install, configure Brotli on Apache, WordPress, W3TC running on Ubuntu 16.04 on dedicated server, cloud server instance. In order to follow this guide, you need SSH access and HTTPS/SSL/TLS to be active on your server/website. You can perform the steps on production server with running website with minimal downtime for restarting Apache an optional reboot.
Install, Configure Brotli On Apache On Ubuntu 16.04
We need to install the module, setup, configure on Apache. For testing this guide we used latest Ubuntu 16.04 and Apache with all updates up to Jun 12, 2017. We install Apache from our mentioned ondrej’s repo in old guides and configure SSL in this method we described.
SSH to your server. First update, upgrade and install Brotli library :
---
1 2 3 | apt update apt upgrade apt-get install brotli |
Then install the dependencies needed to build :
1 | sudo apt-get install autoconf automake pkg-config libgtk-3-dev libtool apache2-dev |
We will build Apache Brotli Module from this repo :
1 | https://github.com/kjdev/apache-mod-brotli |
Change directory to anywhere on your server other than any public directory, /opt
, /root
all are suitable locations. Do these steps :
1 2 3 | git clone --depth=1 --recursive https://github.com/kjdev/apache-mod-brotli.git cd apache-mod-brotli ls |
You’ll see these files :
1 2 | autogen.sh configure.ac Makefile.am README.md brotli LICENSE mod_brotli.c tests |
Now run these commands one by one, they’ll take some time :
1 2 | ./autogen.sh ./configure |
At the end, you’ll receive this kind of message :
1 | make[1]: Leaving directory '/../apache-mod-brotli' |
Now run ls
:
1 | ls |
You’ll see lot of new files :
1 2 3 4 5 6 7 8 | aclocal.m4 config.h.in libtool mod_brotli.c ar-lib config.log LICENSE mod_brotli.la autogen.sh config.status ltmain.sh mod_brotli_la-mod_brotli.lo autom4te.cache config.sub m4 mod_brotli_la-mod_brotli.o brotli configure Makefile README.md compile configure.ac Makefile.am stamp-h1 config.guess depcomp Makefile.in tests config.h install-sh missing |
After the step of make
, we run install
command. Here we will run this command :
1 | install -D .libs/mod_brotli.so /usr/lib/apache2/modules/mod_brotli.so -m 644 |
Now, change directory to Apache’s module’s directory :
1 | cd /etc/apache2/mods-available |
Make sure that you are in the above directory. Create the load
file :
1 | echo "LoadModule brotli_module /usr/lib/apache2/modules/mod_brotli.so" > brotli.load |
Next we need to create brotli.conf
file with the following content, it is a generalised configuration, you can tweak later :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | <IfModule brotli_module> # Compression ## BrotliCompressionLevel: 0-11 (default: 11) BrotliCompressionLevel 10 ## BrotliWindowSize: 10-24 (default: 22) BrotliWindowSize 22 # Logging # Filter note BrotliFilterNote Input brotli_in BrotliFilterNote Output brotli_out BrotliFilterNote Ratio brotli_ratio #LogFormat '"%r" %{brotli_out}n/%{brotli_in}n (%{brotli_ratio}n)' brotli #CustomLog ${APACHE_LOG_DIR}/brotli_access.log brotli # Output filter 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 # Global output filer if necessary. # SetOutputFilter BROTLI # SetEnvIfNoCase Request_URI \.txt$ no-br </IfModule> |
of course later you can add more types :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | AddOutputFilterByType BROTLI application/javascript AddOutputFilterByType BROTLI application/rss+xml AddOutputFilterByType BROTLI application/vnd.ms-fontobject AddOutputFilterByType BROTLI application/x-font AddOutputFilterByType BROTLI application/x-font-opentype AddOutputFilterByType BROTLI application/x-font-otf AddOutputFilterByType BROTLI application/x-font-truetype AddOutputFilterByType BROTLI application/x-font-ttf AddOutputFilterByType BROTLI application/x-javascript AddOutputFilterByType BROTLI application/xhtml+xml AddOutputFilterByType BROTLI application/xml AddOutputFilterByType BROTLI font/opentype AddOutputFilterByType BROTLI font/otf AddOutputFilterByType BROTLI font/ttf AddOutputFilterByType BROTLI image/svg+xml AddOutputFilterByType BROTLI image/x-icon AddOutputFilterByType BROTLI text/css AddOutputFilterByType BROTLI text/html AddOutputFilterByType BROTLI text/javascript AddOutputFilterByType BROTLI text/plain AddOutputFilterByType BROTLI text/xml |
Enable the module :
1 | a2enmod brotli |
Restart Apache :
1 | systemctl restart apache2 |
Now, if you go to test on some online tools like :
1 | https://tools.keycdn.com/brotli-test |
You’ll see that your site supports brotli.
Alternative Ready To Use Solution
This is not exactly recommended way by quick. If you followed the above procedure, then the end, you got three files. Those three files you can download by clicking the below link :
https://thecustomizewindows.com/Downloads/brotli.tar.gz
If you wget from our site, uncompress it :
1 2 3 4 | wget https://thecustomizewindows.com/Downloads/brotli.tar.gz tar -xzvf brotli.tar.gz cd /var/www/html/Downloads/brotli ls |
There will be three files – mod_brotli.so
, brotli.conf
, brotli.load
. You’ll place them in :
1 2 3 | /usr/lib/apache2/modules/mod_brotli.so /etc/apache2/mods-available/brotli.conf /etc/apache2/mods-available/brotli.load |
Enable the module :
1 | a2enmod brotli |
Restart Apache :
1 | systemctl restart apache2 |
Thats it. Here is official documentation from Apache :
1 | https://httpd.apache.org/docs/trunk/mod/mod_brotli.html |
Configure Brotli On Apache For WordPress, W3TC
Now change directory to where your WordPress is installed, like /var/www/html
:
1 | cd /var/www/html |
Open .htaccess
1 | nano .htaccess |
and add this on the top of everything :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | ### stanza for brotli starts ### <IfModule mod_headers.c> # Serve brotli compressed CSS files if they exist and the client accepts brotli. RewriteCond %{HTTP:Accept-encoding} br RewriteCond %{REQUEST_FILENAME}\.br -s RewriteRule ^(.*)\.css $1\.css\.br [QSA] # Serve gzip compressed CSS files if they exist and the client accepts gzip. RewriteCond %{HTTP:Accept-encoding} gzip RewriteCond %{REQUEST_FILENAME}\.gz -s RewriteRule ^(.*)\.css $1\.css\.gz [QSA] # Serve brotli compressed JS files if they exist and the client accepts brotli. RewriteCond %{HTTP:Accept-encoding} br RewriteCond %{REQUEST_FILENAME}\.br -s RewriteRule ^(.*)\.js $1\.js\.br [QSA] # Serve gzip compressed JS files if they exist and the client accepts gzip. RewriteCond %{HTTP:Accept-encoding} gzip RewriteCond %{REQUEST_FILENAME}\.gz -s RewriteRule ^(.*)\.js $1\.js\.gz [QSA] # Serve correct content types, and prevent mod_deflate double gzip. RewriteRule \.css\.gz$ - [T=text/css,E=no-gzip:1] RewriteRule \.js\.gz$ - [T=text/javascript,E=no-gzip:1] RewriteRule \.css\.br$ - [T=text/css,E=no-gzip:1] RewriteRule \.js\.br$ - [T=text/javascript,E=no-gzip:1] <FilesMatch "(\.js\.gz|\.css\.gz)$"> # Serve correct encoding type. Header set Content-Encoding gzip # Force proxies to cache gzipped & non-gzipped css/js files separately. Header append Vary Accept-Encoding </FilesMatch> <FilesMatch "(\.js\.br|\.css\.br)$"> # Serve correct encoding type. Header set Content-Encoding br # Force proxies to cache gzipped & non-gzipped css/js files separately. Header append Vary Accept-Encoding </FilesMatch> </IfModule> ### stanza for brotli ends ### |
It is better to reboot :
1 | reboot |
And clear all W3TC cache. We selected CSS, Js. You should know which other files Brotli supports. The above is dynamic compression which occurs on-the-fly when the visitor opens a page. There is static compression, in which the assets are compressed on the disk before the user request. The above module does not provide the static compression function. Basically it is the static compression which is better. If your file was styles.css
, it needs to be manually made to styles.css.br
. These Google’s stuffs are not so good like old good things. You need to create a script or run command compress the most commonly requested files to brotli. There is a PHP extension available for Brotli :
1 | https://github.com/kjdev/php-ext-brotli |
But yet, I never saw WordPress Brotli Plugin which will automate compression and flush from WordPress interface. I will suggest to manually compress only the CSS and Js served from your server (not CDN) to convert to brotli. From tests like webpagetest.org
, you can see which CSS, Js files from your server (not CDN) are commonly served. If a CSS has location :
1 | https://github.com/wp-content/themes/some.css |
On server, go to the location wp-content/themes/some.css
and convert it to brotli. Never forget to delete on some change and again recreate. In this way, you are serving some static and some dynamic brotli compressed files. Brotli sources provide C, Java, and Python bindings. You can either use these bindings or just fork/exec the bro tool to directly compress files, they have Python tool but Google’s Python is odd :
1 | https://github.com/google/brotli/tree/master/python |
How to easily of it and flush, that I will tell you in future guide. You can read Dropbox’s useful article which is far superior than that of Google’s docs:
1 | https://blogs.dropbox.com/tech/2017/04/deploying-brotli-for-static-content/ |