Configuring Apache for speed

In ramping up for an alpha / beta test, one of the things I was testing was speed on the server the software was to be deployed on. Running Yahoo's ySlow, it gave me all kinds of input, non g-zipped components, missing header expiration dates on my files, etc. I wanted to share here with you some of the apache vhost configuration changes I made that made the site just *fly*! I am no apache expert, so there may be different ways to do this. But I can tell you, after making these changes the site FEELS a ton faster.

PLEASE! - before you make these changes, backup your httpd.conf file! I would hate to be the cause of you hosing your configuration!

First, lets look at getting our css and js assets to be g-zipped before they are served out. You will need to remove the # in front of the

LoadModule deflate_module modules/mod_deflate.so
line in your apache httpd.conf file. Then, inside your <directory> tag, put the following lines (as needed to dictate what you want to be compressed) and re-start your apache service.

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/javascript
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

Next, lets set expiration dates far into the future for any assets that we want to be cached for a long time in the clients browser cache. One thing to note, if you make a change to CSS after doing this, you will need to change the file name for the users browser to get the new file! Yahoo recommends adding version information to your CSS / JS files, so each change gets a new file name and re-cached! There are a ton of files set to cache in the far future here, you may want to only direct js and css.

<FilesMatch <span class='cc_value'>"\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$"</span>>
Header set Expires <span class='cc_value'>"Thu, 15 Apr 2099 20:00:00 GMT"</span>
</FilesMatch>

One issue we had to address was sharing an asset root between multiple web servers. You can add an alias command to point a virtual directory to a UNC path on your network (be sure the user account that is running apache has access to the UNC path) using:

Alias /folder/subfolder/ <span class='cc_comment'><span class='cc_value'>"//servername/sharename/"</span></span>

Now, this could cause high network traffic if you are pulling many assets off this network path, so we setup a disk cache command so that everything being pulled from /folder/subfolder gets cached locally on the web server:

<IfModule disk_cache_module>
   CacheRoot C:/ApacheCache
   CacheEnable disk /folder/subfolder/
   CacheDirLevels 5
   CacheDirLength 3
   CacheMaxFileSize 4096000
</IfModule>

Last but not least, we didn't want the web server to have to keep opening (from the filesystem) the CSS and JS assets, so we configured an in-memory cache for those files with this directive:

<IfModule mem_cache_module>
CacheEnable mem /folder/javascript/
CacheEnable mem /folder/css/
MCacheSize 8192
MCacheMaxObjectCount 100
</IfModule>

These tweaks have made a measurable difference in the responsiveness of Apache 2.2 on windows, and I hope they help someone else tweak a great HTTP service to be the fastest it can be!

Digg StumbleUpon Facebook Technorati Fav newsvine reddit FARK Google Bookmarks
  1. Zac Spitzer

    #1 by Zac Spitzer - September 25, 2008 at 1:35 AM

    good tips :)

    check out http://nginx.net/ it blows apache out of the water and is extremely lightweight.... something in the order roughly 3-5 times faster which means you can handle a lot more load on the same hardware

    there is also a windows port

    I would probably err on the just slightly more conservative side of things when it comes to the expires date
  2. Justice

    #2 by Justice - September 25, 2008 at 6:15 AM

    Thanks for the feedback Zac. I will have to checkout nginx, the main thing I would have to figure out with that is hooking Coldfusion too it. =)

    As for the expiration headers, I would rather have it say 'access + 1 month' or something like that, but I didn't see that when I was looking around on how to configure with apache 2.2. I will dig around some more, as I would much rather have a variable expiration than having to hard code something!
  3. Fred T. Sanders

    #3 by Fred T. Sanders - November 19, 2008 at 7:32 AM

    Thought I'd come look at your blog after Jared R-H is campaigning to
    get you inducted on a private CF list I belong to. I found this topic interesting and thought I'd point you in another direction for some of your caching attempts with apache 2.x.

    You'll want to look at mod_expires.
    After enabling it, I believe you'll need to issue an "ExpiresActive On",
    I'm pretty sure that's still required even in V2.x of Apache.

    For caching of an item by Access + 1 month in your vhost settings.

    You'd do something like this:
    Its an example, I would probably laugh if you had the minutes and seconds defined, I might even laugh about the hours setting.

    ExpiresActive On
    #New School:
    ExpiresByType text/css "access plus 1 month 2 days 3 hours 4 minutes 5 seconds"
    #Old School:
    ExpiresByType text/css A2592000 # Access + 30 days

    Or By Modified date/time:
    #New School:
    ExpiresByType text/css "modification plus 1 month 2 days 3 hours 4 minutes 5 seconds"
    #Old School:
    ExpiresByType text/css M2592000 # Modified + 30 days

    Also putting those in order of most requested will help, a little.
    Right after these keep your <filetypes> tag but change it to:
    (Also notice the changed jpg|jpeg)
    <FilesMatch "\.(ico|pdf|flv|jpe?g|png|gif|js|css|swf)$">
       Header set Cache-Control "public"
    </FilesMatch>

    Or if your REALLY anal:

    <FilesMatch "\.(ico|pdf|flv|jpe?g|png|gif|js|css|swf)$">
       Header set Cache-Control "public,max-age=2592000"
    </FilesMatch>

    Have fun at MAX & hope you make it onto CFGURU :),

    Fred

    P.S.: Your cfm fed captcha totally bombed in the Google Chrome browser, I'll try to forgive you for making me start up IE for this. :)
  4. Justice

    #4 by Justice - November 19, 2008 at 7:38 AM

    Fred,

    Thanks for the advice, I'm actually doing some initial configuration today on my just-in production servers, so your changes are greatly welcome! Sorry about the Captcha, I will investigate why that is happening!
  5. Fred T. Sanders

    #5 by Fred T. Sanders - November 19, 2008 at 7:52 AM

    Oh almost forgot to comment on this:

    "PLEASE! - before you make these changes, backup your httpd.conf file! I would hate to be the cause of you hosing your configuration! "

    I reall hope your not actually keeping vhosts in your httpd.conf file!!

    make a vhosts folder, and keep one conf per site, then have this in your httpd.conf file:

    Include conf/httpd-vhosts.conf

    That should probably have a:

    NameVirtualHost IP_OF_YOUR_WEBSITES

    and either put your vhosts in here, or if you have a lot of them,
    keep them in seperate files in a vhosts directory and:
    include path_your_vhosts_folder/*.conf


    Of course if your not doing that and are just warning people that are doing it, then please kindly disregard. :)
  6. Fred

    #6 by Fred - May 15, 2009 at 5:23 PM

    That has got to be to one of the funniest pieces of link bait I've ever seen. Go Black Hat SEO!! LOL

Comments are closed.