Enable Gzip on WHM globally for all accounts and websites

Apache mod_deflate module is one of the best friends of a cPanel system administrator. It allows you to save bandwidth and accelerate page speed response by deflating output filter from your server to be served compressed (gzipped) before it goes back to the client.

mod_deflate is the modern replacement for the old mod_gzip, which was used to compress – gzip web pages in the first versions of Apache like 1.3.x

How can I enable Gzip on WHM for all my cPanel accounts / domains?

cPanel comes by default with Apache 2.4, and on this kind of servers it’s pretty easy to enable Apache Gzip compression for all your server accounts.

apache gzip test

If you have your own Dedicated Server or Cloud VPS with full root access ro WHM, the you can enable Apache mod_deflate globally for all your cPanel users. This tutorial will show you how to activate Gzip compression for Apache on cPanel using mod_deflate.

1. Login to WHM

Move to WHM control panel login, access with your root credentials from:

https://111.222.333.444:2087/

Replace 111.222.333.444 with your real server IP address.

2. Edit Apache Configuration

On the left menu, search for:

  • Home » Service Configuration » Apache Configuration » Include Editor
  • Edit “Pre VirtualHost Include (All versions)” block.

3. Update Pre VirtualHost

Add this code to your Pre VirtualHost include, as you see below.

<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE image/x-icon
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/x-font
AddOutputFilterByType DEFLATE application/x-font-truetype
AddOutputFilterByType DEFLATE application/x-font-ttf
AddOutputFilterByType DEFLATE application/x-font-otf
AddOutputFilterByType DEFLATE application/x-font-opentype
AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
AddOutputFilterByType DEFLATE font/ttf
AddOutputFilterByType DEFLATE font/otf
AddOutputFilterByType DEFLATE font/opentype
# For Olders Browsers Which Can't Handle Compression
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
</IfModule>

Enable Gzip on WHM for all websites

4. Restart Apache and Verify

After adding that code into your Apache configuration, make sure to hit Update button, so all the settings can be saved. Then hit the Restart button to apply the changes.

5. Verify Gzip is working on all your sites

You can use sites like gzip test to verify if all your WHM websites are using the mod_deflate configuration you specified.

Alternatively, you can simply use curl from the Linux terminal, like this.
If you see “Content-Encoding: gzip” in the headers, then your content is gzipped. See below:

[webtech@localhost ~]$ curl -H "Accept-Encoding: gzip" -I https://nixcp.com
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 12 Jul 2017 17:47:59 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
Vary: Accept-Encoding
Link: <https://nixcp.com/wp-json/>; rel="https://api.w.org/"
Strict-Transport-Security: max-age=15724800; includeSubdomains; preload
X-Frame-Options: DENY
X-Content-Type-Options: nosniff
Content-Encoding: gzip
[webtech@localhost ~]$

If that code doesn’t work, edit the Pre VirtualHost block again, delete the old code and try this alternative:

# Enable GZIP Compression.

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

# Make sure proxies don't deliver the wrong content
Header append Vary User-Agent env=!dont-vary

That’s all, now you know how to enable gzip compression for all your accounts on a WHM based server.

Further reading:

About the Author: Martin Keler

1 Comment

  1. Not worked bro, Are there other alternatives to solve this problem?

    I’m tired of following all but the web optimizetion still doesn’t appear on cpanel even for all users and resellers.

Leave a Reply

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