Enabling Apache’s mod_deflate

If you’re running an Apache web server and you want to speed up your visitor’s browsing you can enable mod_deflate, which has replaced mod_gzip in Apache 2.

You’ll probably find your mod_default module is already installed and available with just a couple of lines of configuration.

I’ve enabled mod_deflate globally across all my domains and hence have added my configuration to my main httpd.conf file. With most flavours of UNIX it’s located in /etc/httpd. If not consult the documentation for your distro. Should you only wish to enable for a single domain you’d need to add the AddOutputFilterByType and BrowserMatch rules below to the VirtualHost section in your configuration.

These instructions are tailored for my setup (a (dv) 3.5 with MediaTemple.) Your host may put your configuration files elsewhere. You will need root access via SSH to your web server to complete these modifications. If you aren’t comfortable in the command line best you ask your host very nicely to do this for you.

First off we need to make sure mod_deflate is being loaded by Apache. Fly in to the command-line and use…

# grep 'mod_deflate' /etc/httpd/conf/httpd.conf

…to search for line that will load the mod_deflate module. If it’s commented out with a leading “#” then we need to remove the hash sign in order for mod_deflate to be loaded when we start up Apache.

I personally used vi to do this but you could use perl from the command line if you wished to. Remember to back up your files regularly and do not make modifications to httpd.conf without a stable copy you can revert to should things go wrong!

# perl -pi -e 's/# LoadModule mod_deflate/LoadModule mod_deflate/g' /etc/httpd/conf/httpd.conf

This command searches for an occurrence of “# LoadModule mod_deflate” in your configuration file and replaces if with “LoadModule mod_deflate” essentially just stripping the hash sign from the line.

Once we have an entry in httpd.conf for loading mod_deflate we need to add configuration settings for mod_deflate. Again, I used vi for this.

# vi /etc/httpd/conf/httpd.conf

In you configuration file you need to add…

#
# Deflate output configuration
#
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

I put these lines in with some of the shtml configuration options near the top of my file. Just make sure you don’t nest the options inside a particular module’s configuration section or within a VirtualHost definition.

I used grep to pull the lines out of my http.conf file below. The -B flag pulls a number of lines before a match is found and the -A pulls lines after. The command below will find any occurrence of “Deflate” in httpd.conf and print out one line before and six lines after.

# grep -B1 -A6 'Deflate' /etc/httpd/conf/httpd.conf
#
# Deflate output configuration
#
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

With mod_deflate enabled and our configuration settings added to compress HTML, plain, XML and css files we’re almost ready to restart Apache. Before doing so we always need to check syntax in our config files using…

# apachectl -t

If we get a “Syntax OK” message back we’re good to initiate a graceful restart.

# apachectl graceful

Now we should find Apache pushes files out using gzip compression, hence reducing page loading times and making the end user experience that bit more pleasurable.

There is a similar article on howtoforge regarding setting up mod_deflate on a Debian system. Check it out at HowToForge.com.

Leave a Reply