Installing Awstats for Apache2 on Debian

From Cosmin's Wiki

Jump to: navigation, search

Home > Linux how to's > Installing Awstats for Apache2 on Debian



After having set up a new site, I wanted to have some traffic statistics for it. For this, I decided to install AWStats. However, as I soon found out, this is not such a straightforward process, at least not on Debian. After struggling a while I managed to set it up, and below is a list of steps I have done to accomplish it. Hope you will find it useful.

# aptitude install awstats
# cd /usr/share/doc/awstats/examples
# gunzip awstats.model.conf.gz 

Now edit the following file:

# vim awstats_configure.pl


Set now the correct paths:

 
$AWSTATS_PATH='/usr/share/awstats';
$AWSTATS_ICON_PATH='/usr/share/awstats/icon';
$AWSTATS_CSS_PATH='/usr/share/doc/awstats/examples/css';
$AWSTATS_CLASSES_PATH='/usr/share/awstats/lib';
$AWSTATS_CGI_PATH='/usr/lib/cgi-bin';
$AWSTATS_MODEL_CONFIG='/usr/share/doc/awstats/examples/awstats.model.conf';             # Used only when configure ran on linux
$AWSTATS_DIRDATA_PATH='/var/lib/awstats';                                               # Used only when configure ran on linux
 


Modify now this:

 
# Define model config file path
# -----------------------------
my $modelfile='';
if ($OS eq 'linux')             {
        if (-f "$AWSTATS_PATH/wwwroot/cgi-bin/awstats.model.conf") {
                $modelfile="$AWSTATS_PATH/wwwroot/cgi-bin/awstats.model.conf";
        }
        else {
                $modelfile="$AWSTATS_MODEL_CONFIG";
                if (! -s $modelfile || ! -w $modelfile) { $modelfile="$AWSTATS_PATH/wwwroot/cgi-bin/awstats.model.conf"; }
        }
}
 

with

 
# Define model config file path
# -----------------------------
my $modelfile='';
if ($OS eq 'linux')             {
        if (-f "$AWSTATS_PATH/wwwroot/cgi-bin/awstats.model.conf") {
                $modelfile="$AWSTATS_PATH/wwwroot/cgi-bin/awstats.model.conf";
        }
        else {
                $modelfile="$AWSTATS_MODEL_CONFIG";
                if (! -s $modelfile || ! -w $modelfile) { $modelfile="$AWSTATS_MODEL_CONFIG"; }
        }
}
 

Now you can finally run:

# perl /usr/share/doc/awstats/examples/awstats_configure.pl

The application will complain about not being run from the location it knew, but just answer with 'Y', then follow the rest of the steps.

Once finished, you still need to do some changes:

  • Edit /etc/apache2/apache2.conf, remove the lines added at the end by awstats and just add in the Alias secion the following:

 Alias /awstatsicons/ "/usr/share/awstats/icon/"
 
    <Directory "/usr/share/awstats/icon">
        Options Indexes MultiViews
        AllowOverride None
        Order allow,deny
        Allow from all
    </Directory>
  • Restart Apache
  • Edit the configuration files in /etc/awstats and make sure the following elements are correctly set:
    • LogFile=”/var/log/apache2/access.log”
    • LogFormat=1
    • SiteDomain=”cosminswiki.com”
  • Now run awstats for the first time like this:

# /usr/lib/cgi-bin/awstats.pl -config=cosminswiki.com


To add some basic security to your AWStats, edit in /etc/apache2/sites-available the config file for your site and add to it:

 
        ScriptAlias /stats/ /usr/lib/cgi-bin/
       <Directory "/usr/lib/cgi-bin">
            AllowOverride None
            Options ExecCGI -MultiViews +SymLinksIfOwnerMatch
            Order allow,deny
            Allow from all
 
            AuthType basic
            AuthName "cgi-bin restricted"
            AuthUserFile /etc/awstats/awstats.htpasswd
            <Files awstats.pl>
                Require valid-user
            </Files>
       </Directory>
 

User and password for the basic authentication in Apache has been generated with

htpasswd -c /yourpath user

We need now AWStats to automatically generate the stats, and not having it clash with Apache's logs. So, edit /etc/logrotate.d/apache2, we make sure we have...

 
create 644 root adm
prerotate
/usr/lib/cgi-bin/awstats.pl -config=cosminswiki.com -update
endscript
postrotate
if [ -f /var/run/apache2.pid ]; then
if [ -x /usr/sbin/invoke-rc.d ]; then
invoke-rc.d apache2 restart > /dev/null;
else
/etc/init.d/apache2 restart > /dev/null;
fi
fi
endscript
 


Then, in /etc/cron.d/awstats, we have the following,

 
3,33 * * * * www-data [ -x /usr/lib/cgi-bin/awstats.pl -a -f /etc/awstats/awstats.cosminswiki.com.conf -a -r /var/log/apache2/access.log ] && /usr/lib/cgi-bin/awstats.pl -config=cosminswiki.com -update >/dev/null


Restart Apache

/etc/init.d/apache2 restart
and we are done! Now you can see statistics for each of your domains by typing in your browser
http://yourdomain.com/stats/awstats.pl?config=cosminswiki.com