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:
- Open up a terminal window
- Issue the command apachectl -t -D DUMP_MODULES | grep deflate
- Check for output as shown in Figure A
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.
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:
It would be redundant to compress most multimedia files, as they already include as much compression as they can tolerate.
- 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:
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
- Open a terminal
- Change to the document root of the web server with the command cd /var/www/html
- 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.
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.
Subscribe to the Developer Insider Newsletter
From the hottest programming languages to commentary on the Linux OS, get the developer and open source news and tips you need to know. Delivered Tuesdays and Thursdays