Enable mod_deflate on a cPanel/WHM web server to compress your pages before they are sent, so your sites load faster. Install it with EasyApache, then switch it on per site or for the whole server.

The Apache module mod_deflate reduces the size of the information sent to a visitor by compressing it first. It works very well.

Before you enable mod_deflate: test if compression is already on

To test if your site already has mod_deflate installed and working, go to http://www.whatsmyip.org/http_compression/ and enter your website.

How to enable mod_deflate on a cPanel/WHM web server

  1. In WHM, run EasyApache and select to install mod_deflate.
  2. Once done, you'll notice that it is not active globally yet.
  3. In the cPanel control panel for your domain, you should now see an icon called Optimize Website under Software / Services. This is how you control mod_deflate on a per user basis.

In current cPanel versions, Optimize Website sits in the Software section. Choose Compress All Content (or only the MIME types you list) and press Update Settings.

Enable mod_deflate for every site on the server

If you wish to enable it globally, so that all websites on your server benefit from it, go back into WHM:

  1. Go to Services Configuration >> Apache Configuration >> Include Editor >> Post VirtualHost Include, and select All Versions. In current WHM the first menu is called Service Configuration.
  2. Paste the following into this file, and hit Update:
    SetOutputFilter DEFLATE
    
    # 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 wont work. You can use the following
    # workaround to get the desired effect:
    BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
    
    # Dont compress images
    SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary
    
    # Make sure proxies dont deliver the wrong content
    Header append Vary User-Agent env=!dont-vary
    
    SetEnvIfNoCase Request_URI "\.(?:gif|jpe?g|png)$" no-gzip
  3. That's it. Go back to the test site from the start, load up your website, and verify that compression is now working.

The "Dont compress images" line uses \.(?:gif|jpe?g|png)$ as in the official Apache mod_deflate example. The backslash makes the dot a real dot, so only file names ending in .gif, .jpg, .jpeg or .png are skipped. GIF, JPEG and PNG files are already compressed, so deflating them again only costs CPU.

Command line method

For command line users:

  1. Run the EasyApache command line build (/scripts/easyapache) and select to include mod_deflate.
  2. Once that is done, edit the /usr/local/apache/conf/includes/post_virtualhost_2.conf file and paste the code above into it.
  3. Remember to restart Apache once you've edited the file.

These paths are from EasyApache 3. On servers with EasyApache 4, Apache's files live under /etc/apache2/ and the global Post VirtualHost include is /etc/apache2/conf.d/includes/post_virtualhost_global.conf. The WHM Include Editor above works on both and writes the right file for you.

mod_deflate questions

How do I enable mod_deflate on a cPanel/WHM server?

Install mod_deflate with EasyApache in WHM. Then switch it on per site with Optimize Website in cPanel, or for every site by pasting the compression rules into the Post VirtualHost Include in WHM's Include Editor (All Versions) and pressing Update.

How do I check that compression works?

Load your website in a compression test such as http://www.whatsmyip.org/http_compression/ before and after the change. Once mod_deflate is active, the test shows your pages are compressed.

Why are images left out?

GIF, JPEG and PNG files are already compressed. Deflating them again saves almost nothing and only uses CPU, so the rules mark them with no-gzip.

Want fast hosting for your website? See our web hosting and reseller web hosting.

Related guides

If you have any questions or you need our help, don't hesitate to contact us. Our support team is available 24/7: open a support ticket.

Was this answer helpful? 0 Users Found This Useful (4 Votes)