How to enable mod_deflate globally in cPanel/WHM

Posted by: Chris  :  Category: Linux Tips

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:

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 mod_headers.c>
# Make sure proxies don’t deliver the wrong content
Header append Vary User-Agent env=!dont-vary

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. Optimize website globally? - cPanel Forums Says:

    […] […]

  2. mcdonalds coupons Says:

    Thanks very much for that imformative entry.

  3. Why isn't mod_deflate running? - cPanel Forums Says:

    […] […]

  4. Win Says:

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

  5. Great Post Says:

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

  6. 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?

  7. 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.

  8. 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,

  9. Abhik Says:

    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

  10. FruT Says:

    Surepb! Nice post! Realy helped!

  11. Clara Says:

    And this is the reason I love Nice post.

  12. bropass Says:

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

  13. 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…

  14. 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.


  15. Disable Optimize website (mod_deflate) on a per user basis - cPanel Forums Says:

    […] […]

  16. Simple php guide Says:

    Thanks mate, Good job

  17. Daniel Says:

    great post, thanks for sharing

  18. RT Says:

    Thank you :).

  19. 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

  20. Chris Says:

    Hello there,

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


  21. Matan Uberstein Says:

    Perfect! Works like a charm! :)

  22. blackjack Says:

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

  23. Brad Says:

    Perfect! You are da man.

  24. Pavel Says:

    A life saver! Thanks for making it so easy!

  25. 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.

  26. 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.

  27. 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


  28. Sumit Bhatia Says:


  29. Scott Says:

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

  30. 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.

  31. jimmy k oak Says:

    Thanks! It worked for me! :)

Leave a Reply