HowTo: enable compression and caching on Apache2

A quick howto on enabling gzip compression and client side caching for static content (images, videos, css, javascript, etc..)

How to enable apache gzip compression:

This will basically “zip up” all your website content before apache sends it down to the client. All popular browsers support gzip compressed content so there is no harm enabling this. And if someone has an older browser, it’s totally backwards compatible! 

As root, enable the apache mod_deflate module, and restart apache:

# a2enmod deflate
# /etc/init.d/apache2 restart

Then head over to this website to test if your web server supports gzip compression

Then get Firefox, install FireBug, and then install the page speed firebug extension from here. Running the page speed test on your website will complain if compression isn’t working properly, it will also show other places you can improve the speed of your website in.

How to enable apache caching:

Enabling client side caching on static content is a must! The page speed plugin (above) will tell you what content should be cached (if it isn’t already). Here’s the process of enabling caching: 

As root, enable the mod_headers apache module, input the config and restart apache:

# a2enmod headers
# vi /etc/apache2/httpd.conf
   add this stuff to the config:

<IfModule mod_headers.c>
# 3 Month
<FilesMatch ".(flv|gif|jpg|jpeg|png|ico|swf)$">
Header set Cache-Control "max-age=7257600"
# 1 Week
<FilesMatch ".(js|css|pdf|txt)$">
Header set Cache-Control "max-age=604800"
<FilesMatch ".(pl|php|cgi|spl|htm|html)$">
Header unset Cache-Control
Header unset Expires
Header unset Last-Modified
FileETag None
Header unset Pragma

# /etc/init.d/apache2 restart

Now test with page speed plugin (above) to see if caching is working correctly.

You might have to killall -9 apache2 if apache still has some old workers around serving uncompressed content to your computer. And shift+refresh the website so any half cache stuff is re-downloaded with the correct caching headers for page speed to test.

Doneski !


1 thought on “HowTo: enable compression and caching on Apache2

  1. mark

    ahh this one i want really needed to set it in my apache for my website to load fast and this did the trick. thank you for this one 😉


Leave a Reply

Your email address will not be published.