break
1 Star2 Stars3 Stars4 Stars5 Stars (6 votes, average: 4.67 out of 5)
Loading...

How to enable mod_deflate globally in cPanel/WHM

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.

31 responses to “How to enable mod_deflate globally in cPanel/WHM”

  1. Thanks very much for that imformative entry.

  2. Win says:

    Great simple article. Saved me a lot of hassle (and bandwidth too!)

  3. Great Post says:

    Many thanks mate. Server now running mod_deflate for gzip compression.

  4. Tony says:

    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?

  5. Chris says:

    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.

  6. Tony says:

    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,

  7. Abhik says:

    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

  8. FruT says:

    Surepb! Nice post! Realy helped!

  9. Clara says:

    And this is the reason I love solidservers.ca. Nice post.

  10. bropass says:

    This was brought up in class a couple of days ago, will definately share. Thanks!

  11. pavel says:

    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…

  12. Chris says:

    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

  13. Simple php guide says:

    Thanks mate, Good job

  14. Daniel says:

    great post, thanks for sharing

  15. RT says:

    Thank you :).

  16. Avinash says:

    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

  17. Chris says:

    Hello there,

    Do you have root access to the server, or reseller access?

    Chris

  18. Perfect! Works like a charm! :)

  19. blackjack says:

    ohh thanks! Man this is sweet especially when used with minify and page/database/object caching.. :D

  20. Brad says:

    Perfect! You are da man.

  21. Pavel says:

    A life saver! Thanks for making it so easy!

  22. Tedd says:

    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.

  23. Big D says:

    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.

  24. Chris says:

    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

  25. Sumit Bhatia says:

    Thanks

  26. Scott says:

    can you used this to compress images? what would to code be?

  27. Chris says:

    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.

  28. jimmy k oak says:

    Thanks! It worked for me! :)

Leave a Reply

Your email address will not be published. Required fields are marked *