How to Enable mod_brotli Brotli Compression on Apache Web Server on CWP

Brotli compression for Apache will save you from slow compression speed as it uses latest compression technology developed by google. The mod_brotli module provides the BROTLI_COMPRESS output filter that allows output from your server to be compressed using the brotli compression format before being sent to the client over the network. This module uses the Brotli library found at https://github.com/google/brotli.

Follow the installation procedure steps to install mod_brotli :-

Installing Brotli on your server:

yum install pcre-devel cmake -y
cd /usr/local/src
git clone https://github.com/google/brotli.git
cd brotli
git checkout v1.0
./configure-cmake
make && make install

Adding path for brotli dependencies files (run this commands one by one):

grep "/usr/local/lib/" /etc/ld.so.conf || echo "/usr/local/lib/" >> /etc/ld.so.conf
ldconfig

Compile Apache with this flags from CWP Apache rebuild module WebServer Settings > Apache Re-Build

then select the latest apache version  “Select NEW Apache version:” and hit “Next” and add this flags to “Additional Configuration” and hit “Start Compiler in Background

--enable-brotli 
--with-brotli=/usr

eg : 

After Apache is rebuilded create the config file :

touch /usr/local/apache/conf.d/brotli.conf

Now add this line to /usr/local/apache/conf.d/brotli.conf :

LoadModule brotli_module modules/mod_brotli.so
<IfModule mod_brotli.c>
BrotliCompressionQuality 6

# To enable globally 
#AddOutputFilterByType BROTLI_COMPRESS text/html text/plain text/xml text/css text/javascript application/x-javascript application/javascript application/json application/x-font-ttf application/vnd.ms-fontobject image/x-icon

BrotliFilterNote Input brotli_input_info
BrotliFilterNote Output brotli_output_info
BrotliFilterNote Ratio brotli_ratio_info
LogFormat '"%r" %{brotli_output_info}n/%{brotli_input_info}n (%{brotli_ratio_info}n%%)' brotli
CustomLog "logs/brotli_log" brotli

#Don't compress content which is already compressed
SetEnvIfNoCase Request_URI \
\.(gif|jpe?g|png|swf|woff|woff2) no-brotli dont-vary

#Make sure proxies don't deliver the wrong content
Header append Vary User-Agent env=!dont-vary
</IfModule>

to enable brotli for all of your sites remove “#” before from AddOutputFilterByType

** BrotliCompressionQuality 6 for better compression you can select value 0-11 i’ll recommend value 6

After configuration restart Apache webserver :

service httpd restart

To enable brotli compression for your site add this config to .htaccess :

<IfModule mod_brotli.c>
AddOutputFilterByType BROTLI_COMPRESS text/html text/plain text/xml text/css text/javascript application/x-javascript application/javascript application/json application/x-font-ttf application/vnd.ms-fontobject image/x-icon
</IfModule>
Back to top button