If you’re running Apache2 + PHP in a configuration with mod-apache2filter instead of the default mod-php5 (apache2handler), you might soon find yourself unable to activate the gzip options in Apache. You will be able to use the output buffers of PHP to enable gzip, but it won’t work via Apache.
Here’s what happens when you run Apache2 with the apache2filter option.
# curl -I "http://site/page.php" -H "Accept-Encoding: gzip,deflate" HTTP/1.1 200 OK Server: Apache Accept-Ranges: bytes Transfer-Encoding: chunked
The example above shows that a default apache2filter will send output as ‘chunked’ bits, in pieces. For Gzip to work, Apache needs to receive the entire request, compress it with gzip, and then send it along to the end-user if it accepts it (the Accept-Encoding header that the user sends along).
Implementing mod-php5 instead of apache2filter is easy, as it is an in-part replacement. Simply installing mod-php5 will remove apache2filter without damage to your PHP scripts. Here’s how.
# apt-get install libapache2-mod-php5
That will perform the following actions (give or take a few, depending on your config):
Reading package lists... Done Building dependency tree Reading state information... Done The following extra packages will be installed: php5-cli php5-common php5-curl php5-gd php5-mysql Suggested packages: php-pear php5-suhosin The following packages will be REMOVED: libapache2-mod-php5filter The following NEW packages will be installed: libapache2-mod-php5 The following packages will be upgraded: php5-cli php5-common php5-curl php5-gd php5-mysql 5 upgraded, 1 newly installed, 1 to remove and 36 not upgraded. Need to get 6,749 kB of archives. After this operation, 4,096 B of additional disk space will be used. Do you want to continue [Y/n]? Y
After the install, copy the php.ini from the Apache2Filter to the mod-php5 configuration, so any changes you made to the apache2filter config will also be in place for the new mod-php5.
# diff /etc/php5/apache2/php.ini /etc/php5/apache2filter/php.ini # cp /etc/php5/apache2/php.ini /etc/php5/apache2/php.ini.backup # cp /etc/php5/apache2filter/php.ini /etc/php5/apache2/php.ini # /etc/init.d/apache2 restart
By now, your results should be better, if your Apache config is OK.
# curl -I "http://site/page.php" -H "Accept-Encoding: gzip,deflate" HTTP/1.1 200 OK Server: Apache Accept-Ranges: bytes Vary: Accept-Encoding,User-Agent Content-Encoding: gzip Content-Length: 20 Content-Type: text/html; charset=utf-8
So if you’re ever struggling with apache2filter and gzip, try to replace it with the mod-php5 implementation. Chances are, that will fix your problem.