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 AWStats
wget http://prdownloads.sourceforge.net/awstats/awstats-7.0.tar.gz tar -zxf awstats-7.0.tar.gz sudo 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
#AddHandler cgi-script .cgi
AddHandler cgi-script .cgi .pl
sudo perl /var/www/awstats/tools/awstats_configure.pl
Create the dataDir for AWStats
sudo mkdir /var/lib/awstats/
Enable combined logs. Ensure the
CustomLogfor your sites use a combined access log
CustomLog "/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.
#!/bin/sh # find new sites sites=$(ls /var/log/httpd/sites/*-access_log) for site in $sites do domain=$(echo $site | sed -e "s/^\/var\/log\/httpd\/sites\///" -e "s/\-access_log$//") if [ ! -e /etc/awstats/awstats.$domain.conf ] then domainregex=$(echo $domain | sed -e "s/\./\./g") cat /etc/awstats/template.conf | sed -e "s/domain\.tld/$domainregex/g" > /etc/awstats/awstats.$domain.conf fi done awstats="/var/www/awstats/wwwroot/cgi-bin/awstats.pl" cd /etc/awstats # update all sites with configuration files for file in $(ls awstats.*.conf) do domain=$(echo $file | sed -e"s/^awstats\.//" -e "s/\.conf$//") $awstats -config=$domain -update done
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.
comments powered by Disqus