Configure Apache's mod_deflate to Improve Website Performance

Leave your reply

Learn how to configure the Apache module mod_deflate to compress files before sending them to a visitor's browser. This will reduce the file size, in exchange for a slight increase in the server's CPU usage. The smaller files mean that your website uses less bandwidth, which improves the file transfer time, and allows your website to render faster. This is particularly useful when tuning your website's SEO, and for visitors who are on a low-bandwidth connection.

Requirements

  • A Cloud Server running CentOS 7 or Ubuntu 16.04.
  • Apache web server installed and running.
  • mod_deflate installed.

The mod_deflate module is installed by default on most contemporary Apache installations. To check if the module is installed, use the command:

sudo apachectl -t -D DUMP_MODULES | grep deflate

If the module is installed, the command will return:

user@localhost:~# sudo apachectl -t -D DUMP_MODULES | grep deflate
deflate_module (shared)

Configure mod_deflate

Open the main Apache configuration file for editing with the command:

  • CentOS 7:sudo nano /etc/httpd/conf/httpd.conf
  • Ubuntu 16.04:sudo nano /etc/apache2/apache2.conf

Scroll to the bottom of the file and add the lines:

#
# mod_deflate configuration 
# 
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css

This configuration will compress HTML, plain text, XML, and CSS files. These are the file types which have the most to gain from compression with mod_deflate. You can add other file types to this line if you wish to compress them as well.

Save and exit the file. Then restart Apache for the changes to take effect:

  • CentOS 7:sudo systemctl restart httpd
  • Ubuntu 16.04:sudo systemctl restart apache2

Test the Compression

There are several ways to verify that the file compression is taking place. The simplest method is to use the free compression test at WhatsMyIp.org.

If you need to create a page to test, you can use this Lorem Ipsum text generator to generate several paragraphs of text. Paste these into a basic HTML file, upload it to your server, then check the file in the WhatsMyIp.org tool.

As an example, a test with a page consisting of 5 paragraphs of Lorem Ipsum text resulted in a compression from 3.3kb down to 1.4kb, with a savings of 58%.