Installing and Configuring AWStats on Amazon EC2

Main Thread 3 min read

As admitted before, I am no sysadmin. However, I've taken an interest in Amazon EC2. As such, I'm learning as I go. This time, it's how to install AWStats. What's a server without stats, right?

Right now, for learning purposes, I have an EC2 micro instance running Amazon Linux 64-bit. That likely doesn't matter for the install. There are a few conventions I follow:

  • Web data is in /var/www/
  • Website logs are in /var/logs/httpd/sites/

With that said, the following steps install and configure AWStats (version 7.0).

  1. Download, extract, and install AWStats

    1wget http://prdownloads.sourceforge.net/awstats/awstats-7.0.tar.gz
    2tar -zxf awstats-7.0.tar.gz
    3sudo mv awstats-7.0/ /var/www/awstats
  2. Enable CGI including the .pl extension under Apache. There are alternatives if you don't want to enable CGI globally.

    sudo vi /etc/httpd/conf/httpd.conf

    Change:

    #AddHandler cgi-script .cgi

    To:

    AddHandler cgi-script .cgi .pl
  3. Run the AWStats Tool and follow the instructions. I followed the defaults and named my server ec2test. From what I read, the name doesn't really matter.

    sudo perl /var/www/awstats/tools/awstats_configure.pl
  4. Create the dataDir for AWStats

    sudo mkdir /var/lib/awstats/
  5. Enable combined logs. Ensure the CustomLog for your sites use a combined access log

    CustomLog "/var/log/httpd/sites/domain.tld-access_log" combined
  6. Although the AWStats tool does, I restarted Apache again as I made changes in the last step.

    sudo service httpd graceful

View AWStats at http://yourec2domain.com/awstats/awstats.pl?config=ec2test

Troubleshooting

When I first visited my AWStats, I got a 404. After some digging around, I found the /etc/httpd/conf.d/awstats.conf was missing the following critical line:

ScriptAlias /awstats/ "/var/www/awstats/wwwroot/cgi-bin/"

AWStats didn't 404. But there was no data. AWStats has an updater you need to run for each of your sites.

sudo /var/www/awstats/wwwroot/cgi-bin/awstats.pl -update -config=ec2test

Automating AWStats Updates

If you only have one site putting the above in cron is no big deal. But if you have multiple sites with multiple configurations, that's another story. There's a maintenance overhead for making the conf file and then adding the command to cron.

I found a shell script that does all this for you. Essentially, it examines your site logs and ensure that an AWStats configuration exists. Under the assumption if a site has an access log you want AWStats for it. It then runs the AWStats updater for that configuration.

You need to create a conf file to be used as the template when creating new site configuration. I simply copied my main configuration file:

sudo cp awstats.ec2test.conf template.conf

I changed a few lines in the template:

  • All instances of ec2test to domain.tld (domain.tld is a placeholder for the script)
  • Changed the name and location of the access log file (use the placeholder)

Here's the script. You can add it solely to cron – one script to rule them all.

1#!/bin/sh
2 
3# find new sites
4sites=$(ls /var/log/httpd/sites/*-access_log)
5for site in $sites
6do
7 domain=$(echo $site | sed -e "s/^\/var\/log\/httpd\/sites\///" -e "s/\-access_log$//")
8 if [ ! -e /etc/awstats/awstats.$domain.conf ]
9 then
10 domainregex=$(echo $domain | sed -e "s/\./\./g")
11 cat /etc/awstats/template.conf | sed -e "s/domain\.tld/$domainregex/g" > /etc/awstats/awstats.$domain.conf
12 fi
13done
14 
15awstats="/var/www/awstats/wwwroot/cgi-bin/awstats.pl"
16cd /etc/awstats
17 
18# update all sites with configuration files
19for file in $(ls awstats.*.conf)
20do
21 domain=$(echo $file | sed -e"s/^awstats\.//" -e "s/\.conf$//")
22 $awstats -config=$domain -update
23done

Run it:

sudo sh /etc/awstats/daily.sh

Closing

There were several good references for installing and configuring AWStats. However, nothing seemed comprehensive or specific for Amazon EC2. So if nothing else, I figured I'd post and fill the keyword gap to hopefully help those like me starting our with server admin and Amazon EC2.

Find this interesting? Let's continue the conversation on Twitter.