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.
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:
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.
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:
[[email protected] ~]$ 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 [[email protected] ~]$
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 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.