Hosting/awstats

Setting up awstats to monitor site activity

Following the steps in this email thread was a good starting point,

I also drew inspiration from:

  • The DEBIAN.readme file in the /usr/share/doc/awstats folder.

This next link shows you how to setup a prerotate script under /etc/logrotate.d/apache2

Essentially this is as follows (I've modified it slightly)

1. Install awstats

  1. Install awstats and other packages for awstats plugins
    sudo apt-get install awstats libgeo-ipfree-perl libnet-xwhois-perl
    
  1. Copy the installed version to the location we want to run it from
    sudo cp /usr/share/doc/awstats/examples /usr/local/awstats
    sudo cd usr/local/awstats
    sudo mkdir wwwroot
    sudo mkdir wwwroot/cgi-bin
    
  1. Decompress and move the model config file into place so we can run the configure perl script
    sudo gunzip awstats.model.conf.gz
    sudo mv awstats.model.conf wwwroot/cgi-bin
    
  1. Run the configure script
    sudo ./awstats_configure.pl
    
  1. Answer the questions for the script as follows, replacing yourdomain.com with the domain you wish to monitor.
    /etc/apache2/httpd.conf
    y
    yourdomain.com
    {ENTER}
    {ENTER}
    {ENTER}
    
  1. Now make sure awstats can find everything in the correct place and modify the permissions of the access.log you wish to scrape. Use the path to the log you need.
    sudo ln -s /usr/local/awstats/css/ wwwroot/
    sudo ln -s /usr/share/awstats/* wwwroot/
    sudo ln -s /usr/lib/cgi-bin/awstats.pl wwwroot/cgi-bin/
    sudo chown -R --dereference www-data:www-data wwwroot
    sudo chown -R --dereference www-data:www-data /var/lib/awstats
    sudo chown root:www-data /path/to/apache2/access.log
    
  1. Change the Options directive in the <Directory "/usr/local/awstats/wwwroot"> directive in /etc/apache2/httpd.conf from None to FollowSymLinks so it now reads as,
    <Directory "/usr/local/awstats/wwwroot">
        Options FollowSymLinks
        AllowOverride None
        Order allow,deny
        Allow from all
    </Directory>
    
  1. Restart apache
    sudo /etc/init.d/apache2 restart
    
  1. Now browse to awstats
      http://yourdomain.com/awstats/awstats.pl
    
    You should see an error message that tells you awstats is not setup properly (or similar). That's ok. We are only making sure that the awstats.pl script runs.

2. Configure awstats

  1. Now edit your /etc/awstats/awstats.yourdomain.com.conf file:
      sudo vim /etc/awstats/awstats.yourdomain.com.conf
    
    Ensuring that the values shown are correct:
      LogFile="/path/to/apache2/access.log"
      LogType "W"
      LogFormat "1"
      SiteDomain="yourdomain.com"
      HostAliases="yourdomain.com www.yourdomain.com 127.0.0.1 localhost"
    
    Enable the GeoIPfree plugin
      LoadPlugin="geoipfree"
    

3. Configure cron and logrotate

  1. Add the following entry to /etc/cron.d/awstats
    3,33 * * * * www-data [ -x /usr/lib/cgi-bin/awstats.pl -a -f /etc/awstats/awstats.yourdomain.com.conf -a -r /path/to/access.log ] && /usr/lib/cgi-bin/awstats.pl -config=yourdomain.com -update >/dev/null
    
  1. Add the following to /etc/logrotate.d/apache2
    /path/to/*.log {
            weekly
            missingok
            rotate 52
            compress
            delaycompress
            notifempty
            create 644 root adm
            sharedscripts
            prerotate
            /usr/lib/cgi-bin/awstats.pl -config=yourdomain.com -update
            endscript
            postrotate
                    if [ -f /var/run/apache2.pid ]; then
                            /etc/init.d/apache2 restart > /dev/null
                    fi
            endscript
    }
    
    The create 644 root adm allows read access to the logs.

4. Update the stats for the website

  1. Run awstats.pl to update the stats for your domain
    sudo /usr/lib/cgi-bin/awstats.pl -config=awstats.yourdomain.com.conf -update
    
    You should see something like this:
    Update for config "/etc/awstats/awstats.yourdomain.com.conf"
    With data in log file "/path/to/access.log"...
    Phase 1 : First bypass old records, searching new record...
    Searching new records from beginning of log file...
    Phase 2 : Now process new records (Flush history on disk after 20000 hosts)...
    Jumped lines in file: 0
    Parsed lines in file: 60
    Found 0 dropped records,
    Found 0 corrupted records,
    Found 0 old records,
    Found 60 new qualified records.
    

5. Browse to http://yourdomain.com/awstats/awstats.pl to view the stats

Your done.

Note

Depending on your apache setup you may need to cut the contents of 'httpd.conf' and place the entries into (say) the appropriate VirtualHost directive. Perhaps you may need to place awstats into its own VirtualHost directive as, say, you want to access this sort of information under a separate sub-domain.