How to Configure AWStats for Windows and IIS

January 5th 2015 AWStats IIS Web Analytics

AWStats is one of the most popular tools for generating web site statistics from server logs, and it's also the one I have selected as a replacement for Google Analytics. In spite of its extensive documentation, I've hit a couple of stumbling blocks while setting it up in my environment. As is often the case, I'm writing this blog post for future reference.

Since the application is written in Perl, it makes sense to install it first. There are a couple of distributions available for Windows. I decided to use ActiveState Perl. It's available as a standard MSI Installer package; which you only need to run with default settings. If perl.exe is available from command line after the installation, you can be sure it completed successfully.

The next step is downloading the latest version of AWStats. It's available in a couple of different formats, zip archive probably being the most handy of them in Windows. You only need to unpack it to a location, where IIS has access it. I've put it inside my C:\WWW folder. Now its time to go into AWStats' wwwroot\cgi-bin subfolder, make a copy of awstats.model.conf named awstats.damirscorner.conf (where damirscorner is the name of the site I'm generating statistics for), and start following AWStats' instructions on how to change it for your needs. I only had to change 2 settings:

  • Set LogFile to C:\WWW\_Logs\W3SVC4\u_ex%YY-4%MM-4%DD-4.log; this matches the location and the name pattern for the log files, created by IIS (e.g. u_ex150105.log for today). The -4 part for years, months and days makes AWStats look for a file matching the date from 4 hours ago. I chose this offset because I'm processing the logs for the previous day at 3 in the morning.
  • Set DirData to C:\WWW\awstats-7.3\wwwroot\stats\damirscorner; that's the location where I'm storing my processed statistics data. The target folder must be created manually.

AWStats can parse logs in almost any format, but its best to just select W3C log file format in IIS and include all the required fields, as shown in the following screenshot:

IIS Log Settings

If you have old log files you want to parse, now is the right time to do it. With the help of this StackOverflow answer, I've come up with the following steps:

  • Copy old log files into a separate folder (e.g. C:\OldLogs). Skip the log file for today as it will get parsed the next morning.
  • Open command prompt and go into tools subfolder of your AWStats installation.
  • Merge all old files into a single one by calling logresolvemerge.pl: perl logresolvemerge.pl C:\OldLogs\*.log > C:\OldLogs\merged.log
  • The resulting file will have all the comment lines removed, including the one specifying the order of columns in the log. You'll need to insert it at the top of the created file yourself or AWStats will fail to parse it. You can copy it from one of the original log files which you have just merged. It should be near the top of the file and starts with #Fields:.
  • Now you can process the generated log file with AWStats by overriding the log file path from its settings. Change your command prompt current dir to wwwroot\cgi-bin subfolder and type: perl awstats.pl -config=damirscorner -LogFile=C:\OldLogs\merged.log. Replace damirscorner with the site name which you have used when naming your *.conf file.

The statistics are now generated. You only need to configure a new web site or web application in IIS which will serve the newly generated data. Just point it to the wwwroot subfolder of your AWStats installation. Because IIS doesn't know anything about Perl, you'll need to open the Handler Mappings and Add Script Map... as shown in the following screenshot (make sure the path to PerlEx30.dll is correct):

Add Script Map...

You might also want to add a index.html file inside the wwwroot folder which will redirect you to the correct URL when you navigate to the web application root from your browser:

<!DOCTYPE HTML>
<html lang="en-US">
  <head>
    <meta charset="UTF-8">
    <meta http-equiv="refresh" content="1;url=cgi-bin/awstats.pl?config=damirscorner">
    <script type="text/javascript">
      window.location.href = "cgi-bin/awstats.pl?config=damirscorner"
    </script>
    <title>Page Redirection</title>
  </head>
  <body>
    If you are not redirected automatically, 
    follow <a href='cgi-bin/awstats.pl?config=damirscorner'>this link</a>.
  </body>
</html>

Again, replace damirscorner in all 3 occurrences with your own site name.

You still need to setup a schedule for updating the statistics with new data. AWStats can only parse each log file once, so you'll need to match the schedule with your IIS settings. As you can see from the first screenshot, I have configured my Log File Rollover to happen Daily, therefore I'll need to process the log files daily as well, once they won't change any more. I used Windows Task Scheduler to create a job which runs at 3 in the morning every day and runs Perl (C:\Perl\bin\perl.exe, to be exact) with the following arguments: C:\WWW\awstats-7.3\wwwroot\cgi-bin\awstats.pl -config=damirscorner -update.

That's it. AWStats has been working flawlessly since I've set it up and I can now check fresh statistics for my blog every morning.

Get notified when a new blog post is published (usually every Friday):

If you're looking for online one-on-one mentorship on a related topic, you can find me on Codementor.
If you need a team of experienced software engineers to help you with a project, contact us at Razum.
Copyright
Creative Commons License