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).
Download, extract, and install AWStats1wget http://prdownloads.sourceforge.net/awstats/awstats-7.0.tar.gz2tar -zxf awstats-7.0.tar.gz3sudo mv awstats-7.0/ /var/www/awstats
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
- sudo perl /var/www/awstats/tools/awstats_configure.pl
Create the dataDir for AWStatssudo mkdir /var/lib/awstats/
Enable combined logs. Ensure the
CustomLogfor your sites use a combined access logCustomLog "/var/log/httpd/sites/domain.tld-access_log" combined
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
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/sh23# find new sites4sites=$(ls /var/log/httpd/sites/*-access_log)5for site in $sites6do7 domain=$(echo $site | sed -e "s/^\/var\/log\/httpd\/sites\///" -e "s/\-access_log$//")8 if [ ! -e /etc/awstats/awstats.$domain.conf ]9 then10 domainregex=$(echo $domain | sed -e "s/\./\./g")11 cat /etc/awstats/template.conf | sed -e "s/domain\.tld/$domainregex/g" > /etc/awstats/awstats.$domain.conf12 fi13done1415awstats="/var/www/awstats/wwwroot/cgi-bin/awstats.pl"16cd /etc/awstats1718# update all sites with configuration files19for file in $(ls awstats.*.conf)20do21 domain=$(echo $file | sed -e"s/^awstats\.//" -e "s/\.conf$//")22 $awstats -config=$domain -update23done
sudo sh /etc/awstats/daily.sh
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.