How To Install And Configure mod_deflate On CentOS 7

Published on June 12, 2015
How To Install And Configure mod_deflate On CentOS 7


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 CentOS 7. 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 CentOS 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 on CentOS 7.


Mod_deflate is included and enabled in the default Apache installation on CentOS 7. To confirm this run apachectl, the Apache Server Control Interface, and filter the output with grep for the keyword deflate like this:

  1. apachectl -t -D DUMP_MODULES |grep deflate

You should see deflate_module (shared) if mod_deflate is installed and enabled. If you don’t see this, follow these troubleshooting steps:

  1. Ensure that the module file is installed. This file is part of the core httpd package which you should already have installed per the previously mentioned prerequisites. By default, it is found in /etc/httpd/modules/mod_deflate.so. Also, the web server should be able to open this file. For this purpose mod_deflate.so should have world readable permissions such as 755.
  2. Check if the module has been loaded. Open the Apache base modules configuration file /etc/httpd/conf.modules.d/00-base.conf and ensure this line is present and not commented out:
LoadModule deflate_module 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.


To start using mod_deflate you have to specify which file types should be compressed. On one hand, plain text formats can be greatly reduced in size by compression, and that’s why it makes sense to apply it to HTML, CSS, or JavaScript files. On the other hand, many multimedia formats such as Flash and pictures already have compression in them, and additional compression will be futile.

To configure mod_deflate, create a new configuration file /etc/httpd/conf.d/mod_deflate.conf with the sample code:

<filesMatch "\.(js|html|css)$">
    SetOutputFilter DEFLATE

The above code means that when a file matches the extensions .js, .html or .css it will be compressed (deflated) through the standard Apache SetOutputFilter directive. You may add other similar text file extensions found on your site such as .txt.

Note: You could place the above code in the main configuration file /etc/httpd/conf/httpd.conf. However, it is better to separate such specific configuration parts in a different file. For this purpose in CentOS 7 any file with the extension .conf placed in the directory /etc/httpd/conf.d/ is automatically loaded thanks to the directive IncludeOptional conf.d/*.conf at the end of the main Apache configuration file.

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 DeflateMemLevel value 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 configure lower values in your configuration file /etc/httpd/conf.d/mod_deflate.conf like this:

DeflateCompressionLevel 1

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 CentOS 7 Droplet then you can install it with the command sudo yum install -y wget.

For the test you will need a text file of at least a few hundred KBs which should become smaller when compressed. If you don’t have such a file at hand you can download JQuery which is a popular JavaScript library and upload it to your site. If you are not sure where to put the file you can simply upload it to Apache default document root which is /var/www/html/ in CentOS 7. 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:

  1. 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 Accept-Encoding: gzip.

When downloaded in the above manner the file jquery-1.11.3.js should be 83KB. As a matter of fact, this is not exactly the same original JavaScript file, and it should be additionally extracted by the client, causing some overhead on the client side too. However, the size transferred over the network will be only 83KB which is more than three times smaller than the original file (278K).

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:

  1. ls -lah jquery-1.11.3.js
  2. -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 command:

  1. ls -lah /var/www/html/jquery-1.11.3.js
  2. -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.

Learn more about our products

About the authors
Default avatar


Default avatar
Tammy Fox


Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

When I run: $ apachectl -t -D DUMP_MODULES |grep deflate

I get this result:

AH00526: Syntax error on line 9 of /etc/httpd/sites-enabled/xxxxxxxxxx.com-le-ssl.conf: SSLCertificateFile: file ‘/etc/letsencrypt/live/xxxxxxxxx.com/cert.pem’ does not exist or is empty


And that site works and is installed correctly, also SSL.

I also checked installfiles etc for deflate and they are al there.

Hi Anatoliy Dimitrov,

Thank you very much for the detailed post it really helped me out a lot.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel