Why need zlib on your linux web server? zlib, gzip and mod_deflate on Apache HTTPD server compress your web pages and serve them to client’s web browsers which can save and reduce bandwidth usage. Most modern web browsers today like Google Chrome or Mozilla Firefox supports both gzip and/or deflate. Enable zlib.output_compression setting in your linux web server will allow gzip compress pages served by PHP.
zlib is a compression library, written by Jean-loup Gailly and Mark Adler. Zlib works in Linux, Mac OS, even gaming consoles like playStaion 3, Xbox 360 and Wii. Zlib uses deflate() and inflate() algorithm to compress and decompress files. To speed up your website, make sure your web hosting support PHP GZIP compression enabled. To enable gzip, you can either modify php.ini if you have root access to your VPS or dedicated server, or use .htaccess to enable zlib.
Install zlib (gzip) on Linux web server
Normally zlib is installed in your web server by default, for some reason if you don’t have zlib installed, here is the command to install zlib to your web server
For fedora, centos, or redhat based distribution
# yum install zlib-devel
For ubuntu, or debian based distribution
# apt-get update && apt-get install libgcrypt11-dev zlib1g-dev
Enable zlib (gzip) on Linux web server
To locate php.ini in your web server, first create info.php and save it at your web root directory, for example in public_html, or www directory
# nano info.php
copy and paste this code to info.php and save it.
<?php phpinfo(); ?>
Load the info.php you just created in your web browser, since you located that info.php in the root web directory, you should be able to load that file at
http://yourdomain.com/info.php
Now look for “Loaded Configuration File” which where php.ini is located in your web server. Next step is to modify php.ini file to enable zlib compression.
# nano /etc/php.ini
Search for zlib.output_compression, default value should be off, to enable zlib, simple change to value off to on.
zlib.output_compression = On
Next we should set the zlib.output_compression_level value to 6 which is the best compression level without reduce your server preformance. The default value of zlib.output_compression_level is -1 which let your server choose which level to use. In php.ini file, search for
# zlib.output_compression_level = -1
Change its value to 6 and uncomment or remove “#” in front of zlib.output_compression_level
zlib.output_compression_level = 6
If your host does not support custom php.ini or you don’t have root access to your host, you can enable zlib by adding this code to your .htaccess at your website root directory
php_flag zlib.output_compression On php_value zlib.output_compression_level
Another method to enable zlib (gzip) to your website without alter php.ini or .htaccess is to add this code right into your PHP scripts before any output
ini_set('zlib_output_compression','On');
If you have installed Python but get this error
can't decompress data; zlib not available.
configure Python with
./configure --with-zlib=/usr/include
Finally run make command to generate the Python binaries.
# ./configure # make # make install