This will be a quick one folks. The Apache module mod_deflate helps in reducing the size of the information sent to a user, by compressing things prior. It seems to work very well. To test if your site already has mod_deflate installed and working, you can go here:
http://www.whatsmyip.org/http_compression/
To enable mod_deflate on your WHM / cPanel server, make sure you run EasyApache through WHM, and select to install mod_deflate. Once done, you’ll notice that it is not active globally. In your cPanel control panel for your domain, you should notice that under “Software / Services” an icon called “Optimize Website” is now there. This is how you control mod_deflate on a per user basis.
If however, you wish enable it globally, so that all websites on your server can benifit from this, you need to venture back into WHM, and this time go to:
Services Configuration >> Apache Configuration >> Include Editor >> Post VirtualHost Include, and select All Versions
Now, paste the following into this file, and hit Update:
<IfModule mod_deflate.c>
SetOutputFilter DEFLATE
<IfModule mod_setenvif.c>
# Netscape 4.x has some problems…
BrowserMatch ^Mozilla/4 gzip-only-text/html
# Netscape 4.06-4.08 have some more problems
BrowserMatch ^Mozilla/4\.0[678] no-gzip
# MSIE masquerades as Netscape, but it is fine
# BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
# NOTE: Due to a bug in mod_setenvif up to Apache 2.0.48
# the above regex won’t work. You can use the following
# workaround to get the desired effect:
BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
# Don’t compress images
SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary
</IfModule>
<IfModule mod_headers.c>
# Make sure proxies don’t deliver the wrong content
Header append Vary User-Agent env=!dont-vary
</IfModule>
</IfModule>
That’s it, now, go back to the site I gave you at the start, load up your website, and verify that compression is now functioning.
For you command line users, you can achieve this my running the easy apache command line build (/scripts/easyapache), and select to include mod_deflate. Once that is done, you can edit the “/usr/local/apache/conf/includes/post_virtualhost_2.conf” file, and paste the above code into it to achieve the same results. Remember to restart apache once you’ve edited the file.
Thanks very much for that imformative entry.
Great simple article. Saved me a lot of hassle (and bandwidth too!)
Many thanks mate. Server now running mod_deflate for gzip compression.
Nice article. Our of curiosity, I have 14 sites on a server and I want to use the global option, but ONE of those sites I don’t want to have the compression. Is it possible to enable globally and still define certain sites not to use it, or do I have enable manually for each site?
Hi Tony,
I’ve just done some testing, and what you can do is add exclusions to your .htaccess file. So, if you do not want php extensions to be compressed, you would add them to the exclusion list, like so:
SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png|php|)$ no-gzip dont-vary
If you wanted to put in html, you would do it like so:
SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png|php|html|)$ no-gzip dont-vary
Let me know if that works out for you.
Wonderful news Chris. Thank you so much for the article and the update to the comments. I know it’s an older article but it rings true in todays server market also!
Thank you,
Thanks..
I wasn’t aware that I can compress the output. Now I can see a huge drop in page load time for my website.
Thanks once again
Surepb! Nice post! Realy helped!
And this is the reason I love solidservers.ca. Nice post.
This was brought up in class a couple of days ago, will definately share. Thanks!
I was making a research and found out that none of the top host providers offer mod_deflate on shared hosting plans. For some strange reason they prefer to send 70% more data rather than compressing it. So although it is a good method, it is not available to most users even today, Aug 2010! If anybody knows a host who offers that function for decent plan price…
The reasoning behind not enabling it is usually the fact that it does take more CPU power on the servers end to compress the data prior to sending it, so, for those that like to oversell and overcrowd their servers, it makes more sense to leave it off, and then charge more for extra bandwidth usage.
Chris
Thanks mate, Good job
great post, thanks for sharing
Thank you :).
i cannot find the Services Configuration option in my whm. where can i find it.? i also dont have the option “Optimize website” in cpanel
Hello there,
Do you have root access to the server, or reseller access?
Chris
Perfect! Works like a charm! :)
ohh thanks! Man this is sweet especially when used with minify and page/database/object caching.. :D
Perfect! You are da man.
A life saver! Thanks for making it so easy!
Awesome – thanks so much for this, you are a lifesaver. I was wondering why I couldn’t get the mod_deflate stuff to work via htaccess… this is even better to just enable it globally. Hope this helps speed up my website, and increase my page speed a tad.
I enabled this and it works – now how would you recommend implementing compression for css/js files? Google wants these compressed to better pass its speed tests.
Hello there,
The code given will compress everything but images, see this line:
SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary
Chris
Thanks
can you used this to compress images? what would to code be?
It is not recommended to compress images. You should save your images as small as you can get them without sacrificing quality prior to putting them on your website.
Thanks! It worked for me! :)