36

I have found a couple of tutorials on how to enable gzip, but nothing seems to be working for me, so my question is how do i enable gzip. I am on a shared Dreamhost hosting server, It is running PHP version 5.2, and Apache, from the php info i have found this line, maybe this could help?

zlib

ZLib Support    enabled
Stream Wrapper support  compress.zlib://
Stream Filter support   zlib.inflate, zlib.deflate
Compiled Version    1.2.3.3
Linked Version  1.2.3.3

Directive   Local Value Master Value
zlib.output_compression Off Off
zlib.output_compression_level   -1  -1
zlib.output_handler no value    no value

I have also found this line

_SERVER["HTTP_ACCEPT_ENCODING"] gzip, deflate

I don't know if that has anything to do with it. But that is my first question, secondly, i have dropbox, hosting a javscript file, and I am wondering is it possible to have that file gzipped, It is not being transfered compressed, so is ther any way to do so?

1
  • What do you want gzip for? Using PHP gzip functions? Compressing the output of your web server? If it's the latter, install mod_deflate, and try serverfault.com if you have any problems/questions doing it.
    – netcoder
    Jan 17, 2011 at 0:51

6 Answers 6

59

Have you tried with ob_gzhandler?

<?php ob_start("ob_gzhandler"); ?>
<html>
  <body>
    <p>This should be a compressed page.</p>
  </html>
<body>

As an alternative, with the Apache web server, you can add a DEFLATE output filter to your top-level server configuration, or to a .htaccess file:

<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml \
        text/css application/x-javascript application/javascript
</IfModule>

Tip: Sometimes it is pretty tricky to detect if the web server is sending compressed content or not. This online tool can help with that.

Using the developer tools in my web browser, I tested a PHP file with and without compression to compare the size. In my case, the difference was 1 MB (non-compressed) and 56 KB compressed.

5
  • what about for the stylesheet and the javascripts? i was figuring it would be more of an .htaccess thing. i will try this though.
    – mcbeav
    Jan 17, 2011 at 0:42
  • As far as i can see that is not giving me any results.
    – mcbeav
    Jan 17, 2011 at 0:45
  • 1
    all that did was make the page output the sourcecode minus the php. btw, i appreciate the help. thanks for tyring to help me out
    – mcbeav
    Jan 17, 2011 at 1:09
  • 4
    Make sure to check the browser supports gzip compression: <?php if (substr_count($_SERVER[‘HTTP_ACCEPT_ENCODING’], ‘gzip’)) {ob_start(“ob_gzhandler”);} ?>
    – Basil Musa
    Jan 7, 2016 at 22:08
  • Why not just using the php.ini configuration zlib.output_compression = 1 ? Dec 24, 2017 at 11:04
10

All I had to do to enable the encoding at the Apache level is

zlib.output_compression = 1 // the PHP.ini file

this will make the server do the necessary request header check, compress, send related headers

you can also do that in your PHP files before the ob_start()

ini_set("zlib.output_compression", 1);

And to make Apache compress the static resources (e.g: .js files , .css files) do as Kamlesh did in his answer

7

In the official wiki of Dreamhost they enable this by modifying an htaccess:

<IfModule mod_gzip.c>
    mod_gzip_on       Yes
    mod_gzip_dechunk  Yes
    mod_gzip_item_include file      \.(html?|txt|css|js|php|pl|jpg|png|gif)$
    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.*
</IfModule>

This basically checks to see if mod_czip.c is found and if it is it will compress the files for you so they are faster to send to the browser. This supposedly speeds up download times 35-40%, and then the file size should supposedly go down to 55-65%.

With a quick search on Google you can come up with another thread on Stackoverflow an in a third party site addressing this issue.

2
  • This is very old. With Apache 2 it's best to use mod_deflate.c instead. Nov 5, 2016 at 11:03
  • You include files like jpg/png/gif and afterwards you forbid them through the mime image/.*. Does not really make sense to me.
    – mgutt
    Feb 21, 2017 at 21:02
6

In Apache, enabling output compression is fairly straightforward. Add the following to your .htaccess file:

# compress text, html, javascript, css, xml:
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript

# Or, compress certain file types by extension:
<files *.html>
SetOutputFilter DEFLATE
</files>

Source : http://betterexplained.com/articles/how-to-optimize-your-site-with-gzip-compression/

0
3

## First of all you have to make changes in WHM (server) setting to enable Gzip. After that make changes in cPanel setting ##

  1. For WHM setting => Easy Apache => Exhaustive Options List < here enable - deflate tab >

  2. For cPanel setting => Sofware/Services => Optimize Website < here choose option what you want >

1

The compression can be done in two ways.

Apache actually has two compression options:

  • mod_deflate is easier to set up and is standard.
  • mod_gzip seems more powerful: you can pre-compress content.

Deflate is quick and works, so I use it; use mod_gzip if that floats your boat. In either case, Apache checks if the browser sent the “Accept-encoding” header and returns the compressed or regular version of the file. However, some older browsers may have trouble (more below) and there are special directives you can add to correct this.

If you can’t change your .htaccess file, you can use PHP to return compressed content. Give your HTML file a .php extension and add this code to the top:

In PHP:

<?php if (substr_count($_SERVER[‘HTTP_ACCEPT_ENCODING’], ‘gzip’))
ob_start(“ob_gzhandler”); else ob_start(); ?>

We check the “Accept-encoding” header and return a gzipped version of the file (otherwise the regular version). This is almost like building your own webserver (what fun!). But really, try to use Apache to compress your output if you can help it. You don’t want to monkey with your files.

Reference: http://betterexplained.com/articles/how-to-optimize-your-site-with-gzip-compression/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.