How to enable GZIP compression

GZIP-Compression

Hello, friends, today’s topic is how to enable GZIP compression on your web server. If you guys know what is GZIP and how to enable it, that’s good but those who don’t know please read this tutorial till the end.

What is GZIP Compression?

GZIP is a file format and a software application used for website file compression and decompression. GZIP compression website files include HTML, Stylesheets, Javascript. GZIP Compression is one of the easiest optimizations for a website.

GZIP compression never work on images because these are compressed in a different way

How we check GZIP Compression enable or not?

Don’t worry, this is not a complicated task, you can check this on your browser and all browsers support this. We have to just check content-encoding: gzip HTTP response header in browser Devtool. If it is active, it means GZIP is enabled on your website. Let me show you in the screenshot.

Content-Encoding

According to the screenshot above, GZIP compression is enabled on the website.

How we enable GZIP Compression?

There are two methods to enable GZIP compression, First one is from the server and the second is from the website .htaccess file. Basically is a mod_deflate, if this enables from the server then you can just add the script in the .htaccess file that’s it. If not then you have enabled it from the server either from cPanel or WHM.

First, we will see how we enable mod_deflate from cPanel, check below screenshots steps by steps:

  1. Login to your cPanel account, Go to the Software section and click MultiPHP INI Editor, see below screenshot MultiPHP INI Editor
  2. Now you are in MultiPHP INI Editor page, Select your domain on which you need to enable deflate, see screenshot below Select your domain
  3. Once you select your domain, available PHP Directives will show in the bottom. You have to find zlib.output_compression, enable it and hit the Apply button. See below screenshot…zlib output compression

 

Above we saw how we enabled GZIP compression or the deflate but what happen if this PHP Directive is not available in your cPanel. Ok, don’t worry, we will walk you through this. If we have WHM access then below steps will help to enable GZIP Compression if you don’t have WHM then ask your hosting support.

Let’s see the WHM steps to enable GZIP Compression, please follow the screenshots they will help you more…

  1. Login to your WHM, search EasyApache in the search bar and click on EasyApache 4, see screenshot…
    search easyapache in WHM
  2. On the EasyApache 4 page, we have to customize cPanel. Click Customize button of cPanel Default box, see screenshot…
    customize cpanel
  3. Once you are on cPanel Customize page, Apache Modules >> search deflate >> enable mod_deflate. Once you enable it, click Review tab, follow the screenshot below…
    deflate in WHM
  4. After clicking Review tab >> scroll down click Provision button >> wait till the process end>> once Provision finished successfully, hit the Done button to complete the process.
  5. mod_deflate is now enabled successfully.

 

Another way to enable GZIP Compression is editing website .htaccess file. We have to simply add the code in .htaccess file but make sure mod_deflate will be enabled from the server as we saw above in the article. You can find your .htaccess file on the website root directory through FTP(FileZilla).

<IfModule mod_deflate.c>
  # Compress HTML, CSS, JavaScript, Text, XML and fonts
  AddOutputFilterByType DEFLATE application/javascript
  AddOutputFilterByType DEFLATE application/rss+xml
  AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
  AddOutputFilterByType DEFLATE application/x-font
  AddOutputFilterByType DEFLATE application/x-font-opentype
  AddOutputFilterByType DEFLATE application/x-font-otf
  AddOutputFilterByType DEFLATE application/x-font-truetype
  AddOutputFilterByType DEFLATE application/x-font-ttf
  AddOutputFilterByType DEFLATE application/x-javascript
  AddOutputFilterByType DEFLATE application/xhtml+xml
  AddOutputFilterByType DEFLATE application/xml
  AddOutputFilterByType DEFLATE font/opentype
  AddOutputFilterByType DEFLATE font/otf
  AddOutputFilterByType DEFLATE font/ttf
  AddOutputFilterByType DEFLATE image/svg+xml
  AddOutputFilterByType DEFLATE image/x-icon
  AddOutputFilterByType DEFLATE text/css
  AddOutputFilterByType DEFLATE text/html
  AddOutputFilterByType DEFLATE text/javascript
  AddOutputFilterByType DEFLATE text/plain
  AddOutputFilterByType DEFLATE text/xml

  # Remove browser bugs (only needed for really old browsers)
  BrowserMatch ^Mozilla/4 gzip-only-text/html
  BrowserMatch ^Mozilla/4\.0[678] no-gzip
  BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
  Header append Vary User-Agent
</IfModule>

Please ensure that this code will add below the current contents of your website .htaccess file.

How did GZIP Compression work?

When a browser (Chrome, Firefox, etc) visits a website it checks to see if the server has GZIP enabled by seeing if the Content-encoding: gzip response header exists. If the header is detected, it serves up the compressed and smaller files. Yes, that’s the way the website loads fast.

Friends if you follow these steps, your website will definitely load fast. Please let me know through your comments if this article helps you. Keep learning.

Share:

Facebook
Twitter
Pinterest
LinkedIn

2 thoughts on “How to enable GZIP compression”

  1. Hey phpyouth ,

    Great post with effective tips to enable the Gzip compression.

    Gzip is a file format and software application used to compress and decompress the website file but it is also true that Gzip compression technique never works on images.

    Your suggested steps to enable the Gzip compression are clear, easy to understand and follow, whereas following these steps will be helpful and allows the users to enable the Gzip compression.

    Truly helpful post and thanks for sharing.

Please share your thought