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.
Table of Contents
Install
1 |
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:
1 |
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:
1 |
create 640 root adm |
To:
1 |
create 644 root adm |
Save, and then change the permissions of the existing log files:
1 |
sudo chmod o+r /var/log/apache2/*.log |
Also, give permission to access the directory:
1 |
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:
1 |
sudo mkdir -p /etc/logrotate.d/httpd-prerotate |
Add the update script:
1 2 3 4 5 6 |
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:
1 |
sudo chmod ug+x /etc/logrotate.d/httpd-prerotate/awstats |
At this point, you can run the statistics collector from the command line:
1 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<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:
1 2 |
sudo mkdir -p /etc/apache2/htpasswd sudo htpasswd -c /etc/apache2/htpasswd/htpasswd.awstats myusername |
Reload Apache configuration:
1 |
/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:
1 |
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:
1 2 3 4 5 |
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:
1 2 |
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:
1 |
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.
Add
AllowFullYearView=3
To allow showing full year statistics in the web ui.
fantastic tutorial and followed it to the T, got an issue though and have this error
“/etc/profile.d/resize.sh: [[: not found”
any advice?
The /etc/profile.d directory should contain shell startup files. I don’t think that resize.sh is related to anything on this page. You should take a look at the file and see what is in it.
The “[[: not found” sounds like the bash expression evaluator “[[” is being called but the executing shell is not bash but something that does not have that. Most likely /bin/sh.
Thank you for a brilliant and clear tutorial. Followed and it sorted out my earlier mess and worked, mostly.
sudo su -l -c /usr/share/awstats/tools/update.sh www-data
… this only updates stats for the core domain but not the additional virtual host site
yet, this does work – manually, but not automatically…
/usr/lib/cgi-bin/awstats.pl -config=correctly.entered.site -update
i cannot find what i’m doing wrong here. Can you help?
if you will set automatical update, /usr/lib/cgi-bin/awstats.pl -config=correctly.entered.site -update use in programs cron in linux.
for anyone stuck at the line ‘su -l -c /usr/share/awstats/tools/update.sh www-data’, simply do ‘su -l www-data -s /bin/bash’