Home » Php » php – How to enable gzip?

php – How to enable gzip?

Posted by: admin April 23, 2020 Leave a comment

Questions:

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?

How to&Answers:

Have you tried with ob_gzhandler?

php manual

<?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

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

Answer:

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

Answer:

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.

Answer:

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/

Answer:

## 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 >

Answer:

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/