Tutorials

How to Configure Mod_deflate for Apache on CentOS 7

Table of Contents

Introduction

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.

You can compress HTML, XML, CSS, text, and JavaScript files by up to 70% of their original sizes using mod_deflate. This will save your server traffic and speed up page loads.

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

Requirements

  • A server running CentOS v. 7 with Apache installed
  • A static IP Address for your server

Installation

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 deflate_module (shared).

Configure mod_deflate

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 /etc/httpd/conf.d/mod_deflate.conf.

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 .js, .html, or .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 1 to 9.

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 1 to 9.

DeflateWindowSize : This directive specifies the zlib compression window size. The default value is 15. You can set up this value between 1 to 15. Higher number means higher compression level, but it will use more server resources.

Test mod_deflate

There are various methods to test mod_deflate with Apache. One way is to use wget.

First of all, you will need a .js file of at least a few hundred KBs which should become smaller when compressed.

As an example, download JavaScript library file 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 /var/www/html/ directory.

sudo cd /var/www/html/

Download the jquery-1.11.3.js file:

sudo wget http://code.jquery.com/jquery-1.11.3.js

Once the 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

Checking the compressed file size

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

Compare the server-side file size

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 jquery-1.11.3.js file (280K) on your Apache server.

Enjoy.....

 
  • If you are here to know about all those ways to fix connections to bluetooth audio devices and wireless displays in windows 10 so this portal will be one of the suitable option for you to get rid of all your hurdles and will also help you to increase your knowledge.

  • After standing hit with cosmic flashes, Johnny converted into a rock-like monster, comparable to the way the Thing viewed in Fantastic Four #2, all thick and such. assignment writing service When the partners responded to a lab in Midtown, Johnny noticed that Midtown was no great a position for them.

Log In, Add a Comment