How to enable gzip?

How to enable gzip?

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Enabling Gzip Compression on Apache: Beyond zlib.output_compression

The most common hurdle when attempting to enable gzip compression is conflating PHP's internal buffering with the server's HTTP headers. Your configuration shows ZLib Support enabled, which confirms that the necessary libraries are compiled into your environment. However, the directive zlib.output_compression is currently set to Off. This setting controls whether PHP automatically buffers output and compresses it using ob_gzhandler.

While enabling this specific PHP flag would technically compress your responses, it is generally not the recommended approach for production environments. When zlib.output_compression is active, PHP forces compression regardless of whether the client browser actually supports it. This means you waste CPU cycles encoding data that the browser will immediately decompress and discard, only to receive a second uncompressed chunk if the browser doesn't support it.

To achieve true efficiency, you should rely on Apache's native compression capabilities rather than forcing it from the application layer. The header _SERVER["HTTP_ACCEPT_ENCODING"] containing gzip, deflate indicates that your client (browser) is correctly requesting compressed content. Your server needs to respond to this request by setting the Content-Encoding: gzip header before sending the body.

Configuring Apache for Gzip

Since you are on a shared Dreamhost server running Apache, you likely cannot install or edit the main httpd.conf file directly. Instead, you must use an .htaccess file in your web root. This file allows you to override global settings without altering core server files.

To enable compression for text-based assets like HTML, CSS, and JavaScript, add the following rules to your .htaccess file:

<IfModule mod_deflate>
    AddOutputFilterByType DEFLATE text/html
    AddOutputFilterByType DEFLATE text/css
    AddOutputFilterByType DEFLATE text/javascript
    AddOutputFilterByType DEFLATE application/json
    AddOutputFilterByType DEFLATE application/xml
</IfModule>

The <IfModule> block ensures these settings only apply if the mod_deflate module is loaded. The AddOutputFilterByType directive tells Apache to apply the DEFLATE filter specifically to MIME types that match, leaving binary assets like images (image/png, image/jpeg) untouched. Compressing images results in a larger file size and increased CPU load without any perceptible performance gain.

Why PHP's zlib.output_compression is Problematic

You might wonder if you should simply change zlib.output_compression to On in your php.ini or through an Apache config that controls PHP settings. The answer is generally no, unless you have a very specific legacy application requirement.

When zlib.output_compression is On, the behavior works as follows:

  1. Unconditional Compression: PHP compresses all output streams before sending them to the client.
  2. Lack of Negotiation: It ignores the Accept-Encoding header sent by the browser. If a mobile device or an older browser does not support gzip, it receives a large compressed file and attempts to decompress it using its own CPU, potentially causing slowness or errors.
  3. Buffer Limitations: PHP has a default output buffer limit (usually 4KB). If your application generates larger responses, the compression might trigger prematurely or behave inconsistently depending on how output buffering is managed in your code.

By relying on mod_deflate, you leverage the web server's ability to negotiate compression with the client. Apache will only compress the response if it detects that the client supports it via the Accept-Encoding header. This offloads the decision-making to the protocol level rather than forcing it at the application layer.

Handling Specific Files and Dropbox Hosting

You mentioned hosting a JavaScript file from Dropbox and noticed it is not being transferred compressed. If you are serving these files directly from your web server (which integrates with Dropbox via a plugin or API), the .htaccess rules above will handle them automatically because text/javascript is explicitly listed in the filter types.

However, if you are streaming content or using a redirect mechanism that bypasses standard file serving, ensure the MIME type is correctly set to application/javascript or text/javascript. If the server detects an unknown content type, it might default to application/octet-stream, which often excludes files from compression filters configured for specific types.

Finally, verify that your PHP scripts do not manually disable output buffering in a way that conflicts with these settings. While zlib.output_compression handles buffering automatically when on, mixing manual ob_start() calls with server-side compression can sometimes lead to double-compression or unexpected header errors. Sticking to server-level configuration for static assets and letting PHP handle dynamic content without forcing compression is the most robust architecture.

If you are using a Laravel application or similar framework on this hosting environment, ensure your .htaccess file also includes rules to prevent directory listing and enforce HTTPS, as these often go hand-in-hand with performance optimizations found in resources like https://laravelcompany.com.

Our network