AWStats

Monitor site statistics with AWStats on Apache

published on
https://www.awstats.org/

If you want a simple way of monitoring visitors to your website but you don't need, or want, a full - blown monitoring system then AWStats might just work for you.

In order to get it set up and working on your Apache server follow the instructions below.

Install the necessary software

Run sudo apt install awstats libgeo-ip-perl libgeo-ipfree-perl to get AWStats installed and ready to set up.

Configure AWStats

Run the following set of commands in order to set up and configure AWStats to work effectively.

sudo cp /etc/awstats/awstats.conf /etc/awstats/awstats.[domain].conf
sudo nano /etc/awstats/awstats.[domain].conf
  LogFormat=1
  SiteDomain="[domain]"
  HostAliases="localhost 127.0.0.1 [domain]"
  AllowFullYearView=3
  AllowAccessFromWebToFollowingIPAddresses="127.0.0.1 192.168.1.0-192.168.1.255"
  LoadPlugin="tooltips"
  LoadPlugin="graphgooglechartapi"
  LoadPlugin="geoipfree"
sudo nano /etc/awstats/awstats.conf.local
  SiteDomain="[domain]"
  HostAliases="localhost 127.0.0.1 [domain]"
sudo mv /etc/cron.d/awstats /root
sudo rm /var/lib/awstats/*
sudo chgrp www-data /var/log/apache2 /var/log/apache2/*.log /var/log/apache2/access.log
sudo chmod 755 /var/log/apache2
sudo chmod 644 /var/log/apache2/*

To finish setting up AWStats edit the necessary logrotate file by running sudo nano /etc/logrotate.d/apache2 then look for create 640 and change it to create 640 root www-data.

Finally run sudo mv /root/awstats  /etc/cron.d to set up cron to run daily.

Configure Apache

Only a few commands are needed to get Apache set up now. Edit the virtual host file by running sudo nano /etc/apache2/sites-available/[domain].conf and adding the following lines just above the </VirtualHost> tag.

Alias /awstatsclasses "/usr/share/awstats/lib/"
Alias /awstats-icon/ "/usr/share/awstats/icon/"
Alias /awstatscss "/usr/share/doc/awstats/examples/css"
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
ScriptAlias /awstats/ /usr/lib/cgi-bin/
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch

Finally run sudo a2enmod cgi to enable the cgi module.

Protect AWStats

At the moment everyone could access your stats provided they know the path to do so. We will now protect AWStats with a password so that only you can get access.

Run sudo htpasswd -c /etc/apache2/.htpasswd [username] to create a username / password pair for accessing your AWStats page. [username] is obviously the username you wish to use to login.

Now add the following lines to your virtual host file you created above:

<Directory "/usr/lib/cgi-bin/">
  AuthUserFile /etc/apache2/.htpasswd
  AuthName "Please Enter Your Password"
  AuthType Basic
  Require valid-user
</Directory>

Finally restart Apache with sudo systemctl restart apache2.

When you now try viewing the stats for your server you will first need to enter the username and password you used above.

Viewing your stats

In order to view the stats for your site visit [domain]/cgi-bin/awstats.pl in your browser.

Conclusion

Whilst these stats are fairly basic and may not be completely accurate they will hopefully serve the purpose of giving you some basic information about your server and how popular your site or sites are.

If you wish to get more accurate statistics try using Zabbix for server monitoring or Matomo for site statistics.