GIDBlog Learning Journal

Compress your PHP-FPM pages

Since the recent upgrade to, this web site is currently being hosted by various Docker containers. One of the Docker containers used is, of course, one derived from a Docker image like php:7-fpm-alpine.

I must confess that this is my first endeavour using PHP-FPM with Apache. All these years I have only been using the Apache module: mod_php to handle all my PHP web pages.

One of the areas I got stuck for a while (making this site work with PHP-FPM) is that PHP scripts — in this case generated web pages — were not sending gz-encoded data (compressed web pages) back to web browsers.

I know how to make Apache send compressed web pages and other documents using the Apache mod_deflate module.

I also know how to make individual PHP scripts turn output compression on.

Actually, reading that last link/page gave me the solution I was looking for – use the zlib.output_compression php.ini setting when creating my PHP-FPM Docker image.

So this was how my Dockerfile for the PHP-FPM/Alpine Linux image looks like with this setting all set up. See the last line:

File: Dockerfile

FROM php:7-fpm-alpine
RUN apk update; apk upgrade;
RUN docker-php-ext-install mysqli
RUN docker-php-ext-install opcache

# Other instructions removed for brevity #

RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"

# zlib compression
RUN echo "zlib.output_compression = On" >> "$PHP_INI_DIR/php.ini"

As soon as this Docker image was built again and running, all web pages generated by PHP-FPM for this web site are being compressed before being sent out to web browsers and clients that accept the encoding.

Learning Journal

Compress Your Web Page

In my never-ending quest to get my web pages to load super-fast  I chanced upon ob_gzhandler() one day while researching the uses of ob_start() in PHP.