Since I rebuilt my VPS the other day there a number of tweaks and changes I need to make which can speed up the website – one of the easiest ones with the biggest effect on pageload time is to have apache serve compressed versions of pages to users. Here’s how to do it, and test that it’s working.
Enabling mod_deflate and mod_headers
Chances are good that you already have mod_deflate and mod_header on your server (they typically come with the apache install) – so all you need to do is enable them. To do so, open your main apache config (for example, on mine it’s /etc/httpd/conf/httpd.conf and uncomment the LoadModule lines for each module, so that they end up like this:
LoadModule deflate_module modules/mod_deflate.so
LoadModule headers_module modules/mod_headers.so
With that done, you can head down to the bottom of the config file and add the following section to enable compression of all html, css, js, xml and such.
# Restrict compression to these MIME types
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE text/css
# Level of compression (Highest 9 - Lowest 1)
# Make sure proxies don't deliver the wrong content
Header append Vary User-Agent env=!dont-vary
Restarting apache and testing it out
With those changes made, save the file, restart apache (for example, via: sudo systemctl restart httpd.service if you’re on a systemd-using machine), and then go to: http://www.whatsmyip.org/http-compression-test/
Plug in your URL and hit submit, and you should see some good news similar to this:
Very nice =D
Many thanks to: brighterlamp.com for the original article this one is based on.