Tutorial

How To Install And Configure mod_deflate On Ubuntu 14.04

Published on June 17, 2015
Default avatar

By Toli

How To Install And Configure mod_deflate On Ubuntu 14.04

Introduction

Mod_deflate is an Apache module which allows output from your web server to be compressed before being sent to the client. Once the size of your site content is compressed, its size is smaller, and clients are able to download it faster. This is valuable not only for clients with lower bandwidth, but it is also taken into consideration by search engines when evaluating your site performance and its page rank.

In addition to compressing content, mod_deflate can be also used for uncompressing purposes. This technique would be applicable if you use Apache as a reverse proxy and you wish to process further the content which passes through the proxy. However, this technique has a very limited use. We will keep the focus of the article on using mod_deflate for compression.

Prerequisites

This guide has been tested on Ubuntu 14.04. For CentOS 7 please check this guide. The module installation and configuration is not OS or OS version dependent, but the location of the configuration files may vary on the different OS and their versions.

It also assumes you are running Apache 2.4.0 or greater. To install Apache please follow Step #1 of the How To Install Linux, Apache, MySQL, PHP (LAMP) stack on Ubuntu 14.04 article.

All the commands in this tutorial should be run as a non-root user. If root access is required for the command, it will be preceded by sudo. If you don’t already have that set up, follow this tutorial: Initial Server Setup with Ubuntu 14.04.

Installation

Mod_deflate is included and enabled in the default Apache installation on Ubuntu 14.04. To confirm this run apachectl, the Apache Server Control Interface, and filter the output with grep for the keyword deflate like this:

  1. apachectl -t -D DUMP_MODULES |grep deflate

**Note:**If apachectl is not in your path, you might have to use the full path to the command: /usr/sbin/apachectl.

You should see deflate_module (shared) if mod_deflate is installed and enabled. If you don’t see this, follow these troubleshooting steps:

  1. Ensure that the module file is installed. This file is part of the core apache2 package which you should already have installed per the previously mentioned prerequisites. By default, it is found in /usr/lib/apache2/modules/mod_deflate.so. Also, the web server should be able to open this file. For this purpose mod_deflate.so should have world readable permissions such as 644.
  2. Check if the module has been loaded. Open the module configuration file /etc/apache2/mods-enabled/deflate.load , which is a symlink to /etc/apache2/mods-available/deflate.load, and ensure this line is present and not commented out:
LoadModule deflate_module /usr/lib/apache2/modules/mod_deflate.so

Note: Don’t forget to restart Apache if you have had to make a change in the Apache configuration. The restart command is sudo apachectl restart.

Configuration

To start using mod_deflate you have to specify which file types should be compressed. On one hand, plain text formats can be greatly reduced in size by compression, and that’s why it makes sense to apply it to HTML, CSS, or JavaScript files. On the other hand, many multimedia formats such as Flash and pictures already have compression in them, and additional compression will be futile.

As a start you can use the default configuration in the file /etc/apache2/mods-enabled/deflate.conf, a symlink to /etc/apache2/mods-available/deflate.conf, which is:

/etc/apache2/mods-enabled/deflate.conf
<IfModule mod_deflate.c>
        <IfModule mod_filter.c>
                # these are known to be safe with MSIE 6
                AddOutputFilterByType DEFLATE text/html text/plain text/xml

                # everything else may cause problems with MSIE 6
                AddOutputFilterByType DEFLATE text/css
                AddOutputFilterByType DEFLATE application/x-javascript application/javascript application/ecmascript
                AddOutputFilterByType DEFLATE application/rss+xml
                AddOutputFilterByType DEFLATE application/xml
        </IfModule>
</IfModule>

The above code means that when a file matches the extensions any popular text file extension such as .html, .txt, .xml, .css, .js and etc. it will be compressed by the deflate action of mod_deflate. If you are not sure how the extensions are mapped by mime types check the file /etc/mime.types. You may wish to add more extensions specific to your site.

<$> Note: In Ubuntu the Apache configuration is organized intuitively in multiple files. Also, configuration files are usually not enabled. For example, the modules configuration files reside in the directory /etc/apache2/mods-available/, e.g. /etc/apache2/mods-available/deflate.conf. A module configuration file is enabled only when a symlink to it is created in the directory /etc/apache2/mods-enabled/ such as /etc/apache2/mods-enabled/deflate.conf. <$>

Furthermore, mod_deflate has a few of its own important configuration options:

  • DeflateCompressionLevel — The compression level to be applied. By default, this level is 9, the highest level of compression. 1 is the least level of compression. Higher compression would makes the smallest output at the price of higher server CPU usage.
  • DeflateMemLevel — The amount of memory zlib the compressing library can use. The default value is 9 which is also the highest value. To calculate precisely the allowed memory you should multiply the DeflateMemLevel value by 16K.
  • DeflateWindowSize — The compression window size. By default, it’s the highest possible value of 15. Higher number means higher compression level, again at the price of more server resources.

In most cases you can leave the above values to their defaults. However, if you suspect your server performance has worsened significantly after using mod_deflate, you can configure lower values in your configuration file /etc/apache2/mods-enabled/deflate.conf like this:

DeflateCompressionLevel 1

The above will decrease the compression level which will result in making the files larger. However, mod_deflate will use less CPU this way. Make sure to restart Apache if you decide to apply any such changes.

Testing

There are various ways to test mod_deflate but the easiest is by using wget, the non-interactive network downloader. If you don’t have it already on your Ubuntu 14.04 Droplet then you can install it with the command sudo apt-get install wget.

For the test you will need a text file of at least a few hundred KBs which should become smaller when compressed. If you don’t have such a file at hand you can download JQuery which is a popular JavaScript library and upload it to your site. If you are not sure where to put the file you can simply upload it to Apache default document root which is /var/www/html/ in Ubuntu 14.04. Thus the file will be available at the root directory of your default site.

<$> Note: In order for mod_deflate to compresses the output the client (usually the browser) has to support compression. If the client does not support compression the file will be sent as is. <$>

Once the example test file is uploaded to your site download it with wget. You can perform this test either from your local machine or from the Droplet. like this:

  1. wget --header="Accept-Encoding: gzip" http://<your_server_ip>/jquery-1.11.3.js

In the above example you are downloading the jquery-1.11.3.js file from your Apache server. To make use of compression we pass the extra wget header argument Accept-Encoding: gzip.

When downloaded in the above manner the file jquery-1.11.3.js should be 83KB. As a matter of fact, this is not exactly the same original JavaScript file, and it should be additionally extracted by the client, causing some overhead on the client side too. However, the size transferred over the network will be only 83KB which is more than three times smaller than the original file (278K).

You can confirm the above calculation first by checking the size of the downloaded file with the Linux command for listing the directory contents ls like this:

  1. ls -lah jquery-1.11.3.js
  2. -rw-r--r-- 1 user user 83K Apr 28 12:20 jquery-1.11.3.js

Then you can compare the result with the original file on your site again with the ls command:

  1. ls -lah /var/www/html/jquery-1.11.3.js
  2. -rw-r--r-- 1 apache apache 278K Apr 28 12:20 /var/www/html/jquery-1.11.3.js

Conclusion

As you have seen from this article and real life example with the JQuery library, Mod_deflate can help you significantly decrease the bandwidth needs for your site. To continue further with the optimization of your site read about Apache content caching, which is the next logical step after enabling mod_deflate.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors
Default avatar
Toli

author


Default avatar
Tammy Fox

editor


Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
2 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Thank you for this detailed explanation :-) it’s very helpful ! In the previous file mod_deflate.conf, there were the tag <Location /> To configure the deflate mode, I have to add the code below :

<Location />
        # Insert filter
        SetOutputFilter DEFLATE
        # Netscape 4.x encounters some problems ...
        BrowserMatch ^Mozilla/4 gzip-only-text/html
        # Netscape 4.06-4.08 encounter even more problems
        BrowserMatch ^Mozilla/4\.0[678] no-gzip
        # MSIE pretends it is Netscape, but all is well
        BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
        # Do not compress images
        SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary
        # Ensure that proxy servers deliver the right content
        Header append Vary User-Agent env=!dont-vary
</Location>

Should I keep the tag <Location /> ? Where I have to paste this code exactly in the new file deflate.conf ?

Thank You so much…it was very very helpful. :)

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel