Table of Contents
Mod_deflate is an Apache module that allows the web server to compress files before sending to the client. Once your file is compressed, its size is smaller, and clients are able to download it faster.
Mod_deflate increases the server load but decreases clients' connection times. Using mod_deflate can decrease the response times by up to 50%.
This tutorial explains how to speed up your Apache web server with mod_deflate on CentOS 7
- A server running CentOS v. 7 with Apache installed
- A static IP Address for your server
Mod_deflate is included and enabled in the default Apache installation on CentOS 7.
To verify whether mod_deflate is enabled or not, run the following command:
sudo httpd -M | grep deflate
if mod_deflate is installed and enabled, you should see
The mod_deflate module is loaded by default, but compression will not be enabled until you set up the configuration file.
To configure mod_deflate, you need to create a new configuration file
sudo nano /etc/httpd/conf.d/mod_deflate.conf
Add the following content:
<filesMatch "\.(js|html|txt)$"> SetOutputFilter DEFLATE </filesMatch> DeflateCompressionLevel 7 DeflateMemLevel 8 DeflateWindowSize 10
Save the file and restart Apache to reflect changes.
sudo apachectl restart
With the above configuration, any file with the extensions
.txt will be compressed.
DeflateCompressionLevel : This directive specify the compression level of file. By default, this level is
9. You can set up this level between
DeflateMemLevel : This directive specifies how much memory should be used by zlib for compression. The default value is
9 . You can set up this value between
DeflateWindowSize : This directive specifies the zlib compression window size. The default value is
15. You can set up this value between
15. Higher number means higher compression level, but it will use more server resources.
There are various methods to test mod_deflate with Apache. One way is to use
First of all, you will need a
.js file of at least a few hundred KBs which should become smaller when compressed.
jquery-1.11.3.js and upload it to the Apache default document root which is
/var/www/html/ in CentOS 7.
First, navigate to
sudo cd /var/www/html/
sudo wget http://code.jquery.com/jquery-1.11.3.js
jquery-1.11.3.js file is downloaded, you can download it with
wget. You can perform this test either from your local machine or from a remote machine.
On a remote machine, run the following command:
wget --header="Accept-Encoding: gzip" http://<your_server_ip>/jquery-1.11.3.js
The above command shows that you are downloading the
jquery-1.11.3.js file from your Apache server with gzip compression.
After downloading the
jquery-1.11.3.js file, check the size of the downloaded file
du -hs jquery-1.11.3.js
Then you can compare the result with the original file on your Apache server again with the
du -hs command:
sudo du -hs /var/www/html/jquery-1.11.3.js
You should see that the size of the
jquery-1.11.3.js transferred over the network will be only
112KB which is considerably smaller than the original
(280K) on your Apache server.