Web hosts managed by cPanel control panel can have GZIP compression enabled by using mod_deflate on Apache 2.x (Apache 2.0 or Apache 2.2 HTTPD server). GZIP compression can compress and reduce the size of web pages that been transmitted and sent to requester, saving bandwidth and speed up website speed. cPanel has built-in mechanism to turn on and enable mod_deflate module (or mod_gzip on Apache 1.3 which is not the scope here), with all configuration, settings and setup can be done using WebHost Manager without manually editing httpd.conf Apache config file.

cPanel supports enabling of mod_deflate on per account or per server (globally for all accounts and websites) basic. Regardless of which kind of implementation prefer, the prerequisite is to compile Apache with Deflate module support.

Prerequisite: Compiling Apache with mod_deflate Module

Note: The step can be skipped if mod_deflate has already been compiled into Apache. To verify, run httpd -t -D DUMP_MODULES command on the server, and look for deflate_module (static), or by viewing details of Previously Saved Config in EasyApache, where mod_deflate is listed as Deflate.

Login to cPanel WHM (WebHost Manager), and run EasyApache (Apache Update) under Software section. Select radio button of Previously Saved Config, and click on Start customizing based on profile button.

Compile Apache with Mod_Deflate

Do whatever changes you prefer during each steps of wizard, else just click Next Step buttons, and finally click Exhaustive Options List button. Select and tick the check box for Deflate under Apache Built-in Modules. Click on Save and Build to start re-building Apache.

mod_deflate or deflate module will be built statically into Apache web server engine.

Method 1: Enable GZIP Compression on Per Account Basis

Login to cPanel account for the user account which GZIP compression wants to be turned on. Then click on Optimize Website under Software / Services.

cPanel Software / Services

User will be presented with options to compress all content or compress the specified MIME types, with a text box to specify file types to compress is selected (default is text/html text/plain text/xml). Choose either one setting to enable GZIP compression on all websites hosted by the particular account. Click on Update Settings when done.

Optimize Websites

Tip: Possible MIME types include text/html, text/plain, text/xml, text/css, text/javascript, application/javascript, application/xhtml+xml, application/xml, application/rss+xml, application/atom_xml, application/x-javascript, application/x-httpd-php, application/x-httpd-fastphp, application/x-httpd-eruby, and image/svg+xml.

Method 2: Turn Off mod_deflate GZIP Compress for Whole Server Globally

Login to WebHost Manager (WHM), and go to Services Configuration -> Apache Configuration -> Include Editor -> Post VirtualHost Include. Select All Versions for “I wish to edit the Post VirtualHost configuration include file for” option.

Copy and paste the following code into the opened text box, and hit Update button:

<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 already-compressed files
SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI .(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI .(?:avi|mov|mp3|mp4|rm|flv|swf|mp?g)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI .pdf$ 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>

Note: Above code is just an example, and can be changed, especially on the exclusion part.

Restart the Apache service after saving the change to make the change effective.

Another alternative, especially for webmasters on shared web hosting which does not have Deflate compiled into Apache, is to enable GZIP compression on PHP.

Check and verify that the GZIP compression is running on the website. If you want to log the compression ratio to a file, here’s the directives to add to enable the mod_deflate GZIP compression ratio logging.

Related Posts