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.
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.
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:
- apachectl -t -D DUMP_MODULES |grep deflate
apachectl is not in your path, you might have to use the full path to the command:
You should see
deflate_module (shared) if mod_deflate is installed and enabled. If you don’t see this, follow these troubleshooting steps:
apache2package 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.soshould have world readable permissions such as 644.
/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.
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:
The above code means that when a file matches the extensions any popular text file extension such as
.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/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
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
DeflateMemLevelvalue 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:
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.
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.
/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:
- 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
When downloaded in the above manner the file
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:
- ls -lah jquery-1.11.3.js
- -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 -lah /var/www/html/jquery-1.11.3.js
- -rw-r--r-- 1 apache apache 278K Apr 28 12:20 /var/www/html/jquery-1.11.3.js
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.
Click here to Sign up and get $200 of credit to try our products over 60 days!