How to enable gzip compression on Apache

mod_deflate came with 2nd generation of Apache web-server. It's an effective replacement of its predecessor – mod_gzip module that was used in Apache 1.3.xx.

Why choose mod_deflate module?

That's a reasonable question since there are plenty of alternatives. For example, additional plugins for popular CMS and different solutions based on mod_rewrite module. But when you use additional PHP plugins it greatly increases system resourses consumpltion and using mod_rewrite module that returns compressed objects instead of original ones is simply inconvinient. Mod_deflate module doesn't have these disatvantages and is easy to configure. Besides, it provides the great balance in performance and compression level on one hand, and moderate system resourses usage on the other.

Results of site analysis before activating gzip compression with mod_deflate

During the analysis of parameters of the site with Google Page Speed I noticed that Enable zip compression test resulted not in the best way. All CSS and javascript files are being returned uncompressed. For example:

Resource transfer table before enabling gzip compression

In this fragment of Chrome page inspector you can see that javascript file which have size of 106.78 Kb is being transferred uncompressed. You can see that Content-Length header and the value in Size/Content are the same as the size of the file. Also Content-Encoding header is absent. The situation was the same for all CSS and javascript files.

Gzip compression with mod_deflate module: activation and configuring

To make apache web server compress all CSS and javascript files by using mod_deflate module you need to add these strings to .htaccess or to httpd.conf or vhost.conf file depending on web-hosting configuration. File .htaccess can be found in the root folder of the site.

<IfModule mod_mime.c>
 AddType application/x-javascript .js
 AddType text/css .css
</IfModule>
<IfModule mod_deflate.c>
 AddOutputFilterByType DEFLATE text/css application/x-javascript text/x-component text/html text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon application/javascript
 <IfModule mod_setenvif.c>
  BrowserMatch ^Mozilla/4 gzip-only-text/html
  BrowserMatch ^Mozilla/4\.0[678] no-gzip
  BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
 </IfModule>
 <IfModule mod_headers.c>
  Header append Vary User-Agent env=!dont-vary
 </IfModule>
</IfModule>

This fragment of apache web server configuration file is checking on mod_deflate module and if mod_deflate module is available it activates compression for CSS and javascript files. It also checks on mod_setenvif module and if its available changes compression parameters for browsers which are processing compressed files incorrectly.

Some guides recommend to activate compression of all files except for files that can't be compressed like archives, images, multimedia files, etc. With this aproach you should replace line 6 with these:

SetOutputFilter DEFLATE
 <IfModule mod_setenvif.c>
  SetEnvIfNoCase Request_URI \.(?:rar|zip)$ no-gzip dont-vary
  SetEnvIfNoCase Request_URI \.(?:gif|jpg|png)$ no-gzip dont-vary
  SetEnvIfNoCase Request_URI \.(?:avi|mov|mp4)$ no-gzip dont-vary
  SetEnvIfNoCase Request_URI \.mp3$ no-gzip dont-vary
 </IfModule>

This fragment of Apache web server configuration file activates compressing of all files and with mod_sensitif module active forbids compressing of archives with .rar and .zip extensions, images with .gif, .jpg and .png extensions, videofiles with .avi, .mov and .mp4 extensions and audiofiles with .mp3 extension. I'm not fond of this approach since it can affect much more objects than required.
There are more directives for subtle configuring of mod_deflate module. But they can be used only in httpd.conf/vhost.conf files which arent available on most hostings. But for most existing sites the directives described in this article will be more than enough.

Results of site analysis after activating gzip compression with mod_deflate module

After activating of mod_deflate module Enable gzip compression test is marked as successful. All CSS and javascript files are being compressed by the server. As for the file mentioned above you can see this:

Resource transfer table after enabling gzip compression

In this fragment of Chrome page inspector the Size value is reduced by three times and Content-Encoding header is here informing that the file is compressed by gzip method. It's the same for all CSS and javascript files. They are now compressed and have two of four times less size than original size.

Afterword

if you use Apache 2.x on your server I strongly recommend taking mod_deflate module into consideration. It allows to reduce the traffic generated by your site at least three times and to increase page loading speed of your site just that much.

The next step you should make is to leverage browser caching in Apache.







pranav
2012-11-24 11:44:11
just awesomeness!!! thanks alot :) keep it up man!!
nik
2013-09-04 17:57:09
There is a problem with compression. Essential for all your recommendations, but the result was negative. On the server mod_deflate works, but mod_headers is not. I'm using WordPress. Thank you for your help.
Sergey
2013-09-04 18:23:05
Then just install it, what is the problem? :)
nik
2013-09-05 10:26:43
Communicated with the server administrator, included mod_headers, mod_filter and mod_expires, but compression and still does not work. I can not understand why??? HTML compression works even without any .htaccess or plugins, but CSS/JS compression won't work. In my phpinfo(): HTTP_ACCEPT_ENCODING gzip,deflate,sdch Accept-Encoding gzip,deflate,sdch Thank you for response.
Sergey
2013-09-05 10:35:00
Probably lack of mod_mime. phpinfo is meaningless here, it's done on http server level. If you have an SSH access to your server, you can check which apache modules are installed and activated by this command: httpd -t -D DUMP_MODULES
sanjay
2014-06-30 07:22:41
How To Enable Gzip Compression Centos...........http://goo.gl/2SlUpS
Priya Sharma
2014-06-30 07:24:18
How To Enable Gzip Compression Centos...........http://mytecharticle.com/?p=1598
arun
2014-07-05 15:50:30
GZip compression is working in localhost but it's not working on server side please help me.
arun
2014-07-05 15:51:35
Gzip is working in localhost but it's not working on apache server 2.2 side please help me.

Got a comment?
Name (optional):
Comment
Anti-Bot:captcha =
 
 
Copyright © 2012-2013 HowToUnix - *nix Howtos and Tutorials
All Rights Reserved.