Member Sign In
Not a member?

A Wired.com user account lets you create, edit and comment on Webmonkey articles. You will also be able to contribute to the Wired How-To Wiki and comment on news stories at Wired.com.


It's fast and free.

Sign in with OpenID
Sign In
Webmonkey is a property of Wired Digital.
processing...
Join Webmonkey

Please send me occasional e-mail updates about new features and special offers from Wired/Webmonkey.
Yes No

Please send occasional e-mail offers from Wired/Webmonkey affiliated web sites and publications, and carefully selected companies.
Yes No

I understand and agree that registration on or use of this site constitutes agreement to Webmonkey's User Agreement and Privacy Policy.
Webmonkey is a property of Wired Digital.
processing...

Retrieve Sign In

Please enter your e-mail address or username below. Your username and password will be sent to the e-mail address you provided us.

or
Webmonkey is a property of Wired Digital.
processing...

Welcome to Webmonkey

A private profile page has been created for you.
As a member of Webmonkey, you can now:
  • edit articles
  • add to the code library
  • design and write a tutorial
  • comment on any Webmonkey article
Close
Webmonkey is a property of Wired Digital.

Sign In Information Sent

An e-mail has been sent to the e-mail address registered in this account.
If you cannot find it in your in-box, please check your bulk or junk folders.
Sign In
Webmonkey is a property of Wired Digital.

Add Compression to Your Web Server

/skill level/
/viewed/
0 Times

Your 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.


This article is a wiki. If you want to flesh out some of the instructions, feel free to hop on in and edit it up.

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'.

Installing mod_gzip

mod_gzip.so 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:

sloganbi@compy:/tmp%tar zxvf mod_gzip-1.3.26.1a.tgz -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.

sloganbi:compy: apxs -c ./mod_gzip_debug.c

This will compile the code and create a mod_gzip.so 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 direct the module to go directly to apache.conf (or httpd.conf or httpd.conf, depending on your installation) file. This should be in your apache directory or in your home directory.

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-1.3.26.1a/mod_gzip.so

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.*
</IfModule>

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.

Edit this article
Reddit Digg
 
Subscribe now

Special Offer For Webmonkey Users

WIRED magazine:
The first word on how technology is changing our world.

Subscribe for just $10 a year