Wednesday, February 22, 2012

Configuring awstats

I recently moved my web server from Apache 1.3.x to 2.2.3 and thought it would be a good time to get awstats configured. It had been installed for a long time and was pretty useless without doing more than ‘aptitude install awstats’. Anyway, reading the docs and the README.Debian file made it seem like it was going to be a huge pain. It wasn’t. I should know by now that there is an inverse relationship between amount of documentation and difficulty.

These notes are specific to Debian, so adjust accordingly. Install awstats:

aptitude install awstats

Set your log files to be output in the combined format:

ErrorLog /var/log/apache2/mydomain_error.log
CustomLog /var/log/apache2/mydomain_access.log combined

I have a number of virtual hosts, so I have that defined for each host within the <virtual> block.

The install script for awstats did its best to figure out what I had and write a default awstats.conf file, but it wasn’t nearly correct. Copy that into files named for your domains, like this:

cp awstats.conf awstats.mydomain.com.conf

Then copy awstats.conf to awstats.model.conf:

cp awstats.conf awstats.model.conf

That file gets ignored and the name reminds you that it’s a template. Edit each of the other .conf files to point to your log files, log type, log format, site domain, host aliases, DNS lookups, etc. There is an optional setting called AllowAccessFromWebToFollowingIPAddresses where you can define who can see your cgi page. I set this to my network range, but I also set it in Apache because I trust that one to be a stronger security measure. CGI gives me the willies. Anyway, the file is heavily commented so check it out and set what you need to. Once you have them set, the docs say to run the awstats.pl script on each one manually the first time:

/usr/lib/cgi-bin/awstats.pl -config=mydomain.com -update > /dev/null

Once you have the config files set, you have to allow the user running awstats to read the log files. By default, that is www-data, which does not have access to the logs. You can change it in /etc/logrotate.d/apache2, so that www-data has group ownership. This may not be the best thing to do and there are some other options which are spelled out in the README.Debian file. The other change you’ll have to make is to the default cron script installed in /etc/cron.d/awstats. It is only set to run on awstats.conf, which won’t give you anything. You can comment out that line, copy it, and adjust it for each of your domains, or you can put this script somewhere and have it parse your awstats config directory:

#!/bin/bash
for conf in `/bin/ls /etc/awstats | /bin/sed 's/^awstats.//' | /bin/sed 's/.conf$//'`
do
if [ $conf = 'model' -o $conf = 'conf.local' ]; then
continue
else
access=${conf%%.*}'_access.log'
if [ -x /usr/lib/cgi-bin/awstats.pl -a -r /var/log/apache2/$access ]; then
/usr/lib/cgi-bin/awstats.pl -config=$conf -update > /dev/null
fi
fi
done

That script is tailored for config files that are named like

awstats.mydomain.com.conf

and log files that are named like

mydomain_access.log

Adjust it to fit your naming scheme. The big benefit is that it’ll examine your directory and run it for each domain configured and you won’t have to remember to add it to the cron script manually.

One last thing (for now, anyway) is a prerotate script in /etc/logrotate.d/apache2. There is a warning in the README.Debian file about losing time in the awstats database due to logs being rotated before the last update of the day is made. Add this to the file

prerotate
/usr/local/bin/awdomlist
endscript

Just above the postrotate entry. I don’t know that it makes any difference, but it seems like a ‘pre-’ ought to go before a ‘post-’.

Copy /usr/share/doc/awstats/examples/apache.conf to /etc/apache2/conf.d/awstats (or whatever) and read the comments in the file. It is set so that anyone (!) can execute stuff in /usr/lib/cgi-bin, so you probably want to limit that using the ‘Allow from’ directive.

Check it out in a web browser at mydomain.com/cgi-bin/awstats.pl and check out the pretty graphs, sure to impress your boss. The docs say the path is mydomain.com/awstats/awstats.pl, but the configuration snippet included for Apache aliases /usr/lib/cgi-bin to /cgi-bin.

No comments:

Post a Comment