Install AWStats Web analytics on Apache2 Debian 8

AWStats is an open source Web analytics reporting tool that generates advanced web, streaming, ftp or mail server statistics, graphically. This log analyzer works as a CGI or from command line and shows you all possible information your log contains, in few graphical web pages. It uses a partial information file to be able to process large log files, often and quickly.

Update and upgrade Debian

#apt-get update && apt-get upgrade -y

Install awstats

#apt-get install awstats

enable the CGI module in Apache

#a2enmod cgi

Incase want to Disable CGI

#a2dismod cgi

Restart Apache to reflect the changes.

#/etc/init.d/apache2 restart

Configure AWStats

Create a configuration file by duplicating AWStats default conf. For example file for “vpshelpdesk.com” or ignore use default awstats.conf

#cd /etc/awstats
#cp /etc/awstats/awstats.conf /etc/awstats/vpshelpdesk.conf

Next, edit configuration file “vpshelpdesk.conf” or “awstats.conf”

#vim awstats.conf
# Examples for Apache combined logs (following two examples are equivalent):
# LogFormat = 1
# LogFormat = "%host %other %logname %time1 %methodurl %code %bytesd %refererquot %uaquot"
#
# Example for IIS:
# LogFormat = 2
#
LogFormat=1

# Change to Apache log file, by default it's /var/log/apache2/access.log
LogFile="/var/log/apache2/access.log"

# Change to the website domain name
SiteDomain="test.com"
HostAliases="www.test.com localhost 127.0.0.1"

# When this parameter is set to 1, AWStats adds a button on report page to allow to "update" statistics from a web browser
AllowToUpdateStatsFromBrowser=1

#Default DNSLookup=2, disable DNS Lookup to save memory, set to DNSLookup=0
DNSLookup=0

Build the AWStats statistics from current apache2 logs

#/usr/lib/cgi-bin/awstats.pl -config=awstats.conf -update

or if use default awstats.conf

#/usr/lib/cgi-bin/awstats.pl -config=awstats.conf -update

If it is successful no errors, some output will shown:

Create/Update database for config "/etc/awstats/awstats.conf" by AWStats version 7.2 (build 1.992)
From data in log file "/var/log/apache2/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: 4473
Found 0 dropped records,
Found 0 comments,
Found 0 blank records,
Found 0 corrupted records,
Found 0 old records,
Found 173 new qualified records.

Configure Apache to work with AWStats

Copy code into apache config file, recommend put at the bottom

# 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 Installation path.
#
Alias /awstatsclasses "/usr/share/awstats/lib/"
Alias /awstats-icon "/usr/share/awstats/icon/"
Alias /awstatscss "/usr/share/doc/awstats/examples/css"
#ScriptAlias /awstats/ /usr/lib/cgi-bin/
# for Security, comment code above, add code below and change folder name 
ScriptAlias /vps/ /usr/lib/cgi-bin/
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
#

Restart Apache to reflect the changes.

#/etc/init.d/apache2 restart
#service apache2 restart

The AWStats  can access via browser at http://domain/vps/awstats.pl

Setup Cron Job to update every night at midnight  (https://crontab.guru)

#vim /etc/crontab

Put code below at bottom

0 0 * * * root /usr/lib/cgi-bin/awstats.pl -config=awstats.conf -update

Error and Solutions once update AWStats via web browser

Error: Couldn’t open server log file “/var/log/apache2/access.log” : Permission denied

Give apache2 (www-data) permission on apache2 log file

#chown -R www-data:www-data /var/log/apache2/
#service apache2 restart

Warning: Error while storing hashfile: can’t create /var/lib/awstats/dnscachelastupdate.hash: Permission denied at (eval 5) line 1.

Warning: Failed to open for writing last update DNS Cache file “/var/lib/awstats/dnscachelastupdate.txt”: Permission denied

Give apache2 (www-data) permission on /var/lib/awstats/

#chown -R www-data:www-data /var/lib/awstats

Reset AWStats statistics to zero

Go to defaultr DirData /var/lib/awstats, delete all files awstatsMMYYYY.txt

#cd /var/lib/awstats
#rm -f awstats**

Go to default Apache2 access.log DirData, delete all files access.log.**

#cd /var/log/apache2/
#rm -f access.**
#touch access.log
#chown -R www-data:www-data /var/log/apache2/

Then Build the AWStats statistics again from apache2 logs

#/usr/lib/cgi-bin/awstats.pl -config=awstats.conf -update
/var/log/apache2# /usr/lib/cgi-bin/awstats.pl -config=awstats.conf -update
Create/Update database for config "/etc/awstats/awstats.conf" by AWStats version 7.2 (build 1.992)
From data in log file "/var/log/apache2/access.log"...
Phase 1 : First bypass old records, searching new record...
Direct access to last remembered record is out of file.
So searching it from beginning of log file...
Jumped lines in file: 0
Parsed lines in file: 0
 Found 0 dropped records,
 Found 0 comments,
 Found 0 blank records,
 Found 0 corrupted records,
 Found 0 old records,
 Found 0 new qualified records.