• Home
  • Archive
  • Tools
  • Contact Us

The Customize Windows

Technology Journal

  • Cloud Computing
  • Computer
  • Digital Photography
  • Windows 7
  • Archive
  • Cloud Computing
  • Virtualization
  • Computer and Internet
  • Digital Photography
  • Android
  • Sysadmin
  • Electronics
  • Big Data
  • Virtualization
  • Downloads
  • Web Development
  • Apple
  • Android
Advertisement
You are here:Home » Install, Configure Brotli On Apache, WordPress, W3TC

By Abhishek Ghosh June 12, 2017 9:19 am Updated on June 12, 2017

Install, Configure Brotli On Apache, WordPress, W3TC

Advertisement

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, WordPress, W3TC

 

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 :

Advertisement

---

Vim
1
2
3
apt update
apt upgrade
apt-get install brotli

Then install the dependencies needed to build :

Vim
1
sudo apt-get install autoconf automake pkg-config libgtk-3-dev libtool apache2-dev

We will build Apache Brotli Module from this repo :

Vim
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 :

Vim
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 :

Vim
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 :

Vim
1
2
./autogen.sh
./configure

At the end, you’ll receive this kind of message :

Vim
1
make[1]: Leaving directory '/../apache-mod-brotli'

Now run ls :

Vim
1
ls

You’ll see lot of new files :

Vim
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 :

Vim
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 :

Vim
1
cd /etc/apache2/mods-available

Make sure that you are in the above directory. Create the load file :

Vim
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 :

Vim
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 :

Vim
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 :

Vim
1
a2enmod brotli

Restart Apache :

Vim
1
systemctl restart apache2

Now, if you go to test on some online tools like :

Vim
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 :

Vim
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 :

Vim
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 :

Vim
1
a2enmod brotli

Restart Apache :

Vim
1
systemctl restart apache2

Thats it. Here is official documentation from Apache :

Vim
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 :

Vim
1
cd /var/www/html

Open .htaccess

Vim
1
nano .htaccess

and add this on the top of everything :

Vim
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 :

Vim
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 :

Vim
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 :

Vim
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 :

Vim
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:

Vim
1
https://blogs.dropbox.com/tech/2017/04/deploying-brotli-for-static-content/

Tagged With apache install mod_brotli ubuntu 14 04 , brotli apache WordPress , change wordpress to brotli
Facebook Twitter Pinterest

Abhishek Ghosh

About Abhishek Ghosh

Abhishek Ghosh is a Businessman, Surgeon, Author and Blogger. You can keep touch with him on Twitter - @AbhishekCTRL.

Here’s what we’ve got for you which might like :

Articles Related to Install, Configure Brotli On Apache, WordPress, W3TC

  • Nginx Brotli : Compression for HTTPS

    Nginx Brotli Compression is a Modern Loseless Compression Algorithm Like gzip. ngx_brotli is a Set of 2 Nginx Modules. Here is How To.

  • Nginx WordPress Configuration Sample File

    Here is Ready to Use Nginx Wordpress Configuration Sample File Which Can Used With Either Community Edition of Nginx or Nginx Plus & PHP5-FPM.

  • Configure Apache With Fail2Ban on Ubuntu 18.04

    Here is How To Configure Apache With Fail2Ban on Ubuntu 18.04 to block more types of malicious attempts towards server to create a practical firewall.

  • How To Install Apache2 on Ubuntu 18.04 With Let’s Encrypt, HTTP/2, HSTS

    Here is Detailed Guide on How To Install Apache2 on Ubuntu 18.04 With Let’s Encrypt, HTTP/2, HSTS With Commands and Configurations For Most Secured Setup.

performing a search on this website can help you. Also, we have YouTube Videos.

Take The Conversation Further ...

We'd love to know your thoughts on this article.
Meet the Author over on Twitter to join the conversation right now!

If you want to Advertise on our Article or want a Sponsored Article, you are invited to Contact us.

Contact Us

Subscribe To Our Free Newsletter

Get new posts by email:

Please Confirm the Subscription When Approval Email Will Arrive in Your Email Inbox as Second Step.

Search this website…

 

Popular Articles

Our Homepage is best place to find popular articles!

Here Are Some Good to Read Articles :

  • Cloud Computing Service Models
  • What is Cloud Computing?
  • Cloud Computing and Social Networks in Mobile Space
  • ARM Processor Architecture
  • What Camera Mode to Choose
  • Indispensable MySQL queries for custom fields in WordPress
  • Windows 7 Speech Recognition Scripting Related Tutorials

Social Networks

  • Pinterest (24.3K Followers)
  • Twitter (5.8k Followers)
  • Facebook (5.7k Followers)
  • LinkedIn (3.7k Followers)
  • YouTube (1.3k Followers)
  • GitHub (Repository)
  • GitHub (Gists)
Looking to publish sponsored article on our website?

Contact us

Recent Posts

  • Hybrid Multi-Cloud Environments Are Becoming UbiquitousJuly 12, 2023
  • Data Protection on the InternetJuly 12, 2023
  • Basics of BJT TransistorJuly 11, 2023
  • What is Confidential Computing?July 11, 2023
  • How a MOSFET WorksJuly 10, 2023
PC users can consult Corrine Chorney for Security.

Want to know more about us?

Read Notability and Mentions & Our Setup.

Copyright © 2023 - The Customize Windows | dESIGNed by The Customize Windows

Copyright  · Privacy Policy  · Advertising Policy  · Terms of Service  · Refund Policy