mod_deflate is an Apache module that enables your Apache server to compress files & data using gzip compression before it is sent to browsers. This not only improves page load speed but also reduces network bandwidth consumption. Let us look at how to install mod_deflate in CentOS.
How to Install mod_deflate in CentOS
Here are the steps to install mod_deflate in CentOS. Before we proceed, please ensure that you have installed Apache on CentOS.
1. Install mod_deflate
By default, mod_deflate is already installed during installation of Apache web server. It is also enabled by default.
You can verify it with the command
$ sudo httpd -M | grep deflate
If mod_deflate is enabled, you will see something like
2. Configure mod_deflate
Although mod_deflate is enabled by default, compression will not be enabled unless you set up its configuration file.
Open configuration file of mod_deflate in a text editor
$ sudo nano /etc/httpd/conf.d/mod_deflate.conf
Add the following lines:
<filesMatch "\.(js|html|css|txt)$"> SetOutputFilter DEFLATE </filesMatch> DeflateCompressionLevel 7 DeflateMemLevel 8 DeflateWindowSize 10
Save and close the file.
The above lines of code instruct Apache to compress only html, js, cssand text files. It also sets 3 important server directives:
- DeflateCompressionLevel – for setting compression level. Values can be 1-9. Default value is 9
- DeflateMemLevel – for setting memory to be allocated for zlib compression. Values can be 1-9. Default value is 9
- DeflateWindowSize- – for setting zlib compression window size. Values can be 1-15. Default value is 15
By default, these 3 directives are set to their maximum values. This makes compression very resource-intensive and negatively affects server performance. That is why, we set them to more reasonable values.
3. Restart Apache Server
Restart Apache server to apply changes
$ sudo apachectl restart
4. Test mod_deflate
You can use online third-party tools to check if mod_deflate is enabled on your website or not. You just have to enter the URL of your website and it will tell you whether your website serves compressed content or not.
That’s it! You have installed mod_deflate in CentOS.