Enable Compression on Apache with mod_deflate

Enabling compression on Apache is a trade-off between CPU load and bandwidth. It is usually faster to compress content as more time is saved when transferring a smaller file as opposed to the minimum CPU time which is required to compress the data in the first place.

We first need to enable mod_deflate. This is the module which handles the compression of the data.

On modern Linux versions with Apache 2:

a2enmod deflate

On Windows edit the "httpd.conf" file and uncomment the following line like so:

LoadModule deflate_module modules/mod_deflate.so

Once you have done that it is a matter of adding the following lines to your "httpd.conf" or a ".htaccess" file:

# add these accordingly for the MIME types to be compressed
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript

You can also compress by file extension. Add the following lines as you did above taking care to replace the extension as necessary:

<Files *.htm>
SetOutputFilter DEFLATE
</Files>

You will now start to save bandwidth and lower the page loading time for your users. It is a true win-win situation. Not all web hosting providers have mod_deflate enabled, so contact them first if you plan to use compression. As you just saw, it is very easy to add compression to Apache.

We hope this Apache configuration tutorial was helpful.

Thank Tutorial Arena for This Tutorial.
Show your appreciation with a +1...