How To Optimize Apache For Sites To Load Faster

How To Optimize Apache For Sites To Load Faster

If you have a cloud server, dedicated server or VPS with root access, you can optimize Apache so that your website loads faster. Here is how you can do it.

1. Use compression by using mod_gzip

Download mod_gzip.

wget http://heanet.dl.sourceforge.net/sourceforge/mod-gzip/mod_gzip-1.3.19.1a.tgz

Extract the tgz file, compile and install.

tar xzvf mod_gzip-1.3.19.1a.tgz

[path to your]apxs -c mod_gzip.c

[path to your]apxs -i -a -n gzip mod_gzip.so

Edit your Apache configuration.

<IfModule mod_gzip.c>

mod_gzip_on Yes

mod_gzip_dechunk Yes

mod_gzip_item_include file \.(html?|txt|css|js)$

mod_gzip_item_include handler ^cgi-wrap$

mod_gzip_item_include mime ^text/.*

mod_gzip_item_include mime ^application/x-javascript.*

mod_gzip_item_exclude mime ^image/.*

mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*

</IfModule>

 

2. Encourage the client to cache by using mod_expire and mod_header.

They are available in the Apache source, under

src/modules/standard

Go to that directory.

Compile and install.

[path to your]apxs -c mod_expires.c

[path to your]apxs -i -a -n expires mod_expires.so

[path to your]apxs -c mod_headers.c

[path to your]apxs -i -a -n headers mod_headers.so

Edit your Apache configuration.

<FilesMatch “\.(ico|flv|jpe?g|png|gif|js|css|swf)$”>

ExpiresActive On

ExpiresDefault “access plus 1 month”

</FilesMatch>

3. Do not load unnecessary modules.

This will make your Apache run faster. Depending on the usage of the web server, the unnecessary modules varies from case to case. For example, WordPress typically needs these modules:

mod_dir

mod_log_config

mod_mime

mod_setenvif

mod_alias

mod_authz_host

mod_rewrite

 

You might want to consider commenting out the other modules that you do not need if you are running WordPress.

4. Experiment with the “KeepAlive”

If you site is full out scripts, you want to set “KeepAlive” as “off”. This is because there is no point making Apache keep a connection alive (and thus wasting resources) if the client’s request has already finished executing. Better to close the connection and use the resource to serve connections that are actually doing something.

If your site is full of images and javascripts, you will want to set “KeepAlive” as “on”, and consider other settings as well. “KeepAliveTimeout” should be kept small, like “5”. You want to keep the connection open long enough for all the data to flow to clients, but not so long that the connection is kept alive and doing nothing. “MaxKeepAliveRequests”, set this higher, maybe twice the default whereabouts. This allows more requests to come through the connection, thus making things run more efficiently.

5. Last but not least, increase your resources.

Getting more RAM and bandwidth will definitely make your web pages load faster.

If you are coding in PHP, you can use function “memory_get_usage” to see how much memory your script takes.

You can use “Firebug” (http://getfirebug.com/), an extension of the Firefox browser, to see which parts of your web site are taking to the longest to load. Sometimes, it could be just an image that you forgot to scale down to size. No point loading a 500K image for a thumbnail, for example.

(The last 2 points does not optimize Apache per-se, but they are things that you check on to help make your web site load faster.)

 

Happy configuring!

About the Author
Announcement: We wish to inform you Signetique will now be operating under Exabytes. Click here for more information.