Add Compression to your Web Server

by webmonkey - 30 October 2008

Apache logoYour web server should be compressing your files before serving them to your users.

Why? There is no reason not to, 99% of browsers know how to decompress them and if you’re serious about hosting a professional web site, you should know every second it takes to get your content from your web server to your user counts. The visitor’s back button should strike fear into your heart.

When it comes down to your site and another site, sometimes it’s the seconds it takes to load which give can make the difference in your bounce rate. When you have multiple multimedia requests on a single page, those seconds add up.

Here’s a secret to help shave some of those seconds off of download time: It’s very simple, compress the data before it gets sent so you send less kilobytes. The data is decompressed on the client-side. It saves bandwidth and valuable time.

In fact, an average page can be compressed to up to a third of the size. The cost is some CPU time, which is a lot cheaper (and faster) than bandwidth. Best part is, it only requires a little Apache web server know how.

Here’s how to start compressing your transfers automatically.

What you’ll need

You’ll want to make sure you have the proper tools on your server.

A Web Server

First off, you need a proper web server and have proper extensible privileges. Check with your host if you have access. We’re going to use Apache for these instructions since it is one of the more popular web hosts out there. If you’re hosting your own Apache server, you should have no problems following these instructions.

A Compression Utility

There are two compression utilities you can use. Deflate, which uses the zlib compression library, or gzip, which uses the compression of the same name. Either method compresses roughly to the same size and is supported by HTTP 1.1 (Internet Explorer, Firefox, Opera or Chrome all apply) — so it doesn’t really matter which one you choose.

That said, we’re going to use gzip because it can be expanded later to automatically pre-compress some of your files, saving your web server a little more time in the long run.

There are two ways to enable gzip compression. The static method which involves compiling a whole new version of Apache’s web server. This is necessary if your Apache install doesn’t support dynamically loaded modules. This is pretty rare (since Apache 1.3 included DSO by default).


If you are running a version of Apache 1.3 or happen to know your server doesn’t support DSO (dynamic shared object), run through the Webmonkey’s Tutorial:Apache for Beginners.

Next, download gzip and start up a Terminal window. Let’s get crackin’.

Install mod_gzip is the source file of the gzip utility that will compress your data. Think of Apache modules as plug-ins. You install them in your module folder, typically /usr/lib/apache.

Then we’ll edit our httpd.conf file (Apache’s preference file) to point to the gzip module on start up. Easy, right?

Here’s how to do it:

First you want to download your mod_gzip_so to your apache library folder. This depends on where you have Apache installed on your server, but the most common place would be at /usr/lib/apache.

Let’s say it’s at /usr/lib/apache. In this instance, you’d want to unpack the file (from the tmp folder in my example) this way:

tar zxvf mod_gzip- -C /usr/lib/apache/

cd into /usr/lib/apache/ and you should see your new mod_gzip_so folder.

We’ll have to compile the new module, but it should be easy with a little utility called apxs.

apxs -c ./mod_gzip_debug.c

This will compile the code and create a file in your directory. Fantastic. Now to point Apache to the newly compiled module.

Configure apache.conf

The Apache config file is usually in your /usr/lib/httpd directory. You can edit it using whatever text editor of your choice. Let’s use Vim in this example.
At this point we have to indicate in the apache.conf (or httpd.conf or httpd.conf, depending on your installation) file the module that we want to load. Open the file in your text editor and insert the following line (again, for UNIX-based systems):

LoadModule gzip_module /usr/lib/apache/mod_gzip-

At the bottom of the document, we’re going to tell Apache to automatically compress HTML, text, JavaScript, CSS, PHP and Perl files on detection of gzip capabilities before transfer.

To do this, enter the following at the bottom of your apache.conf file:

<IfModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*

That’s pretty much all there is to it. There are a lot more commands you can take advantage of by exploring the mod_gzip.conf.sample file found in the docs folder of the module.

One Comment on “Add Compression to your Web Server”

  1. Fiona Alkire Says:

    Your webLog is excellent. I m gonna bookmark, gracias. Continue working on blog.

Leave a Reply