• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar

A cPanel Tech

The Diaries of a Linux Server Administrator

  • Home
  • About
  • Privacy
You are here: Home / Apache / How to Enable Gzip Compression in Apache

Deprecated: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /home/cptech/public_html/wp-content/themes/genesis/lib/functions/image.php on line 116

How to Enable Gzip Compression in Apache

October 12, 2015 By admin Leave a Comment

Gzip compression is a great tool by web servers with which we can cutdown the size of the pages sent out. In this topic I am discussing on how to enable gzip compression in Apache. Enabling compression means the web server will compress the size of the pages there by we can achieve better speed and low bandwidth usage.

[the_ad id=”11935″]
This can be achieved as follows:

Enable mod_deflate in Apache

  1. Add the “Deflate” Apache module via EasyApache
  2.  Under WHM >> Service Configuration >> Apache  Configuration
  3. Click Include Editor
  4. Under Pre VirtualHost Include select “All Versions” and add following lines to the code box:
    <IfModule mod_deflate.c>
    SetOutputFilter DEFLATE
    # 1 - 9, with 9 as highest level of compression
    DeflateCompressionLevel 9
    
    DeflateFilterNote Input input_info
    DeflateFilterNote Output output_info
    DeflateFilterNote Ratio ratio_info
    LogFormat '"%r" %{output_info}n/%{input_info}n (%{ratio_info}n%%)' deflate
    #CustomLog logs/deflate_log deflate
          
    BrowserMatch ^Mozilla/4 gzip-only-text/html
    BrowserMatch ^Mozilla/4\.0[678] no-gzip
    BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
    
    SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png|mov)$ \
        no-gzip dont-vary
    SetEnvIfNoCase Request_URI \
        \.(?:exe|t?gz|zip|bz2|sit|rar)$ \
        no-gzip dont-vary
    SetEnvIfNoCase Request_URI \.pdf$ no-gzip dont-vary
    
    <IfModule mod_headers.c>
    # Make sure proxies dont deliver the wrong content
    Header append Vary User-Agent env=!dont-vary
    </IfModule>
    </IfModule>
  5. Update and restart apache

This will enable deflate module in the server. Now we need to enable it in the cPanel of the domain which we need to enable compression.

Enable compression in cPanel

  1. Login to cpanel.
  2. Under Software/services section click Optimize Website.
  3. Choose out of the following options to enable or disable gzip compression.
     Disabled
     Compress all content
     Compress the specified MIME types
  4. Click Update settings.

To test if gzip compression is working as expected you can use following tool: http://www.whatsmyip.org/http_compression/

 

 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.

Having compression enabled may conflict and generate a blank or scrambled page if you have enabled compression already in the source code of your website.

Filed Under: Apache, cPanel, Tutorials Tagged With: apache, compression, gzip, mod_deflate

Reader Interactions

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Copyright 2018 CPanelTech