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:
Check the enabled modules.
The last and important thing to do is add the following line to your virtual host file.
It is not advised to compress images with it, as they would be compressed already.