Force GZIP compression on your HostMonster-hosted website

2009 January 29
by falcon1986

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.
    zlib-phpini
  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.
    gzip-htaccess1
  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.

10 Responses leave one →
  1. 2009 February 17

    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.

  2. 2009 February 17

    @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.

  3. 2009 May 26
    sudeepmandal permalink

    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?

    • 2009 May 26

      @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 (;).

    • 2009 May 26
      sudeepmandal permalink

      @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.

  4. 2009 May 26
    sudeepmandal permalink

    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.

  5. 2009 June 4
    dfwdraco76 permalink

    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.

  6. 2009 July 11

    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…)

    :]

Trackbacks & Pingbacks

  1. WordPress: Combine CSS stylesheets for fewer HTTP requests « Falcon1986-Online
  2. Enable Gzip | Eric's Who Know

Leave a Reply

You must be logged in to post a comment.