The loading speed of a website is an important consideration for webmasters, not only in term of end-user browsing experience, but potentially also can make or break search engine ranking position (SERP), especially in Google, which started to take web page speed into consideration in its algorithm. One way to improve the serving speed of web pages is to enable GZIP compression to compress the size of web pages been transferred.

Compression reduces the size (bytes) of the web page that is required to be sent across the Internet, and can reduce response time as the size of HTTP response shrinks, as lesser information is transferred over the same network. Each HTTP requests can be served and completed faster by the web server, which in turn free up the process to serve other requests. Compression, which results in reduced web page size, also has added benefit of saving bandwidth. With all the benefits of GZIP compression, it effectively offsets the performance penalty on server load caused by the compression activity.

Most modern web browsers now support compression, which indicated via Accept-Encoding: gzip, deflate header in the HTTP request. GZIP is the most popular and most effective compression method available currently, and is the de-facto compress standard used by most web servers in the world. With GZIP, the size of HTTP response can potentially be reduced by a margin of around 80% after encoding. When receiving the compressed web page, web browser can uncompress it before presenting the page to end-users.

Use the how-to guide below to configure and enable GZIP compression on Apache web server. Apache’s compression is the recommended way when comparing with enabling GZIP (ZLIB output) compression on PHP scripts only. Note that Apache 1.3 uses mod_gzip, while Apache 2.x (2.0 and 2.2) uses mod_deflate. The guide focuses on Apache 2.x with mod_deflate to turn on GZIP support. For web hosts running on cPanel control panel, there is easier method to enable and turn on GZIP compression with Deflate module on cPanel.

Step 1: Enable mod_deflate

mod_deflate should have been installed together with Apache2, and mod_deflate can be enabled for Apache HTTPD web server in many ways. Firstly, check if the mod_deflate has been loaded by Apache with the following command:

apachectl -t -D DUMP_MODULES


httpd -t -D DUMP_MODULES

The output similar to below should be seen:

Loaded Modules:

deflate_module (static)

Syntax OK

If deflate_module is listed, it means the the mod_deflate has been loaded and enabled. Else, webmaster can choose to re-compile Apache to load mod_deflate statically with -enable-deflate switch.

Alternative for user who doesn’t want to recompile Apache is to load mod_deflate dynamically (shared). On Debian, administrator just need to run the following command:

a2enmod deflate

Alternatively, edit the Apache2’s configuration file manually to enable mod_deflate, by adding the following line to the LoadModule section (path may differ):

LoadModule deflate_module /usr/lib/apache2/modules/

Restart the Apache to make the change effective with the following command:

/etc/init.d/apache2 restart


/etc/init.d/httpd restart

Step 2: Configure mod_deflate

mod_deflate can be configured for all websites hosted by Apache on the web server globally, or on per virtual host or site basis, using the Apache configuration file, httpd.conf. To enable GZIP compression globally, enter the mod_deflate configuration settings on Apache’s global configuration section. To enable GZIP compression for a specific virtual host or site, append the mod_deflate configuration settings inside the VirtualHost or Location section.

There are two ways to configure mod_deflate: explicit inclusion or explicit exclusion.

To enable GZIP compression on only selected type of web pages and files, add the following line of codes:

AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript application/x-javascript

The above code explicitly enable GZIP compression of HTML, plain text, XML, CSS and JavaScript type of files only. The type of files are defined by MIME type. All other files will be served without compression. Change the MIME types accordingly when configuring mod_deflate. Some common MIME types that can be compressed are:

AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/atom_xml
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/x-httpd-php
AddOutputFilterByType DEFLATE application/x-httpd-fastphp
AddOutputFilterByType DEFLATE application/x-httpd-eruby
AddOutputFilterByType DEFLATE image/svg+xml

To enable GZIP compress for everything or all files served by Apache, exclude several file types, use the following line of codes (the lines starting with # is remark explaining the code, and can be removed accordingly):

<IfModule mod_deflate.c>
SetOutputFilter DEFLATE

<IfModule mod_setenvif.c>
# Netscape 4.x has some problems
BrowserMatch ^Mozilla/4 gzip-only-text/html

# Netscape 4.06-4.08 have some more problems
BrowserMatch ^Mozilla/4\.0[678] no-gzip

# MSIE masquerades as Netscape, but it is fine
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

# NOTE: Due to a bug in mod_setenvif up to Apache 2.0.48, the above regex won’t work. You can use the following
# workaround (comment the above line and uncomment the below line) to get the desired effect:
# BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html

# Don’t compress already-compressed files
SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI .(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI .(?:avi|mov|mp3|mp4|rm|flv|swf|mp?g)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI .pdf$ no-gzip dont-vary

<IfModule mod_headers.c>
# Make sure proxies don’t deliver the wrong content
Header append Vary User-Agent env=!dont-vary

Above code will compress all files except the files with extensions (URLs ends with) specified in SetEnvIfNoCase lines of code. The lines of BrowserMatch is to provide compatibility to some older browsers which may or may not support GZIP compression. The Header command instructs browser to append the new value (User-Agent) of the Vary header to any previous value of the Vary header (separating the new value from the old one by a comma) or to create a new value for the Vary header, if the dont-vary environment variable is undefined within the environment of the executing Apache service. It causes Apache to alert proxies that cached response should be sent only to clients that send the appropriate Accept-Encoding request header, and thus prevent compressed content from being sent to a client that does not understand it.

Note that above code is just to be used as example. Change the code accordingly to reflect the file types to exclude. It’s also possible to log mod_deflate operation with compression ratio details into a log file.

Once configuration is done, save the httpd.conf, and restar the Apache.

Do check and verify that GZIP compression is running properly.