The mod_deflate module allows output from your Apache web server to be compressed before being sent to the browser. This reduces the size of the content that is required to be sent, thus decreases the amount of data and time transmitted to the browser.
This article shows you how to improve website speed by enabling mod_deflate on Apache 2.2.x Web server.
Compile and Enable mod_deflate and mod_headers Modules
To begin, you will need to have two Apache modules loaded, you can verify by viewing the “Loaded Modules” under “apache2handler” configuration via phpinfo. Ensure mod_deflate and mod_headers modules are there.
If they are not there, you can either re-configure Apache to have them linked statically or load as DSO (Dynamic Shared Object).
Turn mod_deflate Compression On
Open httpd.conf file using a text editor and place the following in it:
# mod_deflate configuration <IfModule mod_deflate.c> # Restrict compression to these MIME types AddOutputFilterByType DEFLATE text/plain AddOutputFilterByType DEFLATE text/html AddOutputFilterByType DEFLATE application/xhtml+xml AddOutputFilterByType DEFLATE text/xml AddOutputFilterByType DEFLATE application/xml AddOutputFilterByType DEFLATE application/x-javascript AddOutputFilterByType DEFLATE text/javascript AddOutputFilterByType DEFLATE text/css # Level of compression (Highest 9 - Lowest 1) DeflateCompressionLevel 9 # 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 \bMSI[E] !no-gzip !gzip-only-text/html <IfModule mod_headers.c> # Make sure proxies don't deliver the wrong content Header append Vary User-Agent env=!dont-vary </IfModule> </IfModule>
Use AddOutputFilterByType directive to define the MIME type to be compressed and DeflateCompressionLevel directive for the compression level.
When you are done with the configuration, restart Apache.
/etc/init.d/apachectl restart
To verfiy whether mod_deflate compression is working, check the response headers of your site for this line.
Content-Encoding gzip