AWStats

February 13, 2025 by Roberto Puzzanghera 0 comments

A big thanks to Bai Borko for providing the instructions from which I derived the following and for the perl script.

AWStats is a free powerful and featureful 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. It can analyze log files from all major server tools like Apache log files (NCSA combined/XLF/ELF log format or common/CLF log format), WebStar, IIS (W3C log format) and a lot of other web, proxy, wap, streaming servers, mail servers and some ftp servers.

First of all, let's define some variables so that we can satisfy different systems and customizations.

# AWStats version
AWSTATS_VERSION=7.9
# where we want to extract the AWStats package
AWSTATS_PREFIX=/var/www/htdocs
# configuration files dir
AWSTATS_CONF_DIR=/etc/awstats
# where to save the data
DATA_DIR=/var/www/htdocs/stats
# the mx domain in the title of the site
DOMAIN=mx.domain.tld
# Alias domain (for the regular expression,  do not delete the slashes)
HOST_ALIAS=domain\\.tld
# apache user: apache or www-data or whatelse runs your web server
APACHE_USR=apache
# apache group
APACHE_GRP=$APACHE_USR
# the file which holds the apache password
APACHE_PWD_FILE=/etc/httpd/httpdpwd/awstats.passwd

Download and extract:

cd /usr/local/src
wget https://prdownloads.sourceforge.net/awstats/awstats-${AWSTATS_VERSION}.tar.gz
tar -zxf awstats-${AWSTATS_VERSION}.tar.gz -C $AWSTATS_PREFIX
ln -s ${AWSTATS_PREFIX}/awstats-${AWSTATS_VERSION} ${AWSTATS_PREFIX}/awstats

Create the configuration file (modify at least the domain):

mkdir $AWSTATS_CONF_DIR

cat > ${AWSTATS_CONF_DIR}/awstats.mail.conf << __EOF__
LogFile="sudo ${AWSTATS_CONF_DIR}/qstatsaw.pl|"
LogType=M
LogFormat="%time2 %email %email_r %host %url %protocol %code %bytesd"
SiteDomain="${DOMAIN}"
HostAliases="localhost 127.0.0.1 REGEX[${HOST_ALIAS}]"
DirData="${DATA_DIR}"
DirCgi="/awstats"
DefaultFile="index.html"
LogoLink="/awstats.pl?config=mail"
Logo="awstats_logo6.png"
DirIcons="/awstatsicons"
DNSLookup=0
#AllowAccessFromWebToFollowingIPAddresses="192.168.0.2-192.168.0.240"
AllowToUpdateStatsFromBrowser=1
LevelForBrowsersDetection=0 # 0 disables Browsers detection.
LevelForOSDetection=0 # 0 disables OS detection.
LevelForRefererAnalyze=0 # 0 disables Origin detection.
LevelForRobotsDetection=0 # 0 disables Robots detection.
LevelForSearchEnginesDetection=0 # 0 disables Search engines detection.
LevelForKeywordsDetection=0 # 0 disables Keyphrases/Keywords detection.
LevelForFileTypesDetection=0 # 0 disables File types detection.
ShowMenu=1
ShowSummary=HBM
ShowMonthStats=HBM
ShowDaysOfMonthStats=HBM
ShowDaysOfWeekStats=HBM
ShowHoursStats=HBM
ShowDomainsStats=0
ShowHostsStats=HBL
ShowAuthenticatedUsers=0
ShowRobotsStats=0
ShowWormsStats=0
ShowEMailSenders=HBML
ShowEMailReceivers=HBML
ShowSessionsStats=0
ShowPagesStats=0
ShowFileTypesStats=0
ShowFileSizesStats=0
ShowOSStats=0
ShowBrowsersStats=0
ShowScreenSizeStats=0
ShowOriginStats=0
ShowKeyphrasesStats=0
ShowKeywordsStats=0
ShowMiscStats=0
ShowHTTPErrorsStats=0
ShowSMTPErrorsStats=1
ShowClusterStats=0
AddDataArrayMonthStats=1
AddDataArrayShowDaysOfMonthStats=1
AddDataArrayShowDaysOfWeekStats=1
AddDataArrayShowHoursStats=1
IncludeInternalLinksInOriginSection=0
SkipHosts=""
SkipUserAgents=""
SkipFiles=""
SkipReferrersBlackList=""
OnlyHosts=""
OnlyUserAgents=""
OnlyFiles=""
__EOF__

Download the script which is going to process the logs:

wget -O ${AWSTATS_CONF_DIR}/qstatsaw.pl https://notes.sagredo.eu/files/qmail/qstatsaw.pl

Add execute permissions

chmod 755 ${AWSTATS_CONF_DIR}/qstatsaw.pl

Add this line to the sudoers file

> visudo
apache ALL=(ALL:ALL) NOPASSWD: /etc/awstats/qstatsaw.pl

Set the cron job for AWStats update

cat > /etc/cron.d/awstats << __EOF__
# Update AWStats
*/10 * * * * ${AWSTATS_PREFIX}/awstats/wwwroot/cgi-bin/awstats.pl -update -config=mail -output> ${DATA_DIR}/index.html > /dev/null 2>&1
__EOF__

Create the data dir and assign proper privileges

mkdir $DATA_DIR
# if set AllowToUpdateStatsFromBrowser=1, stats directory must be writable for apache user 
chown -R ${APACHE_USR}:${APACHE_GRP} $DATA_DIR

Create the authorization file and assign read privileges to the web server user (you can use the same file of vQadmin here):

htpasswd -bc ${APACHE_PWD_FILE} admin [password]
chown ${APACHE_USR}:${APACHE_GRP} ${APACHE_PWD_FILE}
chmod o-r ${APACHE_PWD_FILE}

Manually run the AWStats script:

${AWSTATS_PREFIX}/awstats/wwwroot/cgi-bin/awstats.pl -update -config=mail -output> ${DATA_DIR}/index.html

Configure apache. This is my dedicated virtual host.

Define HTDOCS   /var/www/htdocs
Define LOGDIR   /var/www/error
Define AUTH_DIR /etc/httpd/httpdpwd
Define AWSTATS  ${HTDOCS}/awstats
Define DATA_DIR ${HTDOCS}/stats

<VirtualHost *:80> 
  ServerName qstats.domain.tld 
  DocumentRoot ${AWSTATS}/wwwroot 

  ScriptAlias /cgi-bin/ ${AWSTATS}/cgi-bin/ 
  AddHandler cgi-script .pl 

  Alias /awstatsclasses ${AWSTATS}/wwwroot/classes/
  Alias /awstatscss     ${AWSTATS}/wwwroot/css/
  Alias /awstatsicons   ${AWSTATS}/wwwroot/icon/
  ScriptAlias /         ${AWSTATS}/wwwroot/cgi-bin/

  ErrorLog  ${LOGDIR}/awstats_error.log 
  CustomLog ${LOGDIR}/awstats_access.log common 

  <DirectoryMatch ${AWSTATS}|${DATA_DIR}> 
    AllowOverride AuthConfig 
    AuthType Basic 
    AuthUserFile ${AUTH_DIR}/awstats.passwd 
    AuthName "Qstats" 
    Require ip 10.0.0.0/24 
    Require valid-user 
  </DirectoryMatch> 
</VirtualHost>

Now browse to http://qstats.domain.tld/awstats.pl?config=mail and you're done.

Add a comment

Recent comments
Recent posts

RSS feeds