Speed up a web site by enabling Apache file compression

Technologies: Apache 1.3+ or 2+, mod_gzip or mod_deflate

Speed up page load times by automatically compressing CSS, HTML, and JavaScript files in Apache. Compressed files are smaller and faster to send.

This article is part of the Essential steps to speed up a Drupal web site article series.

How to enable file compression

Apache 1.x and 2.x can automatically compress files, but neither one comes with a compressor enabled by default. Enabling compression reduces CSS, HTML, and JavaScript file sizes by 55-65% and speeds up overall page load times by 35-40%.

Apache uses plug-in modules to add functionality. For Apache 1.x, use the free mod_gzip module to compress files. For Apache 2.x, use mod_gzip or the built-in mod_deflate module.

Enable file compression using mod_gzip

The mod_gzip module can be used with Apache 1.x or 2.x, but it doesn’t come with either Apache distribution. You’ll need to download and install it separately.

  1. Windows:
    1. Log in to your PC using an account with administrator privileges.
    2. Download the zip file containing ApacheModuleGzip.dll from SourceForge.
    3. Unzip the file.
    4. Move ApacheModuleGzip.dll to your Apache modules folder (typically “c:\Program Files\Apache Group\Apache\modules”).
    5. Edit your server configuration file using a text editor like NotePad (typically “c:\Program Files\Apache Group\Apache\conf\httpd.conf”). Add the following line to your server configuration file as the last loaded module:
      LoadModule gzip_module modules/ApacheModuleGzip.dll

      Add the following lines to your server configuration file or to a site’s “.htaccess” 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 handler ^cgi-script$
      mod_gzip_item_include mime ^text/.*
      mod_gzip_item_include mime ^application/x-javascript.*
      mod_gzip_item_exclude mime ^image/.*
      mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
      </IfModule>
  2. Mac:
    1. Log in to your Mac using an account with administrator privileges.
    2. Download the zip file containing the module’s C source code from SourceForge.
    3. Unzip the file.
    4. Compile the module using the included instructions.
    5. Move mod_gzip.so to your Apache modules folder (typically “/usr/libexec/httpd”).
    6. Edit your server configuration file using a text editor like TextEdit or vim (typically “/etc/httpd/httpd.conf”). Add the following line to your server configuration file as the last loaded module:
      LoadModule gzip_module libexec/mod_gzip.so

      Add the following lines to your server configuration file or to a site’s “.htaccess” 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 handler ^cgi-script$
      mod_gzip_item_include mime ^text/.*
      mod_gzip_item_include mime ^application/x-javascript.*
      mod_gzip_item_exclude mime ^image/.*
      mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
      </IfModule>
  3. Restart Apache.

The “LoadModule” line in the configuration file makes the module ready, while the other lines configure and enable it. Put these other lines in the server’s configuration file to affect all sites served by the web server. Or put them within a site’s “VirtualHost” block or in its own “.htaccess” file to affect only that site.

The “mod_gzip_on” line enables the module. The “mod_gzip_dechunk” line instructs the module to handle bursty (chunked) output from Perl or PHP scripts (such as the Drupal content management system).

The remaining lines tell the module to compress files with .htm, .html, .txt, .css, .js, .php, and .pl file name extensions, the output of CGI scripts, and any output that is text or JavaScript, but not images. The last line tells the module to skip compressing content that is already compressed.

Enable file compression using mod_deflate

The mod_deflate module comes with Apache 2.x. All you need to do is enable it.

  1. Windows:
    1. Log in to your PC using an account with administrator privileges.
    2. Edit your server configuration file using a text editor like NotePad (typically “c:\Program Files\Apache Group\Apache\conf\httpd.conf”). Add the following lines to your server configuration file or to a site’s “.htaccess” file:
      <Location />
      SetOutputFilter DEFLATE
      SetEnvIfNoCase Request_URI \
      \.(?:gif|jpe?g|png)$ no-gzip dont-vary
      SetEnvIfNoCase Request_URI \
      \.(?:exe|t?gz|zip|gz2|sit|rar)$ no-gzip dont-vary
      </Location>
  2. Mac:
    1. Log in to your Mac using an account with administrator privileges.
    2. Edit your server configuration file using a text editor like TextEdit or vim (typically “/etc/httpd/httpd.conf”). Add the following lines to your server configuration file or to a site’s “.htaccess” file:
      <Location />
      SetOutputFilter DEFLATE
      SetEnvIfNoCase Request_URI \
      \.(?:gif|jpe?g|png)$ no-gzip dont-vary
      SetEnvIfNoCase Request_URI \
      \.(?:exe|t?gz|zip|gz2|sit|rar)$ no-gzip dont-vary
      </Location>
  3. Restart Apache.

Put the configuration lines in the server’s configuration file to affect all sites served by the web server. Or put them within a site’s “VirtualHost” block or in its “.htaccess” file to affect only that site.

The “SetOutputFilter” line enables the module.

The next two lines instruct the module to skip compressing image files (.gif, .jpg, .jpeg, .png), executables (.exe), and compressed files (.gz, .tgz, .zip, .gz2, .sit, .rar). Everything else gets compressed.

What does it do?

The mod_gzip and mod_deflate modules both add file compression features to Apache. When enabled and configured, text-based files and script output is automatically compressed before it is sent to the visitor’s browser. While the effort to compress a file slows down the web server, this slow down is easily offset by the time saved to send the much smaller compressed file. This is particularly true when the server or the site visitor have a slow network connection.

Even with file compression enabled and configured, Apache always checks first to see if a visitor’s browser can handle compressed files. Only the oldest web browsers cannot, but if the visitor is using an old browser Apache will skip file compression and send an uncompressed file.

How well does it work?

I benchmarked the effect of file compression on two representative test sites: a simple site with a basic page layout, and a complex site with a complex page layout (see my Specifications for Drupal web site testing). Both sites use the Drupal content management system. Both sites have no other performance improvements (e.g., no PHP script cache, no MySQL query cache, and no Drupal page cache or CSS file aggregation). Load times are for each site’s home page, including HTML, CSS, JavaScript, and images. Tests simulated a web page uploaded over a 64Kbps cable modem, such as that used to serve small web sites from a home or small business.

With file compression, the total file size is reduced by 55-65%.

Total page size

The total page load times improved by 35-40%.

Total page load time

The simple and complex web sites both benefit substantially from file compression. The complex site has a fancier home page that requires more HTML and CSS, which compresses well. But the complex site also requires more server-side effort to build pages using Drupal. The page load times for the complex site are then much higher than for the simple site, and file compression has less impact on a percentage basis on speeding up the site.

When doesn’t it work?

All current web browsers support compression, and these file compression Apache modules work well. They are unlikely to require your further attention once enabled.

According to the mod_gzip documentation, very old web browsers prior to Internet Explorer 4, Netscape 6, and Opera 5 do not properly support compressed files. Fortunately, according to monthly browser statistics reported by W3schools.com, these old browsers dropped below 1% market share way back in 2003. While you can configure the compression modules to handle these browsers specially, it hardly seems worth the effort.

Internet Explorer 5.5 and 6 had a rare problem with compressed JavaScript files. A patch was distibuted as a service pack back in 2002. It is unlikely that unpatched IE 6 browsers remain a significant part of the user base any more.

What else could be used?

PHP has optional obj_gzhandler and zlib output handlers that can automatically compress all data sent from a PHP program. Unfortunately, if the PHP program itself compresses its data (and many of them do), then these output handlers will erroneously compress it a second time. The doubly-compressed data is sent to the visitor’s browser, where it is uncompressed only once, producing the original still-compressed data that then gets displayed as a garbled mess. Since PHP’s output handlers can’t be configured to work properly, make sure that they are disabled by editing your “php.ini” file and setting output_handler to empty and zlib.output_compression to Off. These are the default settings anyway.

Conclusions

Enabling a file compression module in Apache provides a huge 35-40% page load time improvement. No other single configuration change I’ve tested has worked as well. Enabling compression should be the first performance change made to any web site.

Further reading

Comments

CPU utilization impact when using Gzip

Have you measured the CPU utilization impact when using Gzip compression? If so, what were your findings?

Re: CPU utilization impact when using Gzip

Short answer: if your site is efficiently configured, the cost of compression is... zero.

The key is memory caching. Without it, Apache has to process each file request by getting the file from disk and running it through filter modules, such as mod_deflate. This requires disk I/O and compute time. Instead, use a memory cache to store the results after compression. The next time the same file is needed, Apache gets it from the memory cache. No disk I/O. No compression CPU time. The cost of disk I/O and compression is now paid only the first time the file is requested, not every time.

To use Apache memory caching, enable and configure the mod_cache and mod_mem_cache modules. Use Apache 2.2.5 or later. There's a fatal bug in memory caching for earlier versions of Apache 2.

There are other memory cache-style solutions. You can buy hardware that does this, such as a Citrix NetScaler. If you are using the Drupal content management system, it saves compressed pages in its database, and that database can (and should) be configured to cache content in memory. Other content management systems do something similar. You can also use a generic memory cache daemon, like MemCacheD.

If you don't use memory caching, compression is still very cheap. On a 2GHz Intel processor, the time to compress a file is about 0.004 seconds. The time to read the file from disk is larger. So, you could theoretically compress 250 files per second. Sustained, that's about 20 million files per day. And if you're serving that much traffic, the CPU requirements aren't your biggest challenges and you can probably afford to buy another server if they are. :-)

apps development

Hi there i came here by chance cause i was searching for apps development. Thanks for sharing such an informative post. Thanks once again.

Speed up website

Great article! I've been doing a lot of analysis of my own web site and I wrote an entire series on how to speed up your website load time using YSlow. Definitely check it out as it is very useful.

how do i know whether my html is compressed or not?

Hi

I just made my site html compressed enable but how can i check whether the html is compressed or not.

I viewed the html source by using "View Source" and its looks same before and after compression installed (mod_gzip).
I also saved the html code in txt file before and after and the site is same again

thank you
neetesh bhatia
NeeGex

Re: how do i know whether my html is compressed or not?

I use web-sniffer.net. The site provides a web page form into which you type a URL for your site. Check the "Accept-Encoding: gzip" checkbox and click the "Submit" button. The site goes and gets the web page, then shows you the HTTP header it sent, the HTTP response header it got back, and the page's content. If your site is returning compressed content, the response header will include the line "Content-Encoding: gzip". If your site is not returning compressed content, that line will be missing.

The content shown you by a browser's "View source" will always be uncompressed. If you save it to a file, that will be uncompressed too. Decompression always happens automatically within a browser.

Hi,

Hi,

I'm hosting my site on shared server. In other words, i'm client of a hosting company (hostgator.com).
So, how can I execute your advice on "configuration the web server"?
Are there any ways that I can do to complete 3 steps that you have advised on configuration the web server?
Thank you very much for your great articles. They helps me a lot

Jio

Re: Hi, I'm hosting my site on

Hosting companies usually handle web server configuration for you. You'll need to contact them to see what configuration options they provide.

Speed up website

I hope this information will help my site load faster.Will try.Thanks.

I think it will pay for

I think it will pay for itself in reduced bandwidth usage and improved customer satisfaction.

its very good

Very good article thank you...

Good Article

Hey, Its really nice article, helped a lot to understand the apache compression modules. have u written any article on the memcache ?

Nice!

Very good article. I was able to setup Apache 2.0 compression in no time at all following this article.

I have seen a major improvement already!

Thanks,
Cody

Large files and the server's CPU

With this feature enabled, the server can get bogged down by media types that are generally larger in file size: MP3, MP4, FLV, AVI, etc.

Additionally, some media players may even have trouble reading these compressed files.

Just a heads up.

Re: Large files and the server's CPU

Compression is only relevant on text files, such as HTML, CSS, and Javascript. It should always be disabled on binary files, such as MP3s, AVIs, images, zip archives, etc. See the above configuration discussion. Once properly configured, you won't waste CPU time on compressing non-text data.

I think it will pay for

I think it will pay for itself in reduced bandwidth usage and improved customer satisfaction.

My Blog became faster with this technique

Thanks you very much, already provide free tutorial here. now, my blog be faster by applying this tutorial and I will follow the next update .

Great resource

Great resource here... if only my hosting company would allow for file compression :( Least I know it's possible

informative post hai.

Hi there i came here by chance cause i was searching for apps development. Thanks for sharing such an informative post. Thanks once again.

This article clarified my doubts about compression

Thank you.
This article clarified my doubt that compress time may be more than the time for sending original file/page.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options

Nadeau software consulting
Nadeau software consulting