Install and enable zlib on linux server

Zlib-Gzip

Install and enable zlib on linux 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 uses deflate() and inflate() algorithm to compress and decompress files.

To speed up any 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:

It is installed in web server by default, for some reason if you don’t have zlib installed, here is the command to install it 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

 

Data length
There is no limit to the length of data that can be compressed or decompressed. Repeated calls to the library allow an unlimited numbers of blocks of data to be handled. Some ancillary code (counters) may suffer from overflow for long data streams, but this does not affect the actual compression or decompression.

When compressing a long (or infinite) data stream, it is advisable to write regular full-flush points.

About This Author

I'm a software engineer with many years of experience, open source enthusiast, now I'm creating and contributing to awesome PHP web projects. I love coding as much as learning, and I enjoy trying new languages and patterns. My passion revolves around (but is not limited to) back-end development.

You are not signed in. Sign in to post comments.