Introduction
The Arch Linux Wiki contains a nice howto on the installation of awstats: https://wiki.archlinux.org/title/AWStats.
Unfortunately, the given configuration options didn't quite fulfill my requirements. In my setup, I wanted to collect web statistics of several domains, but make them available only to a specially protected subdomain. The following steps were needed to accomplish these goals.
Basic installation
Install awstats and mod_perl (together with the dependencies perl-json-xs and perl-try-tiny) as described in the arch wiki.
Configuration of awstats
Configuration of access to dynamic statistics (awstats.pl)
Edit the /etc/httpd/conf/extra/httpd-awstats.conf configuration file to configure access to the awstats.pl script:
# Content of this file, with correct values, can be automatically added to
# your Apachver by using the AWStats configure.pl tool.
#
# If using Windows and Perl ActiveStat, this is to enable Perl script as CGI.
#ScriptInterpreterSource registry
# Load the perl module which is required for awstats
LoadModule perl_module modules/mod_perl.so
#
# Directives to add to your Apache conf file to allow use of AWStats as a CGI.
# Note that path "/usr/local/awstats/" must reflect your AWStats install path.
#
Alias /awstatsclasses "/usr/share/webapps/awstats/classes/"
Alias /awstatscss "/usr/share/webapps/awstats/css/"
Alias /awstatsicons "/usr/share/webapps/awstats/icon/"
ScriptAlias /awstats/ "/usr/share/webapps/awstats/cgi-bin/"
#
# This is to permit URL access to scripts/files in AWStats directory.
#
<Directory "/usr/share/webapps/awstats">
AddHandler perl-script .pl
AddHandler perl-script .cgi
PerlResponseHandler ModPerl::Registry
PerlOptions +ParseHeaders
Options ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
In order to be able to run the awstats.pl perl script, the following settings turned out to be required:
- Options ExecCGI
- AllowOverride All
Protection of awstats statistics
Last, since access to the directory /usr/share/webapps/awstats is restricted (as is access by the web server to anything outside the domain's root, a .htaccess file must be placed in this directory:
htpasswd -c /usr/share/webapps/awstats/.htpasswd your-user-name
Enabling access to awstats statistics per domain
While we want to collect web statistics for all hosted domains, access to the statistics analysis should be limited to the subdomain protected.mydomain.com.
In order to achieve this, the awstats configuration was included inside a single <VirtualHost> directive of the configuration file /etc/httpd/conf/vhosts/httpd-protected.mydomain.com.conf as shown below:
<VirtualHost *:443>
…
# Include the necessary configuration to run 'awstats'
Include conf/extra/httpd-awstats.conf
</VirtualHost>
Configuring web statistics by domain
Now that awstats is installed, it must be configured. Since we use individual log files for each domain, a specific awstats configuration needs to be written for each of the log files to be analyzed.
Copy over the sample configuration file for awstats:
cp /etc/awstats/awstats.model.conf /etc/awstats/awstats.mydomain.com.conf
Edit the configuration file /etc/awstats/awstats.mydomain.com.conf and make the following changes to configure awstats for this domain:
LogFile="/var/log/httpd/mydomain.com-access_log"
LogType=W
LogFormat = "%host %other %logname %time1 %methodurl %code %bytesd %refererquot %uaquot"
SiteDomain="mydomain.com"
HostAliases="localhost 127.0.0.1 REGEX[.*\.mydomain\.com$]"
DNSLookup=1
DirData="/srv/http/protected.mydomain.com/logs/awstats"
DirCgi="/awstats"
DirIcons="/awstatsicons"
AllowToUpdateStatsFromBrowser=0
SkipHosts="127.0.0.1 REGEX[^192\.168\.] REGEX[^10\.]"
UseHTTPSLinkForUrl="/"
Create the data directory where the statistics will be stored:
mkdir /srv/http/protected.mydomain.com/logs
mkdir /srv/http/protected.mydomain.com/logs/awstats
chown -R http:http /srv/http/protected.mydomain.com/logs/awstats
chmod u+x /srv/http/protected.mydomain.com/logs/awstats
Create the statistics manually (only for the first time):
/usr/share/awstats/tools/awstats_buildstaticpages.pl config=mydomain.com -update -awstatsprog=/usr/share/webapps/awstats/cgi-bin/awstats.pl -dir=/srv/http/protected.mydomain.com/logs/awstats
Restart the web server to activate the changes
systemctl restart httpd
The statistics can now be accessed through the following URL:
http://protected.mydomain.com/awstats.pl?config=mydomain.com
The interactive statistics can now be accessed under the following URL:
https://protected.mydomain.com/awstats/awstats.pl?config=mydomain.com
The static statistics can be accessed under the following URL:
https://protected.mydomain.com/logs/awstats/awstats.mydomain.com.html
Manually creating the static statistics pages (first time only)
In order to update the statistics manually, add the following to the cron job /etc/cron.daily/run_webstatistics:
# !/bin/bash
# Renew web server statistics
LOGFILE="/var/log/httpd/renew_webstatistics.log"
# Start the awstats logfile analyzer
echo $(date +'%b %e %H:%M:%S') [run_webstatistics] : starting collection of \'awstats\' statistics >> $LOGFILE
# Update the awstats statistics for all configured virtual hosts
# Loop through all awstats configuration files, strip off the head and tail to get the domain and process everything, except 'model'
for configfile in /etc/awstats/awstats*.conf; do
domain=${configfile#*awstats.}
domain=${domain%*.conf}
if [ $domain != "model" ]
then
/usr/share/awstats/tools/awstats_updateall.pl now -awstatsprog=/usr/share/webapps/awstats/cgi-bin/awstats.pl -config=$domain –update >> /dev/null
fi
done
echo $(date +'%b %e %H:%M:%S') [run_webstatistics] : collection of \'awstats\' statistics done >> $LOGFILE
Don't forget to make the file executable:
chmod ug+x /etc/cron.daily/run_webstatistics
For each domain for which statistics should be collected, a /etc/awstats/awstats.otherdomain.com.conf configuration file needs to be created.
Geo-IP Support for awstats
The awstats statistics rely on DNS lookups to determine where a request came from. The geographical analysis based on this data is rather faulty as most requests come from domains not associated with a particular country (e.g. .com, .org, .net).
A solution to this dilemma is offered by using geo-ip, a service which uses lookup tables for better geographical analysis. A slightly limited version of this service is available for free.
Additionally install the perl-geoip perl package and the geoip-database-extra package:
pacman -S perl-geoip geoip-database-extra
To configure awstats to use Geo-IP, edit the awstats configuration files (in the /etc/awstats directory) and add the following lines:
# Add GeoIP support
LoadPlugin="geoip_city_maxmind GEOIP_STANDARD /usr/share/GeoIP/GeoIPCity.dat"
LoadPlugin="geoip_asn_maxmind GEOIP_STANDARD /usr/share/GeoIP/GeoIPASNum.dat+http://enc.com.au/itools/autnum.php?asn="
Next time, the awstats statistics are updated, the geoip information will be included.