仝 tóng

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

f5ab3276115d9bc784d5a1d3378f7046_view.png

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

71162ca7c31bf17064e603d225e5561b_view.png

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

fe2dfb3441ca6b9e1005ffaac61c13ad_view.png

Second: Configure the cache
1.Config expires time
add next in your httpd.conf or .htaccess
<IfModule mod_expires.c>
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
</IfModule>

  1. config mod cache

  2. next in your httpd.conf or .htaccess

<IfModule mod_cache.c>
<IfModule mod_mem_cache.c>
CacheEnable mem /tmp
MCacheSize 4096
MCacheRemovalAlgorithm LRU
MCacheMaxObjectCount 100
MCacheMinObjectSize 1
MCacheMaxObjectSize 2048
CacheMaxExpire 864000
CacheDefaultExpire 86400
CacheDisable /php
</IfModule>
</IfModule>

  1. config mod_gzip

  2. next in your httpd.conf or .htaccess

<ifModule mod_gzip.c>
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.
</ifModule>
<ifmodule mod_deflate.c>
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
</ifmodule>
Save and restart your httpd or apache2