Ubuntu 11.04 / OpenPanel – Install Webalizer

Make sure you make all changes as user “root”.

Install Webalizer

apt-get install webalizer

First change the default webalizer config.

nano /etc/webalizer/webalizer.conf

Incremental processing allows multiple partial log files to be used instead of one huge one. And OpenPanel creates multiple files by default, so lets set “incremental” to yes.

#Incremental     no
Incremental     yes

OpenPanel created apache log files per virtual host, so we don’t have to do that by ourselves. The access log files are located at “/var/log/apache2/openpanel/logs”.

To generate stats from an apache access log file, you’ll have to execute the following command:

/usr/bin/webalizer -c /etc/webalizer/webalizer.conf -n www.example.com -s www.example.com -r www.example.com -q -T -o /var/www/webalizer/www.example.com /var/log/apache2/openpanel/logs/www.example.com.log

I created a shell script to automatically create stats for all virtual hosts. Put it for example at “/opt/webstats.sh”

#!/bin/sh

logDir=/var/log/apache2/openpanel/logs
webalizerConf=/etc/webalizer/webalizer.conf
webalizerStatsDir=/var/www/webalizer
vhostDir=/home/openpanel-admin/sites

cd ${logDir}
for file in *.log
do
  if [ -f ${file} ]; then
    virtualHost=`basename ${file} .log`

    # Only create stats from log files that belong to virtual hosts
    if [ -d ${vhostDir}/${virtualHost} ]; then

      # Create stats directory for the virtual host if it doesn't exist yet
      if [ ! -d ${webalizerStatsDir}/${virtualHost} ]; then
        mkdir ${webalizerStatsDir}/${virtualHost}
      fi

      # Create stats
      /usr/bin/webalizer -c ${webalizerConf} -n ${virtualHost} \
      -s ${virtualHost} -r ${virtualHost} -q -T -o ${webalizerStatsDir}/${virtualHost} \
      ${logDir}/${file}      
    fi
  fi
done

Create a cronjob for it with:

crontab -e

And add the following file to generate stats every 5 minutes:

*/5 * * * * /opt/webstats.sh

If you want to view the stats in your browser go to “http://myserveripaddress/webalizer/www.example.com”. But you probably also want access with “http://www.example.com/webalizer”. To make this happen, extend your virtual host file with:

nano /etc/apache2/openpanel.d/www.example.com.inc/extended.conf

And add:

Alias /webalizer /var/www/webalizer/www.example.com

Restart the Apache web server

service apache2 restart

And you’re done! Everything should be working smoothly now.

Password protection

Optionally, you can also add password protection to the Webalizer directory. This way your stats will not be publicly available for everyone. Add the following commands to add the protection.

nano /var/www/webalizer/.htaccess

And add:

AuthGroupFile /dev/null
AuthName "Password Protected Area"
AuthType Basic
AuthUserFile /var/www/webalizer/.htpasswd
require valid-user

Create .htpasswd:

touch /var/www/webalizer/.htpasswd

And add a user (for example “admin”), you’ll be prompted to define a password.

htpasswd /var/www/webalizer/.htpasswd admin

Restart Apache web server

service apache2 restart

All your virtual host stats are password protected now with the user/pass defined above.

Tags: ,,