Optimize website speed with mod_gzip, mod_cache and mod_mem_cache

If you enable your server gzip and mod cache, your site speed will improve 30% at least.
First: install mod_file_cache, mod_mem_cache
mod_cache and mod_disk_cache are the default package in apache, so we don’t need to install it.

  1. Download Apache package at http://httpd.apache.org/download.cgi

$ wget url_of_httpd-2.2.27.tar.gz (you can change to httpd-2.4.9.tar.gz for your apache version)
$ tar -zxvf httpd-2.2.27.tar.gz
$ cd httpd-2.2.27/modules/cache
$ yum install httpd-devel –y (you can use “apt-get install apache2-dev” if your server is ubuntu or debian)
$ rpm –ql httpd-devel | grep apxs
it is based on centos, please verify your path after this command
$ find . -name “_cache.c” -print
install and activate mod_file_cache.c
$ /usr/sbin/apxs -iac mod_file_cache.c cache_cache.c cache_hash.c cache_pqueue.c cache_storage.c cache_util.c


install and activate mod_mem_cache.c
$ /usr/sbin/apxs -iac mod_mem_cache.c cache_cache.c cache_hash.c cache_pqueue.c cache_storage.c cache_util.c


you will find mod_mem_cache.c mod_and file_cache.c in /etc/httpd/conf/httpd.conf file


Second: Configure the cache
1.Config expires time
add next in your httpd.conf or .htaccess

ExpiresActive On
ExpiresDefault A86400
ExpiresByType text/css A2592000
ExpiresByType text/js A2592000
ExpiresByType image/x-icon A2592000
ExpiresByType image/ico A2592000
ExpiresByType image/gif A2592000
ExpiresByType image/png A2592000
ExpiresByType image/jpg A2592000
ExpiresByType image/jpeg A2592000
ExpiresByType text/plain A86400
ExpiresByType text/html A86400
ExpiresByType application/pdf A2592000
ExpiresByType video/x-flv A2592000
ExpiresByType application/x-javascript A2592000
ExpiresByType application/x-shockwave-flash A2592000

  1. config mod cache

  2. next in your httpd.conf or .htaccess

CacheEnable mem /tmp
MCacheSize 4096
MCacheRemovalAlgorithm LRU
MCacheMaxObjectCount 100
MCacheMinObjectSize 1
MCacheMaxObjectSize 2048
CacheMaxExpire 864000
CacheDefaultExpire 86400
CacheDisable /php

  1. config mod_gzip

  2. next in your httpd.conf or .htaccess

mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.gzip.

SetOutputFilter DEFLATE
DeflateCompressionLevel 9
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/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 image/svg+xml
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 .(?:pdf|mov|avi|mp3|mp4|rm)$ no-gzip dont-vary
AddOutputFilterByType DEFLATE text/*
AddOutputFilterByType DEFLATE application/ms application/vnd application/postscript application/javascript application/x-javascript
AddOutputFilterByType DEFLATE application/x-httpd-php application/x-httpd-fastphp
BrowserMatch ^Mozilla/4 gzip-only-text/html # Netscape 4.x
BrowserMatch ^Mozilla/4.0[678] no-gzip # Netscape 4.06-4.08
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

Save and restart your httpd or apache2

Leave A Comment

Your email address will not be published. Required fields are marked *