Enable brotli compression on Apache on Ubuntu

Apache by default have gzip compression enabled on all recent versions. But brotli is much better compression than gzip. If you would like to enable brotli on Apache on Ubuntu, read on how to do that.

Recent Apache (2.4.26 and above) versions has brotli module by default, the only thing you need is to enable it.

But if it is not installed you can install it with the following command.

apt install brotli

Once installation is finished, enable it with:

a2enmod brotli

Check the enabled modules.

apache2 -M

The last and important thing to do is add the following line to your virtual host file.

SetOutputFilter BROTLI_COMPRESS
AddOutputFilterByType BROTLI_COMPRESS text/html text/plain text/xml text/css text/javascript application/javascript

It is not advised to compress images with it, as they would be compressed already.

The above piece of code will compress text-based files like HTML, JavaScript, CSS, XML etc.