Networking

How to configure Apache mod_deflate to enhance web server performance

Gaining even the smallest performance boost from your web server can be crucial. Jack Wallen shows you how to eek the most out of Apache with the help of mod_deflate compression.

apachehero.jpg
Image: Jack Wallen

If you're looking to supercharge your Apache web server, one way you can do that is by enabling mod_deflate. This particular module allows output from your web server to be compressed before it is sent to the client (browser). With your content compressed, browsers will be able to download it faster.

Faster page serving isn't the only benefit to be had with compression. When search engines evaluate your site, the lower usage of bandwidth found with mod_deflate will be taken into consideration as search engines evaluate your site's performance and page rank. That's a win-win.

But how do you enable mod_deflate? Let me walk you through the process. I'll be demonstrating on Ubuntu Server 17.04, but this can be done for the Apache server running on any Linux distribution (you only need modify the steps, depending upon your distribution).

Checking for the module

Out of the box, mod_deflate should be installed along with Apache. To check, do the following:

  1. Open up a terminal window
  2. Issue the command apachectl -t -D DUMP_MODULES | grep deflate
  3. Check for output as shown in Figure A

Figure A

Figure A

The mod_deflate module is, in fact, installed and ready to go.

If you don't see deflate_module (shared) in the output, you'll need to check and make sure the module has been enabled. To do this, open up the file /etc/apache2/mods-enabled/deflate.load and make sure the following line isn't commented out:

LoadModule deflate_module /usr/lib/apache2/modules/mod_deflate.so

If that line looks like the following, you'll have to remove the # character:

#LoadModule deflate_module /usr/lib/apache2/modules/mod_deflate.so

If you have to edit the file, save and close it and then issue the command sudo apachectl restart to restart Apache.

Configuration

In order to make use of mod_deflate, you have to instruct Apache which file types to compress. I highly recommend configuring the following file types to make use of the compression module:

  • HTML
  • CSS
  • JavaScript

It would be redundant to compress most multimedia files, as they already include as much compression as they can tolerate.

Open up the file /etc/apache2/mods-enabled/deflate.conf. The default configuration is already set up to compress HTML, CSS, xml, rss, and javascript, but it isn't configured for three important options:

  • Compression Level - Specifies the compression level of file between 1 to 9 (1 being the lowest amount of compression)
  • Mem Level - Specifies how much memory should be used by zlib for compression
  • Window Size - Specifies the zlib compression window size

Out of the box (and without configuration) mod_deflate defaults to compression level 9, mem level 9, and window size 15. Although setting these options to their highest values (which they are) may seem like the most efficient use of mod_deflate, depending on the files it will be compressing, the process could wind up using too much of your system resources, thus negatively affecting the web server performance.

To make this the most efficient, we're going to add the following options:

DeflateCompressionLevel 7
​DeflateMemLevel 8
​DeflateWindowSize 10

The above lines will go directly under the the last default statement in the second </IFModule> section. In the case of Apache 2 (on Ubuntu 17.04), the new statements will be placed directly under the line:

AddOutputFilterByType DEFLATE application/xml

Save and close the file and then restart Apache with the command:

sudo apachectl restart

Testing mod_deflate

We're going to test the newly configured module to make sure it is working. On the server you just configured, download the JQuery javascript file with the following steps:

  1. Open a terminal
  2. Change to the document root of the web server with the command cd /var/www/html
  3. Download the .js file with the command sudo wget http://code.jquery.com/jquery-3.2.1.js

The jquery-3.2.1.js file will be 268039 in size.

Go to another Linux machine on your network, open up a terminal window and issue the command:

wget --header="Accept-Encoding: gzip" http://SERVER_IP/jquery-3.2.1.js

Where SERVER_IP is the IP address of your server.

During the download, you should see the filesize will be listed as less than half the size of the original file. In my case (Figure B), the download size was 109.19K.

Figure B

Figure B

The original size of the file was 262K, so we have compression!

That's it, compression is working efficiently.

Compression made simple

Your Apache server is now running an efficient level of compression, without compromising the performance of your server. This is compression made simple that can give your web server a serious boost. Toy around with the configuration options to find out what levels of compression give you the best performance for your Apache server.

Also see

About Jack Wallen

Jack Wallen is an award-winning writer for TechRepublic and Linux.com. He’s an avid promoter of open source and the voice of The Android Expert. For more news about Jack Wallen, visit his website jackwallen.com.

Editor's Picks

Free Newsletters, In your Inbox