Posts Tagged ‘Speeding up your Apache hosted website through mod_deflate on Debian / How to compress css and (js) javascript’

Speeding up your Apache hosted website through mod_deflate on Debian / How to compress css and (js) javascript

Tuesday, August 17th, 2010

People who run websites which aim at top responce times, are quite familiar with mod gzip and mod deflate .
Both Apache modules mod deflate and mod gzip are used for the very same purpose. They add a standard gzip compression to the files that Apache transfer to your Webserver clients.

Its said mod_gzip is the more powerful and more configurable module, however most people nowadays stick to the more straigh forward and easier to set up mod_deflate

I personally also adhere to mod_deflate since it’s documentation on Apache’s website is quite complete.

In this article I’m gonna explain you how I quickly added support for mod_deflate gzip compression to a Webserver I’ve recently had to configure for a website.

Doing so reduced the access time to the website from 6-7 seconds as reported by Yahoo’s Yslow to 3-4 secs.

These days people are constantly looking for a ways to improve their web site opening times in order to improve the end user experience, so the reduce to about 3 seconds in a website access time is quite a good achievement.

Some time ago I’ve blogged about How to enable http gzip compression on CentOS 5.x to speed up Apache

However I’ve realized I haven’t blogged how the same thing is achieved on a Debian Linux so therefore I decided it might be interesting to somebody out there to explore how same thing is achieved on Apache server running on top of Debian.
Anyways enough jabberish, here is the exact steps I took on a Debian Lenny server to configure properly an Apache 2.x webserver gzip compression through the mod_deflate module.

1. Open up /etc/apache2/mods-available/deflate.conf and place the following configuration directives within the file:

<IfModule mod_deflate.c>
# AddOutputFilterByType DEFLATE text/html text/plain text/xml
AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml application/xhtml+xml application/rss+xml application/json text/css text/javascript application/javascript application/x-javascript
# Insert filter
#SetOutputFilter DEFLATE

# Netscape 4.x has some problems
BrowserMatch ^Mozilla/4 gzip-only-text/html

# Netscape 4.06-4.08 have some more problems
BrowserMatch ^Mozilla/4.0[678] no-gzip

# MSIE masquerades as Netscape, but it is fine
BrowserMatch bMSIE !no-gzip !gzip-only-text/html

# Don’t compress images
SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary

# Don’t compress already compressed stuff !
SetEnvIfNoCase Request_URI .(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI .pdf$ no-gzip dont-vary

# Make sure proxies don’t deliver the wrong content
Header append Vary User-Agent env=!dont-vary

</IfModule>

Note that above configuration has an extra configuration directives to AddOutputFilterByType DEFLATE which are gonna instruct it to also gzip compress content of the CSS and Javascript (js) files as well.

2. Shut off Etags in /etc/apache2/apache2.conf the quickest way to do it would be:

debian-server:~# echo "FileEtag none" >> /etc/apache2/apache2.conf

3. Enable mod_deflate in your Apache server

debian-server:~# ln -sf /etc/apache2/mods-available/deflate.conf /etc/apache2/mods-enabled/deflate.conf
debian-server:~# ln -sf /etc/apache2/mods-available/deflate.load /etc/apache2/mods-enabled/deflate.load

Now you need to restart your Apache server for the changes to take affect.

debian-server:~# /etc/init.d/apache2 restart

This changes are also about to include the common suggestion by Yahoo Yslow that warns about the following message:
Grade D on Compress components with gzip

There are x plain text components that should be sent compressed

Thus the aforementioned changes to your Apache would guarantee you also a good compression status in Yslow web site slowness test.
After the changes the returned message by Yslow would be:

Grade A on Compress components with gzip

By the way it took me quite a lot of time to search for a solution to the annoying yslow issue where the .css and .js scripts failed to be compressed with gzip nevertheless the mod_deflate module was active.
I therefore hope this post will help somebody else issuing the Yslow css and javascript not being compressed issues to get solved.