Compressing web pages with mod_deflate


Browse by products and services

  • Applies to: VPS Hosting
    • Difficulty: Medium
    • Time Needed: 20
    • Tools Required: SSH, vi knowledge

Overview

The mod_deflate module allows the Apache2 web service to compress files and deliver them to clients (browsers) that can handle them. With mod_deflate you can compress HTML, text or XML files by up to 70% of their original sizes. Thus, saving you server traffic and speeding up page loads.

mod_deflate is specific to Apache. This means that if you're using a different HTTP server, this module will not work. For instance, all Plesk versions 11.0 and above use Nginx by default. There is an article located here for activating gzip compression for Nginx.

NOTE:

  • Compressing files will increase load on your server, but it is a small tradeoff considering your client's connection times will decrease significantly.
  • This will not exclude users with older browsers that cannot handle compressed content. The browser negotiates with the server before any file is transferred, and if the browser does not have the capability to handle compressed content, the server delivers the files uncompressed.
  • mod_deflate has replaced Apache 1.3's mod_gzip in Apache2.
  • This article shows how to enable mod_deflate globally across all the domains on your VPS server. Should you only wish to enable for a single domain you'd need to add the AddOutputFilterByType and BrowserMatch rules below to the VirtualHost section in your configuration.

Requirements

READ ME FIRST

This article is provided as a courtesy. Installing, configuring, and troubleshooting third-party applications is outside the scope of support provided by (mt) Media Temple. Please take a moment to review the Statement of Support.

Instructions

  1. Make sure that mod_deflate is enabled on your server. Your DV server should have it enabled by default. If the following command returns a line with a "#" at the beginning you will have to remove(uncomment) the "#" character using vi or the perl command below:
    grep 'mod_deflate' /etc/httpd/conf/httpd.conf

    If the result was LoadModule deflate_module modules/mod_deflate.so, you can continue. If the result was #LoadModule deflate_module modules/mod_deflate.so, please run the following command:

    perl -pi -e 's/# LoadModule mod_deflate/LoadModule mod_deflate/g' /etc/httpd/conf/httpd.conf
  2. Now that we have verified that mod_deflate is in fact being loaded by Apache, we must configure the module. First, backup your existing conf file in case you need to revert back:
    cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.bak 
  3. Using vi, we will edit our Apache conf file and add the following lines at the very bottom of the file, making sure they are separated from any other configurations:
    vi /etc/httpd/conf/httpd.conf
     #
     # Deflate output configuration
     #
     AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css
     BrowserMatch ^Mozilla/4 gzip-only-text/html
     BrowserMatch ^Mozilla/4\.0[678] no-gzip
     BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
  4. Save your file. Now, we need to check that Apache likes our changes using the apachectl command:
    /usr/sbin/apachectl -t
  5. You should see a Syntax OK message. If not, please check your file to make sure you modified the file correctly. Next, we need to restart apache using the same apachectl command:
    /usr/sbin/apachectl graceful

You have now enabled mod_deflate on your DV server! You can see whether the changes are in effect using the following tool at http://whatsmyip.org/mod_gzip_test/.

Here is a quick before and after for a default Plesk index page for a domain. You can clearly see how this can benefit sites with rich content.

BEFORE:

AFTER:

Resources