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 22.214.171.124 Linked Version 126.96.36.199 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?
Have you tried with ob_gzhandler?
<?php ob_start("ob_gzhandler"); ?> <html> <body> <p>This should be a compressed page.</p> </html> <body>
Tip: Sometimes is pretty tricky to detect if the web is send compressed or not, i use firefox’s firebug plugin, i tested a php file without compression and with compression and compare the size, in my case, the difference was 1mb (non compressed) and 56kb compressed.
Or in your .htaccess
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
And to make Apache compress the static resources (e.g: .js files , .css files) do as Kamlesh did in his answer
In the official wiki of Dreamhost they enable this by modifying an htaccess:
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%.
In Apache, enabling output compression is fairly straightforward. Add the following to your .htaccess file:
## First of all you have to make changes in WHM (server) setting to enable Gzip. After that make changes in cPanel setting ##
For WHM setting => Easy Apache => Exhaustive Options List < here enable – deflate tab >
For cPanel setting => Sofware/Services => Optimize Website < here choose option what you want >
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:
<?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.