Daily statistics in AwStats

AwStats is a log file parser written in perl, with which you can keep statistics of site visits based on information from log files.

As a rule, awstats displays general information for the week, month. But what to do if you need to display information about visiting the site for a specific day.

A similar trick will be discussed further on the example of a server based on Linux Ubuntu with Apache2.

First, install AwStats:

apt-get install awstats libapache2-mod-perl2

Create website config:

cp /etc/awstats/awstats.conf /etc/awstats/awstats.**website.com**.conf

Edit the file accordingly:

LogFile="/var/log/apache2/access.log" #path to logfile;
LogFormat=1 #for full statistics;
SiteDomain="website.com” #domainname;
HostAliases="www.website.com website.com" #site aliases

That is it. Next enable AwStats in Apache:

cp /usr/share/doc/awstats/examples/apache.conf /etc/apache2/conf.d/awstats.conf  
/etc/init.d/apache2 reload

Edit cron tab:

crontab -e

Create the following cron jobs:

0 2 * * * /usr/lib/cgi-bin/awstats.pl --config=/etc/awstats/awstats.website.com.conf -DatabaseBreak=day > /dev/null
0 2 * * * /usr/lib/cgi-bin/awstats.pl --config=/etc/awstats/awstats.website.com.conf -DatabaseBreak=month > /dev/null
0 2 * * * /usr/lib/cgi-bin/awstats.pl --config=/etc/awstats/awstats.website.com.conf -DatabaseBreak=year > /dev/null

It remains to create index.cgi that will provide the ability to select a range for displaying statistics. Create an index.cgi file in /usr/lib/cgi-bin/

The contents of the file are in the spoiler.


# The awstats config file name

### Begin program ###




print "Content-type: text/html\n\n";
print "<html><body>\n";
print "<a href='".getLink($today_year,$today_month,$today_day)."'>Today</a> ";
print "<a href='".getLink($ytd_year,$ytd_month,$ytd_day)."'>Yesterday</a> ";
print "<a href='".getLink($today_year,$today_month)."'>ThisMonth</a> ";
print "<a href='".getLink($lastmonth_year,$lastmonth)."'>LastMonth</a> ";
print "<a href='".getLink($today_year)."'>ThisYear</a> ";
print "<a href='".getLink($lastyear)."'>LastYear</a> ";
print "\n<hr/>\n";

printCal($lastmonth_year, $lastmonth);
print "\n<br>\n";
printCal($today_year, $today_month);

print "\n<hr/></body></html>\n";

##### Methods ######

sub getLink
	my($year, $month, $day)=@_;
	return "awstats.pl?config=${CONFIG}&$query";

sub printCal
my($y, $m)=@_;
open(CAL, "cal $m $y |");
@days = <CAL>;

$month = $days[0];
$month=~ s/\s\s\s*//g;
if($m==$today_month && $y==$today_year)
print "<table border=1><tr><td colspan=7 $mbg><a href='".getLink($y, $m)."'>$month</a></td></tr><tr>\n";
foreach $dy (split(/ /, $days[1]))
	print "<td>$dy</td>";
print "</tr>\n";
foreach $line (@days)
	chomp $line;
	$line =~ s/^\s+//;
	$line =~ s/\s+$//;
	print "<tr>";
	foreach	$d (split(/\s+/, $line))
		if($d==$today_day && $m==$today_month && $y==$today_year)
		print "<td $bg><a href='".getLink($y, $m, $d)."'>$d</a></td>";
print "</table>\n";

Make it executable and change the owner:

chmod 755 /usr/lib/cgi-bin/index.cgi
chown www-data:www-data /usr/lib/cgi-bin/index.cgi

Edit /etc/apache2/conf.d/awstats.conf with the following:

DirectoryIndex index.cgi

I got the same note for NginX