AWStats including GeoIP-Plugin on Ubuntu
AWStats is an open source Web analytics reporting tool, suitable for analyzing data from Internet services such as web, streaming media, mail and FTP servers. AWStats parses and analyzes server log files, producing HTML reports.
Installing with Synaptic
sudo apt-get install awstats
Configuring
sudo cp /etc/awstats/awstats.conf /etc/awstats/awstats.yourdomain.com.conf
Edit the file
sudo vim /etc/awstats/awstats.yourdomain.com.conf
Do the following changes
LogFile="/var/log/apache2/access.log"
SiteDomain="yourdomain.com"
HostAliases="localhost 127.0.0.1 yourdomain.com"
DNSLookup=1
LogFormat=1
AllowFullYearView=3
Generating initial stats for Awstat based on existing access.log file
/usr/lib/cgi-bin/awstats.pl -config=yourdomain.com -update
Apache2 configuration
Create a new virtual host for our awstats access
vim /etc/apache2/sites-available/awstats
<VirtualHost *:80>
ServerName awstats.yourdomain.com
ServerAdmin [email protected]
DocumentRoot /var/www
# Custom Log files to awstats
ErrorLog ${APACHE_LOG_DIR}/error_awstats.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access_awstats.log combined
<Directory "/usr/lib/cgi-bin/">
<Files "awstats.pl">
Options +ExecCGI
</Files>
</Directory>
Alias /awstats-icon/ "/usr/share/awstats/icon/"
<Directory /usr/share/awstats/icon>
Options None
AllowOverride None
Order allow,deny
Allow from all
</Directory>
RewriteEngine on
RewriteRule /awstats-icon - [L]
RewriteCond %{QUERY_STRING} !^(.*&)?framename=index(&.*)?$
RewriteCond %{QUERY_STRING} ^(.*&)?framename=.*(&.*)?$
RewriteRule /(.*) /usr/lib/cgi-bin/awstats.pl [L]
RewriteCond %{QUERY_STRING} ^(.*&)?config=([^&]*)(&(.*))?$
RewriteRule /.* /%2?%1%4 [R,L]
RewriteRule /awstats.pl$ / [R,L]
RewriteRule /([^/]*)(/.*)? /usr/lib/cgi-bin/awstats.pl?config=$1 [QSA,L]
<LocationMatch /(?!awstats.pl|robots.txt)$>
Require user admin
</LocationMatch>
<Location /awstats-icon>
SetHandler None
Allow from all
</Location>
</VirtualHost>
Enabling awstats and checking config files
a2ensite awstats
apache2ctl configtest
If we get “Syntax OK” you can now reload the Apache service
/etc/init.d/apache2 reload
Now you can check stats for yourdomain.com
http://awstats.yourdomain.com/
and
http://awstats.yourdomain.com/yourdomain.com
Don’t forget to secure them otherwise they are public
I use basic authentication like this
<VirtualHost *:80>
ServerName yourdomain.com
ServerAdmin [email protected]
DocumentRoot /var/www
ErrorLog ${APACHE_LOG_DIR}/error_awstats.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access_awstats.log combined
<Directory "/usr/lib/cgi-bin/">
<Files "awstats.pl">
Options +ExecCGI
</Files>
</Directory>
Alias /awstats-icon/ "/usr/share/awstats/icon/"
<Directory /usr/share/awstats/icon>
Options None
AllowOverride None
Order allow,deny
Allow from all
</Directory>
RewriteEngine on
RewriteRule /awstats-icon - [L]
RewriteCond %{QUERY_STRING} !^(.*&)?framename=index(&.*)?$
RewriteCond %{QUERY_STRING} ^(.*&)?framename=.*(&.*)?$
RewriteRule /(.*) /usr/lib/cgi-bin/awstats.pl [L]
RewriteCond %{QUERY_STRING} ^(.*&)?config=([^&]*)(&(.*))?$
RewriteRule /.* /%2?%1%4 [R,L]
RewriteRule /awstats.pl$ / [R,L]
RewriteRule /([^/]*)(/.*)? /usr/lib/cgi-bin/awstats.pl?config=$1 [QSA,L]
<Location />
SetHandler cgi-script
Satisfy any
Order deny,allow
Deny from all
AuthType Basic
AuthName "AWStats Access"
AuthUserFile /usr/share/awstats/awstats.pwd
require valid-user
</Location>
<LocationMatch /(?!awstats.pl|robots.txt)$>
Require user admin
</LocationMatch>
<Location /awstats-icon>
SetHandler None
Allow from all
</Location>
</VirtualHost>
To create your /etc/awstats/awstats.pwd do the following
sudo htpasswd -c /etc/awstats/awstats.pwd admin
Then it will prompt you to type your password twice
Auto update stats
awstats installation creates a cron.d job in /etc/cron.d/awstats
vim /etc/cron.d/awstats
Should look like this:
MAILTO=root
*/10 * * * * www-data [ -x /usr/share/awstats/tools/update.sh ] && /usr/share/awstats/tools/update.sh
# Generate static reports:
10 03 * * * www-data [ -x /usr/share/awstats/tools/buildstatic.sh ] && /usr/share/awstats/tools/buildstatic.sh
If not insert the following
*/10 * * * * www-data /usr/lib/cgi-bin/awstats.pl -config=yourdomain.com -update > /dev/null
If you get this error message:
Error: Couldn’t open server log file “/var/log/apache2/access.log” : Permission denied
First way to do it is to give www-data user to access log file:
sudo chmod 644 -R /var/log/apache2/*
chmod 644 /var/log/apache2
and edit /etc/logrotate.d/apache2 file
sudo vim /etc/logrotate.d/apache2
Replace
create 640 root adm
with
create 644 root adm
Second way is to replace www-data by root in /etc/cron.d/awstats file like this:
*/10 * * * * root [ -x /usr/share/awstats/tools/update.sh ] && /usr/share/awstats/tools/update.sh
# Generate static reports:
10 03 * * * root [ -x /usr/share/awstats/tools/buildstatic.sh ] && /usr/share/awstats/tools/buildstatic.sh
Installing modules like GeoIP
Prerequisite
apt-get install build-essential zlib1g-dev apg libgeo-ipfree-perl libnet-ip-perl libnet-dns-perl liburi-perl apache2-utils
cd /usr/src/
wget http://zlib.net/zlib-1.2.7.tar.gz
tar xvzf zlib-1.2.7.tar.gz
cd zlib-1.2.7
./configure --prefix=/usr/local/zlib && make && make install
Installing GeoIP
wget http://maxmind.com/download/geoip/api/c/GeoIP.tar.gz
tar xzvf GeoIP.tar.gz
cd GeoIP-1.4.8
./configure && make && make install
Finishing the installation
cpan
install YAML
install Geo::IP
install Geo::IPfree
install Geo::IP::PurePerl
install URI::Escape
install Net::IP
install Net::DNS
install Net::XWhois
install Time::HiRes
install Time::Local
quit
Check in the AWSTAT configuation file if the LoadPlugin is enabled
vim /etc/awstats/awstats.yourdomain.com.conf
LoadPlugin="geoip GEOIP_STANDARD /usr/local/share/GeoIP/GeoIP.dat"
Now reload Apache2
/etc/init.d/apache2 reload
Installing Geo Lite City
cd /usr/src/
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
gzip -d GeoLiteCity.dat.gz
cp GeoLiteCity.dat /usr/local/share/GeoIP/GeoLiteCity.dat
Check in the AWSTAT configuation file if the LoadPlugin is enabled
vim /etc/awstats/awstats.yourdomain.com
LoadPlugin="geoip_city_maxmind GEOIP_STANDARD /usr/local/share/GeoIP/GeoLiteCity.dat"
Some examples




