Using mod_alias to Share Assets

Posted by Pat Thu, 26 Jan 2006 07:19:00 GMT

In awstats – The Static Way, I showed you how to generate static stats pages using awstats. Shane quickly pointed out that the icons won’t work properly. I hadn’t noticed this, because awstats has very nice alt tags, but he’s absolutely right. There’s a much easier way to fix it though, and it involves using a nice (though apparently poorly documented) feature of our favorite webserver – mod_alias.

mod_alias is kind of like a simple mod_rewrite, but instead of rewriting the URL, it takes the incoming URL and maps it to a different location on the file system. This allows you to set up an single alias in your lighty config file, and use that path among all your sites. In the case of awstats, the HTML files all link to images in /awstatsicons/. If you followed my instructions though, that won’t exist. mod_alias to the rescue.

First just add mod_alias to the list of modules in lighttpd.conf. It’ll end up looking something (though perhaps not exactly) like this:
server.modules              = (
                                "mod_rewrite",
                                "mod_redirect",
                                "mod_proxy",
                                "mod_access",
                                "mod_fastcgi",
                                "mod_alias", # Add this line
                                "mod_compress",
                                "mod_accesslog" )
The next thing to do is set up an alias. In our case, we want /awstatsicons/ to be accessible to every site we run, and the icons are stored in /usr/local/www/awstats/icons. So in your config file, before any virtual hosts are defined, add this line:
alias.url = ( "/awstatsicons/" => "/usr/local/www/awstats/icons/" )

Now load up your stats page, and you’ll see the pretty icons!

Final Thoughts

As you can see, mod_alias is a pretty useful module for sharing static assets. If you have a bunch of images or CSS files that you want to share among a number of different web sites, you can simply use mod_alias to set up a common URL path. Pretty slick.

Posted in  | Tags , ,  | no comments

awstats - The Static Way

Posted by Pat Tue, 24 Jan 2006 23:27:00 GMT

awstats seemed pretty nice, but I didn’t really want to run it via CGI. I’m sure there’s nothing wrong with it…but one less server side process is one less point of attack, particularly if I didn’t write the process.

awstats provides a way of building static HTML pages instead of using a cgi script to view the stats. I wrote up a quick script that will generate stats for all my sites. Make it run whenever you want via cron, and you’re all set. Here’s the code:

awstats_build_stats.rb
#!/usr/local/bin/ruby

# Define the location of awstats, and the dir to output the stats pages to
perl = '/usr/bin/perl'
awstats = '/usr/local/www/awstats'
statscmd = "#{perl} #{awstats}/tools/awstats_buildstaticpages.pl"
statsdir = '/home/pergesu/www/stats'

# Get all the config files
configs = Dir["#{awstats}/cgi-bin/awstats.*.{com,net,org}.conf"]
siteregexp = /awstats\.(\S+)\.conf$/

# Loop through all the configs
configs.each do |conf|
  res = siteregexp.match(conf)
  site = res[1]
  # Create the dir if it doesn't exist
  Dir.mkdir("#{statsdir}/#{site}") unless File.exists?("#{statsdir}/#{site}") and File.directory?("#{statsdir}/#{site}")

  # Now run the command to generate the stats
  `#{statscmd} -config=#{site} -update -awstatsprog=#{awstats}/cgi-bin/awstats.pl -dir=#{statsdir}/#{site}`
end

I throw it all in a single stats dir because I have a couple sites that all have different docroots…they’re all in the same main /home/www/pergesu folder, but some have rails project folders which throws the naming off. So I just spit the output to a central stats dir, then I manually symlinked a stats folder in each site’s docroot to its appropriate awstats folder. I also symlinked index.html to the generated main awstats page (awstats.site.com.html) so I can just go to http://www.mysite.com/stats/ and view the stats.

One limitation here is that it doesn’t let me navigate between months or years to view past stats. There may be an option in the awstats_buildstaticpages.pl script, I’m not sure. For now, this works fine, and I don’t have to run a CGI process to view my stats.

Tags ,  | 1 comment

Cool stats with awstats

Posted by Pat Tue, 24 Jan 2006 06:30:00 GMT

There was a post on the Rails list the other day about installing awstats on lighttpd. If you don’t know, awstats is a log analyzer tool that runs through your web server logs and generates cool stats. Since I want to see how much traffic I’m (not) getting, I figured I’d install it. Should be a breeze, because I’m using FreeBSD :)

cantona# cd /usr/ports/www/awstats/
cantona# make install clean

Now awstats is installed to /usr/local/www/awstats/. Follow the instructions on the link above to finish..took me about 5 minutes. I’d like to see if somehow I can make stats.mydomain.org automatically append the config for the incoming hostname. That sure would be sweet..might have to hack the script up a little bit.

Tip: In your lighttpd config file, you may have virtual hosts set up with something like:

$HTTP["host"] =~ "(www\.)?mydomain\.com"

I was stumped for a minute because it kept matching “stats.mydomain.com” when I didn’t think it should. Anyway, since it’s using Perl pattern matching, the key is to make sure that you check to see that the domain starts with that, which you do using ^:

$HTTP["host"] =~ "^(www\.)?mydomain\.com"

Pretty simple..regular expressions sure are fun.

Posted in ,  | Tags ,  | 2 comments