How to configure Apache mod_deflate to improve web server performance

Getting even the smallest performance improvement from your web server can be crucial. Jack Wallen shows you how to get the most out of Apache using mod_deflate compression.

Image: Jack Wallen

If you are looking to overload your Apache web server, you can do so by enabling mod_deflate. This particular module allows you to compress the output of your web server before sending it to the client (browser). With your content compressed, browsers will be able to download it faster.

Faster page delivery isn’t the only benefit of compression. When search engines evaluate your site, the low bandwidth usage found with mod_deflate will be taken into account as search engines evaluate your site’s performance and rank. It’s a win-win.

But how do you activate mod_deflate? Let me walk you through the process. I will demonstrate on Ubuntu Server 17.04, but it can be done for the Apache server running on any Linux distribution (you just need to modify the steps, depending on your distribution).

Module verification

Out of the box, mod_deflate must be installed with Apache. To verify, follow these steps:

  1. Open a terminal window
  2. Issue the command apachectl -t -D MODULES_DUMP | grep deflate
  3. Check the 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 will need to check and make sure the module has been enabled. To do this, open the file /etc/apache2/mods-enabled/deflate.load and make sure the following line is uncommented:

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

If this line looks like the following, you will need to remove the # character:

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

If you need to edit the file, save and close it, then run the command reboot sudo apachectl to restart Apache.

Configuration

To use mod_deflate, you need to tell Apache what types of files to compress. I highly recommend configuring the following file types to use the squeeze module:

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

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

  • Compression level – Specifies the level of compression of the file between 1 and 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 size of the zlib compression window

Out of the box (and no configuration) mod_deflate defaults to compression level 9, memory level 9, and window size 15. Although setting these options to their highest values ​​(which ‘they are) might seem like the most use of the files it will compress, the process might end up using too much system resources, thus negatively affecting the performance of the web server.

To make this as efficient as possible, we will add the following options:

DeflateCompressionLevel 7
​DeflateMemLevel 8
​DeflateWindowSize 10

The above lines will go directly below the last default statement in the second section . In the case of Apache 2 (on Ubuntu 17.04), the new instructions will be placed directly below the line:

AddOutputFilterByType DEFLATE application/xml

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

sudo apachectl restart

Test mod_deflate

We will test the newly configured module to make sure it works. On the server you just configured, download the JQuery javascript file by doing the following:

  1. Open a terminal
  2. Change to the root of the web server document 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 have a size of 268039.

Go to another Linux machine on your network, open a terminal window, and run 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.

While downloading, you should see that the file size will be listed as less than half of the original file size. In my case (Number B), the download size was 109.19K.

Number B

Number B

The original file size was 262KB, so we have compression!

That’s it, the compression works efficiently.

Compression made easy

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

Also look


Source link

Comments are closed.