mattdorn.com

Generously funded by Matt Dorn

Automating AWStats configuration for multiple domains

without comments

Like most Web sites, this site shares server space with a number of other domains. When I recently undertook to set up AWStats after years of not knowing anything about what kind of traffic my personal sites were getting, I figured it would probably be relatively easy to make it so that anybody else with an account on the box who also wanted stats could avoid the hassle.

With a bit of help from another tutorial, I came up with the following solution.

First of all, I installed the AWStats package on my server’s Fedora system via yum. With that installation, I ended up with AWStats’ default config file in /etc/awstats/awstats.model.conf.

This setup needs a separate AWStats configuration file for each domain, so this default file is the model from which all others will be generated. For that reason, I needed to tweak a few of the default values to work the script described later:

LogFile="/home/httpd/$USERNAME/logs/%MM-0-%YYYY-0/access_log"
SiteDomain="$DOMAIN"
HostAliases="$ALIASES"
DirData="/home/httpd/$USERNAME/awstats"
AllowAccessFromWebToFollowingAuthenticatedUsers="$USERNAME"

(I should also note that I enabled the GeoIP plugin in this file, but that’s a subject for a different post.)

Now, on the Apache side of things, you need to decide on a standard location for the Apache log files for all the accounts. In my case, that’s /home/httpd/ACCOUNT_NAME/logs. (In that directory, the setup described below will generate directories named by month and year (like so: MM-YYYY), and put the log file there.) The Apache virtual host directives of course will need to specify that location. Since I don’t actually know or have any communication with my neighbors who share the box, I’ll leave that to them, but my new directives look like this:

CustomLog "|/usr/local/sbin/cronolog /home/httpd/DOMAIN/logs/%m-%Y/access_log" combined

Where DOMAIN is the name of the directory that holds the Web files (htdocs, logs, etc.) for a given domain on the server.

I’ve also installed cronolog as an easy way to rotate logs. As you can see the Apache config pipes to that program. That’s not strictly necessary for this setup, but if you want to do it differently (e.g., with logrotate), you’re on your own.

Making use of this information, anytime you want AWStats set up for a new domain, you can use the following script, which is named /usr/local/sbin/awstats_script.sh in my setup, and which was adapted from another tutorial , mentioned previously:

#!/bin/bash
echo "Enter the username:"
read WUSERNAME

ACCESS_FILE="/etc/awstats/awstats.pwd"
STAT_DIR="/home/httpd/$WUSERNAME/awstats"

echo "Enter the password:"
read PASSWORD

echo "Enter the main domain:"
read DOMAIN

echo "Enter aliases separated by space:"
read ALIASES

# Create the statistics directory
if [ -d $STAT_DIR ]; then
    echo "Statistics dir already exist"
else
    mkdir $STAT_DIR
fi

# Create the virtual host awstats.conf
cat /etc/awstats/awstats.model.conf | \
sed -e "s/\\\$DOMAIN/$DOMAIN/g" | \
sed -e "s/\\\$USERNAME/$WUSERNAME/g" | \
sed -e "s/\\\$ALIASES/$ALIASES/g" > \
"/etc/awstats/awstats.$DOMAIN.conf"

# Add user/password to password file
if [ -e $ACCESS_FILE ]; then
    /usr/bin/htpasswd -bm $ACCESS_FILE $WUSERNAME $PASSWORD
else
    /usr/bin/htpasswd -bm -c $ACCESS_FILE $WUSERNAME $PASSWORD
fi

The "username" requested is the name found in the /home/httpd directory for which you want to create the site. This will also be the username needed to login (via Apache authentication) to view the stats. The "password" then requested is for that same Apache authentication (will be stored in /etc/awstats/awstat.pwd). The "main domain" is what people use to access the site, (e.g., www.DOMAIN.com), but you’ll also then be asked to include any subdomains that log to the same Apache log file (at least if you want AWStats to process them).

After the setup is complete, you can check out your stats by going to: http://www.DOMAIN.com/awstats/awstats.pl

AWStats knows which config file (automatically generated) it needs by reading the domain from the URL, but you can also view other domains by appending "?config=www.whatever.com" to the URL. That config file is left in /etc/awstats, and the individual AWStats DB file that results from processing the logs is left in each user’s directory in /home/httpd/DOMAIN/awstats

While each user will be able to HTTP-authenticate against any other user’s domain, the values in the AllowAccessToWebToFollowingUsers variable will prevent users from being able to view one another’s stats.

Finally, note that your AWStats installation may have included a cron task to update the AWStats data in /etc/cron/hourly or other. In any case, you’ll want AWStats to automatically process your logs on an at least daily basis via the included script /usr/share/awstats/tools/awstats_updateall.pl.

Written by mdorn

October 15th, 2006 at 6:34 pm

Posted in technology

Tagged with , ,

Leave a Reply