Advertisment

Give Your Apache a Performance Boost

author-image
PCQ Bureau
New Update

Caching of web pages is regularly done to speed up access. Let's suppose you

have an Apache web server deployed locally. By deploying mod_cache you can get

the server to regularly update the contents of frequently visited sites. This

way the visitor has access to the latest version of a page as soon as he visits

it and Apache is spared the need to check for it real time. This saves a lot of

bandwidth for enterprises. Apache 2.x also provides the option of integrating

'deflate' module, that allows one to improve the downloading speed by

compressing the downloaded packets.

Advertisment

How the server uses these modules is actually pretty simple. It first checks

if the URL is present in its cache or not. It the URL is present and the version

has not expired, then it serves the page directly from the cache else the page

is loaded from the respective server. In such a scenario (while the page is

being served directly from the server), Apache applies filters to the page to

check if it contains cacheable content or not. In case it's cacheable, then the

web page is cached by mod_cache for future use. It makes use of two other

modules: mod_disk_cache and mod_mem_ cache. Let's see how you can implement this

in real life scenario. Before we start, make sure that mod_cache module is

installed. For this, run the following command, '/usr/sbin/httpd -M.'

This will display all modules that are installed in the Apache module set.

Check if cache module is present or not. If not, then download 'Apache cache

module' rpm package from rpmfind.net and install it on your web server using the

following command:

Direct Hit!

Applies To: Web admins



Price: Free


USP: Learn how to implement mod_cache and
Deflate



Primary Link: www.apache.org


Keyword: mod_cache, deflate


Advertisment

# rpm —ivh

After you have installed the module, it's time to configure the configuration

file of Apache. This is to tell Apache that it should load the cache module and

use it. Uncomment the following lines in httpd.conf file, in case the line is

marked as a comment; this would allow Apache to load the cache module. The file

can be found at the location: /etc/httpd/ conf/httpd.conf.

LoadModule cache_module modules/mod_cache.so



LoadModule disk_cache_module modules/mod_disk_cache.so


LoadModule file_cache_module modules/mod_mem_cache.so

Advertisment

Apart from the mod_cache module the server needs two other modules:

mod_disk_cache and mod_mem_ cache. Mod_disk_cache is used to for disk based

caching or storing and mod_mem_cache is used for memory based caching. The

second caching option should be used when the content is generated locally. Now

place the following lines into httpd.conf:





CacheRoot /var/www/cache


CacheEnable disk /


CacheDirLevels 5


CacheDirLength 3













CacheEnable mem /


MCacheSize 4096


MCacheMaxObjectCount 100


MCacheMinObjectSize 1


MCacheMaxObjectSize 2048










In the first module we have defined where the content is to be cached, ie at

'/var/www/cache'. Then we enabled the caching option; defined the directory
level, ie the depth of the directory; and finally the number of characters in a

subdirectory name. In the second module, ie mod_mem_cache, we first enabled the

caching option then defined the size of cache inside the memory. Next, we

defined the maximum objects that can be stored in the memory and the min and max

size of the object. This has to be defined so that the memory is correctly used

else caching a single file of 1 GB in memory can't be considered. Finally, you

enabled all the caching options in Apache. For cases where the page is not

cached and is served directly from the server, you can implement the Deflate

module. It is a data compression algorithm that uses the combination of LZ77

algorithm and Huffman coding.

Advertisment

This will help you in case your content is being downloaded directly from the

server. The content is first compressed then sent to clients, which makes data

transfer faster over the network. Before we start, check whether the Deflate

module is installed or not. For checking, use the same method as used for

mod_cache. If the module is not installed, download the Deflate module rpm from

rpmfind.net and install it in the same way as you installed other modules. Once

the module has been installed, copy the following line inside the 'Location' tag

in the httpd.conf file:

AddOutputFilterByType DEFLATE text/html text/plain

text/xml

This will enable Apache to compress all HTML and XML files. But if you want

to enable compression for other types of data such as images and scripts, into

Apache, then type the following lines as well:

Advertisment





AddOutputFilterByType DEFLATE text/plain


AddOutputFilterByType DEFLATE text/xml


AddOutputFilterByType DEFLATE application/xhtml+xml


AddOutputFilterByType DEFLATE text/css


AddOutputFilterByType DEFLATE application/xml


AddOutputFilterByType DEFLATE image/svg+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 text/html















Finally you have a full set of solutions in place for serving web pages

faster to the clients. Apart from this, there are directives to optimize

mod_cache. For example CacheMaxFileSize and CacheMinFileSize help you in

defining the minimum and maximum file size that needs to be cached on the disk.

The syntax would be (numbers specified in bytes):

CacheMaxFileSize 20000



CacheMinFileSize 2000

MCacheMaxStreamingBuffer is another such directive for mod_mem_ cache. This

can be used when there is some streaming data to be cached. As you cant predict

the size of the data being streamed, you inadvertently end up having falling

short on cache. Using this directive you can specify the maximum size of the

buffer for streaming data. So the moment the buffer gets full, it discards any

further data; hence you are spared of having your cache space getting saturated.

This is useful as the streamed data doesn't have Content-Length header which

tells the size of the data. Syntax for implemenging the buffer is,

'MCacheMaxStreamingBuffer 10000.'

Advertisment