Compressing a web page been serving it to web browser which requested the web page can save and reduce bandwidth usage. With lesser bytes to transmit and transfer over Internet, a web page request can be fulfilled and transferred faster, freeing up process and resources to handle other requests. The end result is faster website, which improves visitors’ experience and potentially improves search engine ranking.

Most modern web browsers accept web pages encoded in GZIP or Deflate encoding method, with GZIP as the more popular compression format. Webmasters and system administrators are also encouraged to implement GZIP compression.

There are many ways to implement and enable GZIP compression on a website. For example, enable GZIP compress by configuration mod_deflate on Apache HTTPD server (Note: mod_deflate is for Apache 2.x only, while mod_gzip is used in Apache 1.x). However, for users who can’t modify the Apache configuration file, or unfamiliar with various syntax and directives of mod_deflate and mod_gzip modules, there is a simple way to turn on and enable GZIP compression, specifically for PHP scripts.

PHP can compress the content output by the PHP scripts, including HTML, JavaScript and CSS codes which are included inside the PHP files. GZIP compression is supported by default on PHP version 4.3 or newer.

To enable PHP GZIP compression, edit the php.ini (example locations such as in /etc/ or /usr/local/lib) with any text editor such as vi, and locate the following directive:


The default value is Off, change the setting to On to make the line looks like below:

zlib.output_compression = On

Restart Apache HTTPD server after the change. Every web pages produced by PHP will now be compressed before sending over Internet to the web browser to decode.

PHP also has a directive which can be used to adjust the compression level. To set the compress level, use the following line in php.ini, with valid values between 1 and 9, where 1 is least compress and 9 is most compress. Default compression level is 6, which provides the best compression without degrading server performance.

zlib.output_compression_level = 6

Previously, PHP compression is achieved by entering <?php ob_start(“ob_gzhandler”); ?> code at the beginning of each PHP scripts. The method is not recommended as each and every scripts have to be changed. zlib enabling in php.ini will apply to all PHP scripts on the web server without exclusion.

If it’s impossible to modify php.ini file, or does not have control over php.ini especially on a shared hosting, the PHP GZIP compression can also be setup via .htaccess file, typically located on the root of the website. To enable PHP GZIP compression via Zlib, just add the following line to the .htaccess file. Note that each website has to be changed individually.

php_flag zlib.output_compression on

The disadvantage of enabling GZIP compression via PHP is that only web pages generated by PHP scripts will be compressed by GZIP encoding. All external CSS or JavaScript files will not be compressed. For the ability to enable GZIP compression on all file types, uses mod_deflate or mod_gzip on HTTPD web server instead.

Do check and verify that GZIP compression is running properly.