Purpose: So I decided I need some kind of web statistics program for my Debian Lenny system which runs an Apache2 web server and a lot of other web-based application like MediaWiki, Mantis, etc. There are a lot of different programs that will let you view your web server statistics like Webalizer, AWstats, Visitors, etc. I was familiar with AWstats (on Windows Server) before so I decided that I will give it a shot on Linux also. After doing some researching it seems that there is still some confusion about configuring AWstats in Debian. So here is my attempt to put together all the data.

So let’s get started…

Assumption

  • I am using my hostname as “mydomain.org”. You can substitute it whatever is your hostname. You can check your hostname like this:

# cat /etc/hostname

Output

mydomain.org

  • Also there is some text in “blue” color which basically means that the code you need to pay attention to because mostly likely you will have to edit it.

Step 1: Install AWstats

# apt-get update
# apt-get awstats

Step 2: Configure AWstats for Apache

# nano /etc/apache2/awstats.conf

and add the following lines:

Alias /awstatsclasses "/usr/share/awstats/lib/"
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
Alias /awstats-icon/ "/usr/share/awstats/icon/"
<Directory /usr/share/awstats/icon>
Options None
AllowOverride None
Order allow,deny
Allow from all
</Directory>

Save and quit.

Step 3: Tell Apache to read the AWstats config file

Now we tell Apache to read the above configuration file. So we simply edit the file:

# nano /etc/apache2/apache2.conf

and the following lines at the end of it:

Include /etc/apache2/awstats.conf

Save and quit.

Step 4: Configure AWstats for your machine

Now we need to configure some parameters in the AWstats main configuration file. First create a copy of the configuration file:

# cp /etc/awstats/awstats.conf /etc/awstats/awstats.mydomain.org.conf

You can substitute your hostname in the above command instead of mydomain.org

Set the following variables are in the file that you just copied:

# nano /etc/awstats/awstats.mydomain.org.conf
LogFile="/var/log/apache2/access.log"
LogFormat=4
SiteDomain="mydomain.org"

Save and quit.

Step 5: Generate your first statistics


Now we are ready to generate first statistics for our system by the following command:

# su root
#  /usr/bin/perl /usr/lib/cgi-bin/awstats.pl -update -config=mydomain.org

Now fire up a web browser and go the following URL:

# http://mydomain.org/awstats/awstats.pl

Congratulations!!! You have just generated your first set of statistics.

Step 6: Set up cron job

Now it is not very convenient to manually run the above command manually every time you need to generate your statistics. So we will set-up a cron job for that. The good news is that AWstats package already does that for you. All you need to do is to give proper permission to your Apache2 log files so that the user “www-data” can access it. Why www-data? Because by default that is the user which is responsible for generating statistics.

# nano /etc/cron.d/awstats

and make sure it reads like this:

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/apache2/access.log ] && /usr/lib/cgi-bin/awstats.pl -config=mydomain.org -update >/dev/null

Save and quit.

Now modify apache2 log file permissions:

# chmod 744 -R /var/log/apache2/*.*
# chmod 751 /var/log/apache2

Now after every 10 minutes the cron job will run automatically and you can see your statistics as shown above. If you are having trouble getting the cron job to run, please this post.

Step 7: Give proper permission for logrotate

Finally we need to make sure that when logrotate is executed, www-data has proper permission to run the statistics. So edit the following file:

# nano /etc/logrotate/apache2

and make sure it reads like this:

/var/log/apache2/*.log {
weekly
missingok
rotate 52
compress
delaycompress
notifempty
create 644 root adm
sharedscripts
prerotate
/usr/lib/cgi-bin/awstats.pl -config=mydomain.org -update
endscript

postrotate
if [ -f "`. /etc/apache2/envvars ; echo ${APACHE_PID_FILE:-/var/run/apache2.pid}`" ]; then
/etc/init.d/apache2 reload > /dev/null
fi
endscript
}

Save and quit.

That’s it. You have successfully configured your AWstats for your Debian (or Ubuntu) web server successfully.

Happy AWstat’ing!

Be Sociable, Share!