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:
- 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:
- Ensure that the module file is installed. This file is part of the core
httpdpackage 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.soshould have world readable permissions such as 755.
- Check if the module has been loaded. Open the Apache base modules configuration file
/etc/httpd/conf.modules.d/00-base.confand 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 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 </filesMatch>
The above code means that when a file matches the extensions
.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
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
DeflateMemLevelvalue 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:
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.
/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:
- 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
When downloaded in the above manner the file
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:
- ls -lah jquery-1.11.3.js
- -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 -lah /var/www/html/jquery-1.11.3.js
- -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.