How to install, automate and secure AWStats including GeoIP-Plugin on Debian

If you don’t want to use a 3rd-party website analytics tool like i.e. Google Analytics but still want to monitor your visitors, pageviews and so on, then AWStats might be the right tool for you. It works serverside by analyzing your logfiles and creating a simple graphical website on the most important info about your visitors. Although it is not as detailed as most frontend analytics tools it still provides a very good overview.
This is how installation works on Debian, assuming you are running Apache2 server:

apt-get install awstats

The debian installer does not automatically set the correct paths in the AWStats configuration script, so we have to do this manually (for all text editing just use your favorite editor, I will be using nano throughout this tutorial):

nano /usr/share/doc/awstats/examples/awstats_configure.pl
$AWSTATS_PATH='/usr/share/awstats';
$AWSTATS_ICON_PATH='/usr/share/awstats/icon';
$AWSTATS_CSS_PATH='/usr/share/awstats/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';
$AWSTATS_DIRDATA_PATH='/var/lib/awstats';

Now the configuration should be ok. In the next step we have to make the configuration script executable by the apache2 user, most probably www-data:

chown www-data /usr/lib/cgi-bin/awstats.pl

Now AWStats is already up and running. But we are still far from being done. Next we should check your apache2 configuration for the following lines and add them if not present (it will enable the AWStats icons for our AWStats infosite):

nano /etc/apache2/apache2.conf
Alias /awstats-icon/ /usr/share/awstats/icon/
<Directory /usr/share/awstats/icon>
    Options None
    AllowOverride None
    Order allow,deny
    Allow from all
</Directory>

Ok. Next thing to do is updating some settings in our AWStats configuration file. We need to set the correct path to our Apache access log, set our site domain and I would also suggest to change the default LogFormat and disable DNSLookup.

nano /etc/awstats/awstats.conf
LogFile="/var/log/apache2/access.log"
LogFormat=1
SiteDomain="webdevwonders.com"
DNSLookup=0

Now we can try to run AWStats for the first time (remember to fill in your domain name instead of mine):

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

If you get an error message with one or the other command you might want to check your configuration settings once again. One problem that might occur is access denied to the apache log. Fix it like this and try executing the commands from above once again:

chmod 755 /var/log/apache2

If everything works we can continue by enabling browser access to our statistics:

chown www-data /usr/lib/cgi-bin/awstats.pl

Now try to open the AWStats statistics in your browser. The path should look like this (again of course your own domain name): http://webdevwonders.com/cgi-bin/awstats.pl. If you receive a 504 Gateway Timeout you will have to increase the timeout value inside apache2.conf. Something between 20 and 60 seconds should be enough.
Now next is the automation of our statistics by adding a cronjob that will automatically update them:

nano /etc/crontab

Add the following line for a 15 minute crontab or change the update cycle according to your needs (adjust domain name as usual).

/15 *   * * *   root    /usr/lib/cgi-bin/awstats.pl -config=awstats.webdevwonders.com.conf

Now let’s protect our statistics from prying eyes. We are doing this by adding a .htaccess file to our “cgi-bin”-directory:

cd /usr/lib/cgi-bin/
touch .htaccess
nano .htaccess

Now insert the following (please note that for this tutorial we will simply put the .htpasswd file in “/var/www/awstats” but you might consider putting it somewhere else).

<FilesMatch "awstats.pl">
    AuthName "Login Required"
    AuthType Basic
    AuthUserFile /var/www/awstats/.htpasswd
    require valid-user
</FilesMatch>

Now create the .htpasswd file (remember to fill in your own username):

cd /var/www/
mkdir awstats
cd awstats
htpasswd -c /var/www/awstats/.htpasswd username

Almost done, but we have to enable “AllowOverride” for the “cgi-bin”-directory to have Apache make use of our access rule. So edit the part inside your VirtualHost configuration:

nano /etc/apache2/sites-available/default
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
    AllowOverride All
    ...
</Directory>

One more thing: Restart Apache.

/etc/init.d/apache2 restart

Check your AWStats URL once again. It should be protected. AWStats configuration is fine like this and will show you lots of nice info about your visitors already, but there is one more module called GeoIP which is a “nice to have”-plugin. It will show you the country where your visitors are located at, even with a nice country flag icon!
Before we can start installing GeoIP we need to install the GNU GCC Compiler, which is included in the “build-essential”-package, as well as the library zlib:

apt-get install build-essential
wget http://zlib.net/zlib-1.2.7.tar.gz
tar xvzf zlib-1.2.7.tar.gz
cd zlib-1.2.7
./configure --prefix=/usr/local/zlib && make && make install

Now let’s go ahead and download and install the GeoIP plugin:

wget http://maxmind.com/download/geoip/api/c/GeoIP.tar.gz
tar xzvf GeoIP.tar.gz
cd GeoIP*
./configure && make && make install

Although installation should work, in some cases you might get an error like this:

checking for zlib.h... no
configure: error: Zlib header (zlib.h) not found. Tor requires zlib to build.
You may need to install a zlib development package.

This problem can be fixed by installing zlib1g-dev package. Afterwards retry the installation:

apt-get install zlib1g-dev
./configure && make && make install

Now continue with removing all previous installation files and finish installation of GeoIP:

cd ..
cd ..
rm -rfv zlib*
cpan
install Geo::IP
quit

One final step: Check in your AWStats configuration if the LoadPlugin is enabled and the path is set correctly:

nano /etc/awstats/awstats.conf
LoadPlugin="geoip GEOIP_STANDARD /usr/local/share/GeoIP/GeoIP.dat"

Now, just for the sake of making sure everything is enabled, let’s restart Apache once more:

/etc/init.d/apache2 restart

And that’s it! Hopefully you will have AWStats up and running as expected. If anything in this tutorial is missing, not working or simply wrong please let me know via my contact form so I can update the tutorial accordingly. Thanks!

Please vote: How useful was this post for you?
Current rating:
(5 out of 5)
This entry was posted in Linux and tagged , , , , . Bookmark the permalink.

Comments are closed.