[How To:] Brotli support in Apache2

To enable Brotli in Apache2 is very simple

Step 1 - Install

apt-get install brotli

Step 2 - edit your custom template file .tpl and .stpl files / copy and edit new so kept after update.

/usr/local/hestia/data/templates/web/apache2/

add something like the below (depending on what extension you want to compress with Brotli

<IfModule mod_brotli.c>
            AddOutputFilterByType BROTLI_COMPRESS text/html text/plain text/xml text/css text/javascript application/javascript
      </IfModule>

under the last and save

</IfModule>

Enable in Apache2

a2enmod brotli

then

systemctl restart apache2

You can test if it worked here

Brotli Test - Verify Brotli Compression Support | KeyCDN Tools

or check heqaders

HTTP Header Checker - Check HTTP Response Headers With curl | KeyCDN Tools

You will get something like below

HTTP/2
200
server: nginx
date: Sun, 24 Oct 2021 04:11:37 GMT
content-type: text/html; charset=utf-8
content-length: 1005
last-modified: Sun, 24 Oct 2021 01:54:31 GMT
cache-control: max-age=86400, proxy-revalidate
expires: Mon, 25 Oct 2021 04:11:37 GMT
vary: Accept-Encoding,User-Agent
content-encoding: br
strict-transport-security: max-age=15768000;
accept-ranges: bytes

As always try at your own risk. This was tested on a fresh Deb 10 install

1 Like

I have a simple solution to add brotli to your apache server wide configuration without templates.

add

<IfModule mod_brotli.c>
            AddOutputFilterByType BROTLI_COMPRESS text/html text/plain text/xml text/css text/javascript application/javascript
      </IfModule>

to a file named brotli.conf in /etc/apache2/mods-enabled

I prefer the Apache2 solution to the Nginx solution because if something goes wrong with the Brotli module of apache, it will not crash the web server. The broli module of Apache2 is a default package of the Linux distro. So, it is already tested to work with your version of Apache 2. Anyways, if it does not work, it will not crash thanks to the ifmodule tag.