Falcon1986-Online

Home of a med student who likes to manage websites and talk tech!

Force GZIP compression on your HostMonster-hosted website

with 27 comments

NOTICE: An update to this article has been posted here in which I have made a few adjustments.

As you may already know, I am a speed and performance junkie. When it comes to website management, nothing makes me feel better than a website that loads fast! If this can be done with dynamic content then it is something to admire.

Works on HostMonsterOne feature that I have implemented on the HostMonster-hosted website that I manage is GZIP compression. For a detailed description on GZIP you can read this very interesting article on BetterExplained.com. In short, however, what GZIPing does is that it compresses downloaded files which essentially saves a bit on server bandwidth and slightly speeds up client-side viewing. Although you can use some PHP to call for compression on the fly for specific files it is much easier to simply activate compression from the web server. Here, I will show you how you can activate it for a website hosted with HostMonster. This should also work if you are on another shared hosting provider that runs under Linux and Apache. You need access to your php.ini file and .htaccess file.

  1. Check to make sure that your hosting environment can utilize GZIP. If you host your website with HostMonster this feature is supported. On other web hosts you will need to execute phpinfo(), then ensure that ‘gzip‘ is listed under _ENV[“HTTP_ACCEPT_ENCODING”] and HTTP_ACCEPT_ENCODING to verify.
  2. Once you have verified that the feature is supported go ahead and create a single php.ini file that can be applied to all subdirectories within your /htdocs or /public_html directory. On HostMonster-hosted, just go to the ‘Software/Services’ section of your cPanel and click on ‘PHP Config’. Select ‘PHP5 (single php.ini)’ and save the change. Then, click on “Install PHP.ini master file”. Rename the “php.ini.default” file to php.ini in your public web files directory. If you had one there before, delete it before renaming the new php.ini file.
  3. Open the php.ini file in a simple text editor and navigate to the section that deals with . Set the value equal to ob_gzhandler and ensure that any values for zlib are disabled or commented out. Afterward, save and update the php.ini file on the server.
    output_handler = ob_gzhandler
    zlib.output_compression = Off
    ;zlib.output_handler = 
  4. Open the .htaccess file that is located in your public web files directory (usually the same location as you php.ini file). Add the following statements to invoke the use of the relevant Apache module for compressing your files listed and save.
    mod_gzip_on Yes
    mod_gzip_dechunk Yes
    mod_gzip_item_include file \.(js|css|html|php)$
    mod_gzip_item_include handler ^cgi-script$
    mod_gzip_item_include mime ^text/.*
    mod_gzip_item_include mime ^application/x-javascript.*
    mod_gzip_item_exclude mime ^image/.*
    mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
  5. Now try accessing your website through your browser. After about a minute or two, run the GIDZipTest for your website. If your site is reported to be serving compressed pages then you are finished!

I have been able to achieve compression as high as 70% as reported by the GIDZipTest page. I hope it works for you and your site sees a bit more improvement when it comes to loading time. There are still a few other things that can be done in terms of making things faster, but those will come later. Happy tweaking!

WARNING: Do not attempt to enable GZIP using these steps while you have ZLIB enabled. Use one or the other. Again, HostMonster supports ZLIB but, just like GZIP, it is not enabled by default. Furthermore, attempting to enable server-side compression while you have PHP or plugins already doing the compression can lead to a garbled output.

Written by falcon1986

29 January, 2009 at 10:31 PM

Posted in Web Development

Tagged with , ,

27 Responses

Subscribe to comments with RSS.

  1. Hi,

    Great post. I got my html and php to compress (~65% compression).

    Still trying with javascript and css; YSlow keeps telling me they aren’t being compressed.

    Thanks a lot for posting this.

    adriano11235

    17 February, 2009 at 7:35 PM

  2. @adriano11235: I’m glad the post was useful to you.

    I should have mentioned in my post that the compression being executed here is only for files stored on the local server. External files stored in locations outside of your domain will not be compressed.

    falcon1986

    17 February, 2009 at 9:24 PM

  3. […] download in order to view your website then you can increase page loading speed. Combine this with server-side compression and your web pages will feel like they are on steroids! You may be asking yourself, “How does […]

  4. Hi Falcon,

    I tried following this post, but I am unable to gzip any other file but the main HTML file. The js/css files are still uncompressed (they are on my server). Is this a limitation because HostMonster does not allow access to httpd.conf or is there something I am missing to enable the zipping of js/css files in addition to basic html files?

    sudeepmandal

    26 May, 2009 at 12:49 AM

    • @sudeepmandal:

      While you do not have access to the http.conf file in the shared server environment, you can still utilize .htaccess files instead, which will override some settings within http.conf.

      Also, note the following:

      1. An .htaccess file will only affect the directory in which it is currently located and any subdirectories just below it. It will not affect anything higher in the directory tree structure.

      2. Are you utilizing a global php.ini file? This can be configured using PHP Config from cPanel. Choose ‘PHP5 (single php.ini)’ > ‘Save Changes’ > ‘Install php.ini master file’. Rename the file to php.ini in your /public_html directory, then edit it so that only ‘output_handler’ is set to use ‘ob_gzhandler’. ‘zlib.output_compression’ and ‘zlib.output_handler’ can be commented out using a preceding semicolon (;).

      falcon1986

      26 May, 2009 at 9:40 AM

    • @falcon

      Thanks for getting back to me. I did check everything and I have everything setup as you have suggested Single php.ini, I even did a fresh install and modified the lines to set ob_gzhandler and commented out the zlib stuff.

      After some more testing this is what I have observed.

      a) Just changing the output handler to ob_gzhandler automatically makes my website serve zipped text/html files. It does this even when I have not inserted any of the .htaccess code.

      b) The additional code in .htaccess doesn’t seem to have much effect. It is as if it is not finding mod_gzip.c and so it doesn’t use any of the code in the If block. I tried removing the If statement and put the contents of the code directly in the .htaccess file but then I started getting 500 errors.

      Do you have any thoughts? As for my site, it is running WP 2.7.1 and I am using the Hyper Cache plugin to serve cached pages along with DB-Cache to cache some PHP queries.

      So far I have had no luck trying to serve zipped js and css files.

      sudeepmandal

      26 May, 2009 at 1:48 PM

  5. Talking to HostMonster support, they mentioned that mod_deflate and mod_gzip are not enabled on user accounts. Are you certain that you were able to use mod_gzip to compress your css and html files? Right now I have to jump through a bunch of hoops to try and get this to work.

    sudeepmandal

    26 May, 2009 at 11:10 PM

  6. I’ve been trying to get this working on my Hostmonster hosted blog tonight, http://blog.thelifeofbrian.info. After trying this with no luck, then trying your instructions with the same result, I contacted support for assistance.

    I just received this response:
    We do not have mod_deflate and it doesn’t look like mod_gzip is working any more either. My site used to show it was gziped but now it doesn’t I guess the admins decided to remove it from the servers.

    dfwdraco76

    4 June, 2009 at 8:34 PM

  7. Dude.

    Dude.

    Dude!

    You so ****ING rock. My site is SO much faster now, it’s insane. And being that I am using Hostmonster, I rather expected this not to work, but it does! w00t.

    You are teh awesome. I am SO giving you some love on my blog (http://geeksneversleep.com, if you want to see what your awesomeness helped…)

    :]

    louiemctool

    11 July, 2009 at 7:24 PM

    • Thanks, ‘louiemctool’. Glad it worked out for you. I only recently went to your blog and saw the article you posted. Sorry I didn’t do so earlier.

      BTW, I will be posting an update to this article some time soon. I have made a few changes and found some very useful tools for implementing quite a few other optimizations. Stay tuned!

      falcon1986

      14 November, 2009 at 7:01 PM

  8. […] having SuPHP installed for added security 02. How to use gzip compression on your PHP websites 03. Force GZIP compression on your HostMonster-hosted website 04. GZIP […]

    Enable Gzip | Eric's Who Know

    15 September, 2009 at 3:46 PM

  9. Hi all
    I must be stupid or completely unable. my site generated a 500 error when I tried to apply the procedure. I am on hostmonster! So here are my steps:

    1) FIRST doubt: I have a static site AND a blog on that root. is this feasible and affecting my site (.html pages too?)

    2) I did manage to create the PHP.INI as requested, and think I made all the checks (zlib and all the rest). though the ini file is not something I am used to I found the commands after a lot of nots, but they seems to be OK. maybe I am completely stupid but a screenshot might help

    3) .htaccess this is more familiar to me. I typed the text give at the end of the file (I have some redirects already in the file) I checked and checked again and I think i wrote the wole correctly (COULD NOT CUT AND PASTE being the code an image) here is my text:

    mod_gzip_on Yes
    mod_gzip_dechunk Yes
    mod_gzip_item_include file \.(js|css|html|php)$
    mod_gzip_item_include handler ^cgi-script$
    mod_gzip_item_include mime ^text/.*
    mod_gzip_item_include mime ^application/x-javascript.*
    mod_gzip_item_exclude mime ^image/.*
    mod_gzip_item_exclude mime rspheader ^Content-Encoding:*.gzip.*

    I think I did all the necessary
    Why am I having a 500 error on my page then?
    Do I have to do something to my root, files or else that you all know and I do not?
    Please help me

    saphira

    17 December, 2009 at 3:50 PM

  10. sorry here the correct code I used:

    mod_gzip_on Yes
    mod_gzip_dechunk Yes
    mod_gzip_item_include file \.(js|css|html|php)$
    mod_gzip_item_include handler ^cgi-script$
    mod_gzip_item_include mime ^text/.*
    mod_gzip_item_include mime ^application/x-javascript.*
    mod_gzip_item_exclude mime ^image/.*
    mod_gzip_item_exclude mime rspheader ^Content-Encoding:*.gzip.*

    saphira

    17 December, 2009 at 3:51 PM

    • Please have a look at an updated article on the topic and see if things are explained better there.

      https://falcon1986.wordpress.com/2010/01/08/more-on-compressing-your-website-content-for-speed-part-i/

      If changing something within .htaccess causes a 500 Internal Server error, this probably means that there was something wrong with the syntax. It is important that you copy/paste in simple text, not formatted text. Additionally, if you are copying from one character encoding type to the other, weird symbols can appear.

      BTW, these mod_gzip statements should be placed close to the top of the .htaccess file.

      falcon1986

      8 January, 2010 at 8:53 PM

  11. Admin how do you keep this site so clean and neat?

    Software Parental

    28 January, 2010 at 5:39 AM

    • I guess using WordPress has a lot to do with it.

      falcon1986

      31 January, 2010 at 11:48 AM

  12. I use http://www.wiredtree.com/243-0-3-28.html to host my website and I have for no reason been happier. They are probably the most secure, reliable, and supportive web hosting company. I recommend you use their services too!

    Fanny Seegmiller

    31 January, 2010 at 12:11 AM

    • Thanks for the recommendation. Will probably look into it when I need VPS or dedicated hosting. For now, shared is sufficient.

      falcon1986

      31 January, 2010 at 11:47 AM

  13. I host my site with hostmonster too. I must say that I’m very happy with their hosting. I never have problems contacting support, and I have never experienced downtime!

  14. thanks Work perfect

    Salem Fituri

    26 November, 2012 at 4:26 AM

  15. I definitely wanted to jot down a quick remark in order to

    express gratitude to you for the

    awesome tactics you are writing at this site. My time intensive
    internet look up has finally been recognized with reputable tips to

    write about with my close friends. I would

    express that we readers actually are very much fortunate to be in a
    great

    network with very many lovely professionals with interesting strategies.
    I feel

    truly blessed to

    have encountered your webpages and look forward to so many more fabulous

    times reading here. Thanks once again for all the details.

    Aracely

    11 December, 2012 at 1:56 AM

  16. You completed certain fine points there. I did a search on the

    topic and found most

    people will have the same opinion with your blog.

    Freddie

    12 December, 2012 at 11:05 AM

  17. Howdy just wanted to give you a brief heads up and
    let you know a few of the pictures aren’t loading correctly. I’m not sure why
    but I think its a linking issue. I’ve tried it in two different web browsers and both show the same results.

    bamboo phone cases

    13 December, 2012 at 9:06 AM

    • I’m not sure what you did, but when I wrote these articles, they were not intended for compressing images. The various operations do not even attempt to do this. Please check your hosting configuration or .htaccess files for recent changes that could have caused the problem. Sorry to hear about your experience. What do you mean by “the pictures aren’t loading correctly”?

      falcon1986

      22 December, 2012 at 12:45 PM

  18. There is certainly a great deal to find out about this
    issue. I really like all the points you made.

    crystal heels

    30 March, 2014 at 3:05 AM

  19. Way cool! Some extremely valid points! I appreciate you penning
    this write-up and the rest of the website is very good.

    Our company

    9 April, 2014 at 3:45 PM

  20. Ina

    3 May, 2017 at 6:06 AM


Leave a comment