An Introduction to gzip / mod_deflate

mod_deflate Gzip Compression

mod_deflate is an Apache module that can be used to compress data using gzip compression before sending it to the user. For bandwidth intensive websites, using this feature can offer a benefit, as the server is compressing data before sending it you will be using less bandwidth. The major drawback to using mod_deflate however is that the server may need to use more resources to compress each file and this in turn can cause performance issues on older servers. As our shared accounts included unlimited bandwidth, using mod_deflate to reduce bandwidth usage is not usually a concern for shared users.

It’s important to understand that gzip compression does not reduce resource usage on a server, nor is it guaranteed to reduce page load times for your website. To reduce possible resource usage and reduce page load time, a better solution is a caching option for your site. There are caching modules or settings for sites created with a content management system (CMS) such as WordPress, Joomla, Drupal, etc.

Please note: Due to the performance issues that can be caused by mod_deflate, it should not be used on older shared servers. Contact Live Support for more information about your hosting account and how to migrate to a new Linux server hosting plan.

How to Install mod_deflate

The mod_deflate Apache module is already installed on our shared hosting servers.

cPanel VPS and dedicated server administrators can install the HTTP compression module in WebHost Manager (WHM).

  1. Log into WHM as root.
  2. On the left select “EasyApache 4.”
  3. Beside “Currently Installed Packages,” select “Customize.”
  4. Select “Apache Modules.”
  5. Search for “mod_deflate.”
  6. Select the mod_deflate toggle switch to turn it blue.
    Installing mod_deflate in EasyApache4
  7. Select “Review.”
  8. Select “Provision.”

Cloud server administrators can use the following command to see if the Apache module is already installed and enabled:

apachectl -M | grep deflate

If the output states “deflate_module (shared)” then it is installed.

How to Use mod_deflate

To use mod_deflate, you have two options:

  • Use the Website Optimization option in cPanel
  • Add a few lines to your .htaccess file

Enabling mod_deflate (gzip Compression) in cPanel

Under Software/Services in your cPanel click on the Website Optimization icon.

Optimize Website cPanel App for mod_deflate

Select Compress all content or specific MIME types. To limit which types of content are compressed, choose Compress the specified MIME types and input the particular MIME types you want to compress.

If you specify particular MIME types, it is best not to add image, video, audio, PDF or other types of binary files. These file formats are already compressed, so it is not necessary to compress these further.

Once you have chosen your settings, click Update Settings to save your changes and enable gzip compression on your account.

To turn off gzip compression, select Disabled on the Website Optimization page and click Update Settings.

Enabling mod_deflate (gzip Compression) in .htaccess

Edit your .htaccess file and add the following code:

AddOutputFilterByType DEFLATE 
text/text
text/html 
text/plain 
text/xml 
text/css 
application/x-javascript 
application/javascript

How to Test if mod_deflate is Working

There are several sites online that you can use to help test whether mod_deflate is working for your site or not. One site we found rather easy to use is https://www.whatsmyip.org/http-compression-test/.

All you need to do is enter your domain and click the “Test” button. Below we have showed before and after screenshots that indicate whether mod_deflate is enabled.

whatsmyip.org HTTP compression test unsuccessful
URL compression test failed
whatsmyip.org HTTP compression test successful
URL compression test successful

Issues with PHP 7

Various users have reported issues involving the cPanel Optimize Website button and performance with PHP files. This seems to have affected websites using PHP version 7 and up. If you are having trouble using the compression features available from cPanel, you can try making these modifications in your php.ini file.

output_handler = Off 
zlib.output_compression = On 
zlib.output_handler = ob_gzhandler

Take your business to the next level! Meet your customers online with InMotion’s Shared Hosting solutions.

32 Comments

  • As per GTMetrix (https://gtmetrix.com/enable-gzip-compression.html), they say you need the tags and .

    Do I need to do that in conjunction with the code shared on this page?

    AddOutputFilterByType DEFLATE
    text/text
    text/html
    text/plain
    text/xml
    text/css
    application/x-javascript
    application/javascript

    Furthermore, do I add it outside/after the element?

    Thanks!

    • Hello and thanks for asking. It can be helpful depending on what types of files you have your website. You can add them all within the same section.

  • When you say “can cause performance issues on older servers“, what apache server versions are you referring to ? 

    I am planning to use mod_deflate for website (mostly static) hosted on apache server. 
    Would you suggest using mod_deflate for Apache 2.0.x OR 2.2 & above ?

    • Hello Aniruddha,

      As noted above, this issue has to do more with the version of cPanel being used where mod_deflate is not present. So, if you don’t see the Optimize Website in your cPanel, then you know you’re on an older server. According to this Apache HTTP Server post, using Apache 2.0 is okay though it is no longer supported. Version 2.4 should be okay.

      If you have any further questions or comments, please let us know.

      Regards,
      Arnel C.

  •  I have added

    ini_set(‘zlib.output_compression’, ‘On’); ini_set(‘zlib.output_compression_level’, ‘1’);

    with and w/o php tags and it broke the site. 

     

  • Well in the cpanel , most of the times you have the Optimize website option. That is the GZip function . all u have to enable it .. Hope it Helps.

     

    Many Thanks

  • Hi,

      I am having shared hosting linux account & it does not have “Optimize Website” option in Cpanel. Also i added mod_deflate code in  .htaccess file. But still my website  https://www.bismicaterersperambur.com/ not compressed. Can someone help me out for this issue ?

    • Hello Muhammed,

      Are you one of our customers? This option should be within your cPanel if you are. If you are not, you may want to see if they have another option.

      Kindest Regards,
      Scott M

  • Hi ,

    I have a Magento website . Is it safe to turn on Website optimisation – from Cpanel . 

    I only Plan to compress html and xml .

     

    Will it be safe . do i need backup before enabling it 

    thanks

    • Hello Bankim,

      That should be fine, but you should definitely back up the site beforehand just in case.

      Kindest Regards,
      Scott M

  • I am new to CPanel and i have to redirect IP address to Domain Name of my comany website, how can i do this using CPanel? And i already tried .htaccess code but it’s not working….. Thanks in advance

    • Hello Advante360,

      Sorry for the problem with the re-direct. Your question is on the wrong page within our support center. You should go to the 301 Permanent Re-direct page for more information. The re-direct in .htaccess will be the best way for you to make this happen. The cPanel re-directs simply write the code in the .htaccess for you. If you’re using an IP address, then you edit the .htaccess file directly. Remember that the re-direct should be at the TOP of the file since actions within the .htaccess file are done within sequence. If there’s a conflicting rule, then it may bypass your re-direct causing it not to work. If you require further assistance with the issue, please post a reply with either your URL(if you are an InMotion customer), or further information on the code being used in the .htaccess.

      If you have any further questions or comments, please let us know.

      Regards,
      Arnel C.

  • I’ve been reading about this GZIP compression for about an hour or two now and so far I got it to work. But there’s one thing I still don’t get. Is there a difference between enabling GZIP(mod_gzip) and mod_deflate or is it all the same thing. Just been reading a article about this on another website (https://www.giftofspeed.com/enable-gzip-compression/ ) which says there is a difference between the two mods. Anyone to help me explain this? 

    • Hello Marlin!

      Great question! This is well discussed on the internet and in the responses above. You can a great discussion as well as reference links by going here. However, to summarize the issue: mod_deflate and mod_gzip use the same compression algorithm, but in different ways. Mod_deflate is configured to compress files differently (using gzip) than mod_gzip. If you want a really thorough breakdown, check out the Linux Journal article on using mod_gzip and mod_deflate. The article clearly states that when you compress with either method, you’re using gzip.

      However, for issues of performance, mod_gzip is better with the shared server as it uses less CPU. Both of these options have pro’s and cons, but it appears that InMotion recommend using mod_gzip. I hope this helps to clear up the issue. If you have any further questions or comments, please let us know.

      Regards,
      Arnel C.

  • Sorry to revive such an old topic but it does seem to be one of the more ‘up-to-date’ ones regarding gzip and mod deflate.  In light of BradM’s response, I was wondering therefore if there a critical point where if a site reaches a certain size or traffic, then gzip compression should not be enabled?  Or does this also depend on the server it resides on?

    Thanks in advance 🙂

    • Hello Flick, we love it when an old article gets some new comments so thanks for that!

      The critical point where you would want to figure out if gzip is going to really help you or not, is really going to depend on the content of your website itself, as well as the amount of CPU usage you have available to your account.

      For instance if you have very high traffic to your website, and you primarily serve up dynamically generated pages from something like PHP that are rather large in size (> 200K), having the server compress that data on the fly can be a bit taxing and lead to higher CPU usage. Also if you are trying to serve up images using gzip compression that have already been compressed for the web, this can waste CPU time doing unnecessary work. This is why in this guide it just mentions enabling the compression on plain-text files instead of everything on your site.

      In most cases having gzip compression turned on is a good idea all around. If you are on a shared hosting account you’d just want to view your resource usage to ensure you’re not taxing the server’s CPU resources too much. If you are on a VPS or dedicated server with more resources, more than likely you won’t run into any issues at all with it enabled for your sites.

      Hope that answered your question, let us know if you had anymore!

      – Jacob

    • This is ALL making my head hurt… as a beginner, I don’t understand most of all this tech talk… and wish someone from IMH would just take over our sites for 10 minutes and FIX all these technical things to WPress, server settings, cache, and all the other things that are evidently hurting BOTH of us. The problem is it takes a beginner a month to even figure out what to do, and wasting the techs time etc… I am reading OLD info mixed in with new and that is making it worst. I am ready to scream.

  • Yes, off course! Thanks Brad. You are certainly rigjht, even if today CPU power is not anymore a big constrain. Thanks indeed
    Alessandro

  • Hi alegiac,

    This compression isn’t enabled in most programs by default because in addition to its benefits, it has its drawbacks as well. While compression sends smaller files to the end user, the server has to work hard to compress everything it’s sending. Some servers, especially those getting lots of traffic, may ultimately fail / crash if they can’t keep up with all of the compression requests.

    So, basically, gzip compression can be really hard on servers in some situations, and sometimes the consequences are worse than the benefits that can be gained.

    Does that make sense?

  • Thanks, Brad. Very useful notes. I added to our .htaccess file on our web site https://www.itware.com the instructions you see at the bottom of this message, sligtly different oy yours, but it seems they work perfectly.
    My question is: why the Joomla developer team doesn’t include this code by default on the .htaccess file? All Joomla webmaster could save a lot of time, expecially the ones at the beginning of their experience, just like me: we got a very poor ranking from Google Page Speed, so I worked about solving the several items they put in the “to do list”. I found your note after several hours of navigation.
    Why don’t you suggest them this improvement?
    Thanks
    Ciao
    Alessandro

    #Gzip

    AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript text/javascript

    #End Gzip

  • Hello Farreldoc,

    I was looking at your account along with a Systems person, and we discovered that you do not have DEFLATE setup on your server. Therefore, none of the optimizations (using gzip) would work. You can install it yourself since you have root access, or you can send an email request to [email protected] Make sure you verify the account in the request with either the last 4 digits of your credit card or the AMP password.

    If you require further assistance please let us know!

    If you have any further questions, please contact technical support available 24 hours a day / 7 days a week.

    Regards,

    Arnel C.
    Community Support

  • I have been unable to get compression to work. I have a VPS, and I don’t have the Software Optimization option. I added the code to my .htaccess (and tried other code I found on Apache’s site), and I still fail the compression test.

    What am I doing wrong?

    www.alvingolf.com

  • You’re welcome Teebird. Thank you for bringing this to our attention so we could clarify the information.

    Please feel free to contact us anytime you have questions or need assistance. We’re always happy to help!

    Regards,

    Christi N.

  • Ah, thanks for the update and good points about WP theme updates.

    I have gzipped my InMotion sites via “Software Optimization” wizard in my cPanels. Thanks! 🙂

  • Hi Teebird,

    We apologize for the confusion. That note mod_deflate on shared servers was in reference to older shared servers that do not support mod_deflate. I have updated the article to clarify this.

    While your theme files may not be technically core files (I apologize for the incorrect reference) if you are not using a Child Theme and your theme is updated, your changes could be overwritten and lost.

    To speed up WordPress, a caching plugin like https://www.inmotionhosting.com/support/edu/wordpress/recommended-wordpress-plugins/optimizing-wordpress-with-wp-super-cache is a preferred solution.

    Regards,

    Christi N.

  • Hi CNick,

    I would use the above method if I wasn’t on a shared hosting plan. The tutorial above says that mod_deflate is NOT recommended for InMotion shared hosting:

    “Please note: Due to the performance issues that can be caused by ‘mod_deflate, it should not be used on our shared servers’, and instead only be used on VPS and Dedicated accounts. Also, as our shared accounts included unlimited bandwidth, this further reduces the need to use mod_deflate to reduce bandwidth usage.”

    Also, I don’t believe files in a WordPress theme are considered a part of
    the WordPress Core files. 🙂

    The method I am using is also pretty common among wordpress users: https://bit.ly/IpEOOC

    Thanks

  • Hi Teebird,

    Thank you for the input. Modifying your core WordPress files is not recommended, and in this case is not needed. You can simply add the code shown above to your .htaccess file (either before or after any code added by WordPress).

    Please let us know if you have any questions or need any assistance.

    Regards,

    Christi N.

  • For those on shared InMotion hosting, I’ve added the following code snippet to the top of my WordPress themes’ header.php files in order to achieve Gzip compression:

    So far I haven’t run into any performance issues.

  • Tim S, thanks for the support. The change by InMotion to the IP address corrected the problem. If you can post that as an answer, I will mark it as the best answer.

    Thanks so much for the excellent support. InMotion rocks!… JMcSweeney

  • Hi JMcSweeney,

    Thanks for posting. I’ve touched base with a tier2c support rep and the sending IP address of the server has been changed. Please test sending the email again. If you get any further bouncebacks please feel free to contact us.

    If you need further assistance please feel free to contact us as well.

    Thanks!

    Tim S

Was this article helpful? Let us know!