Configuring Awstats for Apache

awstats_ban_460x270

Awstats is a free log analyzer written in Perl. It lets you build nice visit report graphs for your site based on info from log files.

I’ll go over the setup using Linux Ubuntu as an example. For CentOS there’s no big difference.

First we need to install the required packages:

apt-get install awstats libapache2-mod-perl2

After this, Awstats will be installed into the /usr/share/awstats folder and the config files will live in the /etc/awstats folder. The parser script itself sits in /usr/lib/cgi-bin/

Next let’s move on to configuring the parser:

cp /etc/awstats/awstats.conf /etc/awstats/awstats.my_cool_site.com.conf

If you have more than one site on the same server, it’s better to create separate config files for them. Next let’s edit the newly created file:

/etc/awstats/awstats.my_cool_site.com.conf

Open it in your favorite editor, then find and edit the following values according to your server’s config:

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

Next we need to tell the Apache web server how to work with Awstats. First, make sure mod_perl is loaded. You can do that by running the following command:

apache2ctl -M |grep perl

or

apachectl -t -D DUMP_MODULES |grep perl

Next, in the /etc/apache2/conf.d/ (/etc/httpd/conf.d/) folder you need to create the awstats.conf file, or make sure it’s created correctly.

The following parameters matter:

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/
<Directory "/usr/lib/cgi-bin/">
	Options ExecCGI -MultiViews +SymLinksIfOwnerMatch
</Directory>

The next step is creating a crontab job for regular log file parsing. Naturally it’s good practice to run it with superuser (root) rights:

*/20 * * * * /usr/lib/cgi-bin/awstats.pl -config=my_cool_site.com -update > /dev/null

Now the log file with site access info will be parsed every 20 minutes. You can increase the interval, but then parsing will need more time and resources. Awstats has its own metadata storage where it saves info about processed logs. That means on every run the parser continues from where it left off last time.

To get the statistics via web, the link will look like this:
http://my_cool_site.com/awstats/awstats.pl?config=my_cool_site.com

I strongly recommend protecting the stats page with some kind of login form. You can read how to do that with .htaccess in this article (basic auth)