AWStats Multi-Site Setup
Here's how to install and configure AWStats to compile separate statistics about multiple Apache sites or virtual hosts running on a single server. This can be done when you configure each Apache site or virtual host to use a different access log file.
The following instructions have been tested on Ubuntu 12.04.
Install
sudo apt-get install awstats
Configure AWStats for the default site
Edit the /etc/awstats/awstats.conf.local file and not the master configuration file. The main awstats.conf file is a good reference for all the configuration options, of which there are plenty.
The only thing you really need to start gathering stats for your default-site, is enter your web server's default site domain name /etc/awstats/awstats.conf.local:
SiteDomain="my.domain"
That is all that is needed for AWStats to make statistics out of the default site. We will add more configuration files later, one for each site or virtual host we want to get statistics for. But we must fix a couple of things first.
Fix log file permissions
AWStats update script will run every 10 minutes as the user "www-data" (see /etc/cron.d/awstats). The Apache log files are by default not readable by the user www-data, so something must be done about that.
Edit /etc/logrotate.d/apache2 and give Apache log file read permissions to everybody. This may not be a good idea on a server with untrusted users with shell access, but mine has none.
Change the following line:
create 640 root adm
To:
create 644 root adm
Save, and then change the permissions of the existing log files:
sudo chmod o+r /var/log/apache2/*.log
Also, give permission to access the directory:
sudo chmod o+x /var/log/apache2
Ensure logrotate does not skew your statistics
AWStats runs every ten minutes, but logrotate might rotate a log file whenever it gets full, which will cause AWStats to miss some hits.
To ensure AWStats update script is always run before a logfile is rotated, we need to add a script for that. Add the following directory:
sudo mkdir -p /etc/logrotate.d/httpd-prerotate
Add the update script:
sudo tee /etc/logrotate.d/httpd-prerotate/awstats << 'EOF' #!/bin/sh if [ -x /usr/share/awstats/tools/update.sh ]; then su -l -c /usr/share/awstats/tools/update.sh www-data fi EOF
Give execute permission:
sudo chmod ug+x /etc/logrotate.d/httpd-prerotate/awstats
At this point, you can run the statistics collector from the command line:
sudo su -l -c /usr/share/awstats/tools/update.sh www-data
If there are no error messages, you should now have up-to-date statistics collected. Let's get a site up and running to display the stats.
Configure Apache
Now that we have some real data, let's set up a location where we can actually take a look at it. There's an example Apache configuration file in /usr/share/doc/awstats/examples/apache.conf. But don't copy it directly to Apache configuration directory. I added something like this to my default site file under /etc/apache2/sites-enabled:
<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> <Directory /usr/share/java/awstats> Options FollowSymLinks AllowOverride None Order allow,deny Allow from all </Directory> <Directory /usr/lib/cgi-bin> AuthType Basic AuthName "Unauthorized use prohibited" AuthBasicProvider file AuthUserFile /etc/apache2/htpasswd/htpasswd.awstats Require valid-user </Directory> ScriptAlias /awstats.pl /usr/lib/cgi-bin/awstats.pl Alias /awstats-icon/ /usr/share/awstats/icon/ Alias /awstatsclasses/ /usr/share/java/awstats/
That will require a username and password to access the stats. It is a good idea to use an SSL-secured version of the default site, and use that instead of unsecured one. You can run "a2ensite default-ssl" to enable the default SSL site.
The above configuration will actually from now on require a password for all your cgi scripts dwelling under /usr/lib/cgi-bin, so if you have something there you wish not to protect, move the awstats.pl to somewhere else and secure that instead.
To add the username and password for access:
sudo mkdir -p /etc/apache2/htpasswd sudo htpasswd -c /etc/apache2/htpasswd/htpasswd.awstats myusername
Reload Apache configuration:
/etc/init.d/apache2 reload
And then point your browser to http(s)://my.domain/awstats.pl to see some statistics!
Next, let's move on to configuring separate statistics for multiple sites.
Configure AWStats for multiple sites
Place all additional configs in /etc/awstats/. Name the new configs "awstats." + domain.name + ".conf", eg. awstats.my.other.domain.conf.
In each file, include the main awstats.conf file. The first line should be:
Include "/etc/awstats/awstats.conf"
After that, we need to configure at least the SiteDomain directive, the location of the Apache log file to scan, and place to put the statistics data. For example:
Include "/etc/awstats/awstats.conf" SiteDomain="my.other.domain" HostAliases="my.other.domain www.my.other.domain" DirData="/var/lib/awstats/my.other.domain" LogFile="/var/log/apache2/my.other.domain_access_log"
Make sure you get the LogFile path right (take a look at your Apache configuration to find out where it should be).
Also, create the directory for the statistics:
sudo mkdir -p /var/lib/awstats/my.other.domain sudo chown www-data:www-data /var/lib/awstats/my.other.domain
The next time AWStats runs, within the next 10 minutes, it will collect statistics about this other site. If you are impatient, or want to see if you made any mistakes with your configuration, you can run it manually:
sudo su -l -c /usr/share/awstats/tools/update.sh www-data
Now we have data for this site.
Repeat the procedure for all other sites you have:
- Add a configuration file
- Create a stats directory with the right permissions
- Gather stats
Accessing statistics for other sites than the default-site
To see the statistics, browse to your default site's awstats.pl script, but give it a "config" url parameter:
http(s)://my.domain/awstats.pl?config=my.other.domain
You can also configure AWStats separately for each site's Apache configuration file, so you can use URLs like this: http(s)://my.other.domain/awstats.pl.