How To: Setting up awstats with apache 2 on debian-ubuntu

Awstats is a great web log analyser. It gives really usefull statistics about traffic on your web sites.

Installing awstats in debian/ubuntu still needs so tweaking to produce neat statistics.

In this articles, I'm going to show you what needs to be done.

What we are going to do here, is to configure awstats such as you will be able to access your stats using a url like http://www.mysite.org/awstats/awstats.pl. There is basically 5 steps to do:

  1. Install Awstats
  2. Configure Apache
  3. Configure Awstats
  4. Generate the first stats
  5. Automate it using cron

This article comes in 2 pages. Point 1 and 2 are viewed on this page. Points 3 to 5 in page 2.
1. Installing Awstats:
In the first place, you need to install awstats:

tester@laptop:~$sudo apt-get install awstats

OK, awstats is installed, let's configure apache

2. Configuring Apache2:

You need to make awstats setting global to all web site. To do so, we are going to create a file named /etc/apache2/awstats.conf where we are going to stuff all apache settings for awstats, so create and edit this file:

tester@laptop:~$sudo vi /etc/apache2/awstats.conf

Alias /awstatsclasses "/usr/share/awstats/lib/"
Alias /awstats-icon/ "/usr/share/awstats/icon/"
Alias /awstatscss "/usr/share/doc/awstats/examples/css"
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
ScriptAlias /awstats/ /usr/lib/cgi-bin/
Options ExecCGI -MultiViews +SymLinksIfOwnerMatch

What we are saying here is that awstatsclasses will be found in /usr/share/awstats/lib/, awstats icon in /usr/share/awstats/icon/, awstats css in /usr/share/doc/awstats/examples/css and that the cgi-bin repertory on a web site is located in /usr/lib/cgi-bin/, this is the place where is located awstats.pl. As we also want to be able to access the stats using an URL like http://www.mysite.org/awstats/awstats.pl, we add the ScriptAlias instruction making the repertory /awstats on a web server to point to /usr/lib/cgi-bin/.

Now that we define all the basic setting that will let us access the stats directly over http, we need to inform apache2 about this. Edit /etc/apache2/apache2.conf :

tester@laptop:~$sudo vi /etc/apache2/apache2.conf

and add the following line at the end of the file:

Include /etc/apache2/awstats.conf

Now, reload apache2:

tester@laptop:~$sudo /etc/init.d/apache2 reload

Ok, now you can give a first shot to http://www.mysite.org/awstats/awstats.pl but you will get the following error:
Error: SiteDomain parameter not defined in your config/domain file. You must edit it for using this version of AWStats.

That basically means that we need to configure awstats.


How To: Setting up awstats with apache 2 on debian-ubuntu -- page 2

3. Configuring Awstats:

Awstats configuration file are located in /etc/awstats. By default, when calling the URL http://www.mysite.org/awstats/awstats.pl, awstats is looking for the configuration file name after your domain name, namely here /etc/awstats/awstats.www.mysite.org.conf. You can override this by giving awstats an argument named config. For instance http://www.mysite.org/awstats/awstats.pl?config=foobar will indicate awstats to fetch the configuration from /etc/awstats/awstats.foobar.conf.

Let's get back to it. On install, awstats provides a default configuration file named /etc/awstats/awstats.conf. Copy this file to /etc/awstats/awstats.www.mysite.org.conf:

~$sudo cp /etc/awstats/awstats.conf /etc/awstats/awstats.www.mysite.org.conf

and edit the file:

~$sudo vi /etc/awstats/awstats.www.mysite.org.conf

and check for the following lines and edit those for your needs:

LogFile="/var/log/apache2/access.log"
SiteDomain="mysite.org"

This is all setted up, now you need to generate the first stats.

4. Generating the First Stats:

in order to generate the first stats, you need to call the script as root using the following command line:

~:$sudo -u www-data /usr/bin/perl /usr/lib/cgi-bin/awstats.pl -update -config=www.mysite.org

Once this is all processed, check out your stats from http://www.mysite.org/awstats/awstats.pl :D.

5. Automatising the stats generation using Cron:

If we check the file installed by awstats and search for the word cron using the following command line:

~:$dpkg -L awstats | grep cron
/etc/cron.d
/etc/cron.d/awstats

we can see that awstats already installed a cron job which contain:

0,10,20,30,40,50 * * * * www-data [ -x /usr/lib/cgi-bin/awstats.pl -a -f /etc/awstats/awstats.conf -a -r /var/log/apache/access.log ] && /usr/lib/cgi-bin/awstats.pl -config=awstats -update >/dev/null

which basically check every 10 minutes that file /usr/lib/cgi-bin/awstats.pl is executable AND file /etc/awstats/awstats.conf exists and is a regular file AND file /var/log/apache/access.log is readable, if this is TRUE, it executes awstats.pl with the config awstats.

As the file /etc/awstats/awstats.awstats.conf does not exist, it will take /etc/awstats/awstats.conf by default.

What we can do in the first, is to use that file for our needs. Let say I want update my stats every day at 2am, I will change the line to:

0 2 * * * www-data [ -x /usr/lib/cgi-bin/awstats.pl -a -f /etc/awstats/awstats.conf -a -r /var/log/apache/access.log ] && /usr/lib/cgi-bin/awstats.pl -config=mysite.com -update >/dev/null

Hope this helped ;)

Recommanded Book:

Apache: The Definitive Guide (3rd Edition)