I'm running AWStats for my web servers (Apache 2) for several month now but I only take a look at those fancy statistics once in a while. After having accumulated more and more vhosts on my domain, the statistics started to become messed up. All vhost were logging into one access.log and one error.log. AWStats was running only one configuration instance and in the end it was impossible to get the statistics for only one vhost. I decided it was time to rework my whole approach on using logging and gathering statistics.

I'm still on Debian Sid so I'll use the awstats package from the repository:

aptitude install awstats

First, I made AWStats available to all vhosts by placing it's apache configuration in /etc/apache2/conf.d/awstats.conf where it get's included automatically on apache startup:

--(1036:Don,10 Apr 08:$)-- cat /etc/apache2/conf.d/awstats.conf
<Directory /var/lib/awstats>
Options None
AllowOverride None
Order allow,deny
Allow from all
</Directory>
<Directory /usr/share/awstats/icon>
Options None
AllowOverride None
Order allow,deny
Allow from all
</Directory>
Alias /server-awstats/icon/ /usr/share/awstats/icon/
ScriptAlias /server-awstats/ /usr/lib/cgi-bin/

The alias /server-awstats/ can be customized to any name you see fit. I choose it to later make sure that visits to AWStats pages don't get counted in the statistics.

Second, I granted every vhosts it's own log files, for example www.fladi.at:

--(1035:Don,10 Apr 08:$)-- cat /etc/apache2/sites-available/www.fladi.at
<VirtualHost *>
DocumentRoot /var/www/vhosts/www.fladi.at
ServerName www.fladi.at
CustomLog /var/log/apache2/access.www.fladi.at.log combined
ErrorLog /var/log/apache2/error.www.fladi.at.log
</VirtualHost>

Make sure, that the "combined" logging format is available! In /etc/awstats I copy the existing awstats.conf to awstats.www.fladi.at.conf:

cp /etc/awstats/awstats.conf /etc/awstats/awstats.www.fladi.at.conf

I modify the following directives in the new configuration /etc/awstats/awstats.www.fladi.at.conf:

LogFile="/var/log/apache2/access.www.fladi.at.log"
LogType=W
LogFormat=1
SiteDomain="www.fladi.at"
DirData="/var/lib/awstats/www.fladi.at"
DirCgi="/server-awstats"
DirIcons="/server-awstats/icon"
AllowAccessFromWebToAuthenticatedUsersOnly=1
AllowAccessFromWebToFollowingAuthenticatedUsers="__REMOTE_USER__"
SkipFiles="REGEX"

You can leave AllowAccessFromWebToAuthenticatedUsersOnly set to "0" if you don't want to make sure that authentication is required to view the statistics. But be warned that AWStats has had several critical vulnerabilities in it's past!

Now we need to create the new folder where the statistical data gathered from the logs will be stored. It's the directory I defined at "DirData":

mkdir /var/lib/awstats/www.fladi.at

To make things secure I create a user for running AWStats and make him member of the "adm" group:

useradd -r -d /var/lib/awstats/ -g adm awstats

My logfiles in /var/log/apache2 are readable by group adm (seems to be the default Debian setting) that's why I choose "adm" for the primary group of the user "awstats". Now make the new user the owner of the directory structure in /var/lib/awstats:

chown -R awstats:adm /var/lib/awstats

Now it's time to restart apache to get the new separated log files populated:

/etc/init.d/apache2 restart

Make sure that the new log files are in /var/log/apache2 and that they are readable by group "adm" (or any other group/user you choose to use for awstats). Also make sure that logrotate honors your decision of user/group in /etc/logrotate.d/apache2.

After a while, when there's some data in the new log files run the script /usr/share/doc/awstats/examples/awstats_updateall.pl which updates statistics for all configurations in /etc/awstats. This skript is part of the debian awstats package.

su -c "/usr/share/doc/awstats/examples/awstats_updateall.pl now \
-awstatsprog=/usr/lib/cgi-bin/awstats.pl -configdir=/etc/awstats/" awstats

If there are no errors in the output, you can set this up as a cronjob:

echo "*/10 * * * * awstats /usr/share/doc/awstats/examples/awstats_updateall.pl now \
-awstatsprog=/usr/lib/cgi-bin/awstats.pl -configdir=/etc/awstats/ >/dev/null" \
>/etc/cron.d/awstats

Now statistics will be updated every ten minutes.

To make the statistics show up on each vhost, the vhost config has to be extended by a simple block. In my case its th vhost for www.fladi.at again:

<VirtualHost *>
DocumentRoot /var/www/vhosts/www.fladi.at
ServerName www.fladi.at
CustomLog /var/log/apache2/access.www.fladi.at.log combined
ErrorLog /var/log/apache2/error.www.fladi.at.log
<Location /server-awstats/>
SetEnv AWSTATS_FORCE_CONFIG www.fladi.at
AuthType Basic
AuthBasicProvider ldap
AuthName "Statistik: www.fladi.at"
AuthLDAPURL "ldap://127.0.0.1/dc=fladi,dc=at?uid"
AuthLDAPGroupAttribute memberUid
AuthLDAPGroupAttributeIsDN off
Require ldap-group cn=admins,ou=groups,dc=fladi,dc=at
</Location>
</VirtualHost>

I use apache mod_env to pass an environment variable to all scripts in /server/awstats/ containing the name of the vhost which I want statistics to show up for. Besides this I set up authentication for this location using mod_authnz_ldap but mod_authn_file would also do.

Now reload apache:

/etc/init.d/apache2 reload

Point your browser to http://www.fladi.at/server-awstats/awstats.pl and enjoy your statistics.

To set this up for another vhost just replace the name (e.g. www.fladi.at with shop.fladi.at) in each configuration (apache and awstats) and setup the directory in /var/lib/awstats.