Awstats + nginx : installation procedure

From kraba notes
Jump to: navigation, search

Info

Installing awstats - a web analytics reporting tool - from source with nginx as web server and GeoIP plugin.

Prerequisites : an nginx+php-pfm working environment.

Example configuration : www.mysite.com, nginx access log at /var/log/nginx/mysite.com.acces.

Installing awstats

Downloading the package and prepare it:

cd /usr/local/
wget https://prdownloads.sourceforge.net/awstats/awstats-7.7.tar.gz  (or latest stable)
tar zxvf awstats-7.7.tar.gz
mv awstats-7.7 awstats
chown www-data. -R awstats

Create the working directory:

mkdir /var/lib/awstats
chown www-data.  /var/lib/awstats

Prepare the sofware:

cd /usr/local/awstats/tools
perl awstats_configure.pl

and answer the questions (none for webserver). Now edit the configuration file for awstats, configure.pl script generate it with the correct name:

cd /etc/awstats
vi /etc/awstats/awstats.www.mysite.com.conf

A simple complete configuration file is, feel free to read all 1500 rows :) :

LogFile="/var/log/nginx/myste.com.access"
LogType=W
LogFormat=1
LogSeparator=" "
SiteDomain="mysite.com"
HostAliases="www.mysite.com mysite.com wiki.mysite.com"
DNSLookup=2
DirData="/var/lib/awstats/"
DirCgi="/cgi-bin"
DirIcons="/icon"
AllowToUpdateStatsFromBrowser=0
AllowFullYearView=2
Logo="logo.png" # put your logo in /usr/local/awstats/wwwroot/icon/other/
LogoLink="http://www.mysite.com"
LoadPlugin="hostinfo"
LoadPlugin="graphgooglechartapi"
ExtraTrackedRowsLimit=500

and launch for the first time the collector:

/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=mysite.com -configdir=/etc/awstats

Create/Update database for config "/etc/awstats/awstats.mysite.com.conf" by AWStats version 7.7 (build 20180105) 
From data in log file "/var/log/nginx/mysite.com.access"...
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: 15807
 Found 4 dropped records,
 Found 0 comments,
 Found 0 blank records,
 Found 8 corrupted records,
 Found 0 old records,
 Found 15795 new qualified records 

and a new file will be created at /var/lib/awstats.

Adding automatic update on crontab

vi /etc/cron.d/awstats-update

 05 */1 * * * root /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=mysite.com -configdir=/etc/awstats > /dev/null 2>&1

GeoIP installation

Download and install (gcc is required...):

mkdir /usr/local/share/GeoIP/ 
cd /usr/local/share/GeoIP/
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz 
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz 
wget http://geolite.maxmind.com/download/geoip/database/asnum/GeoIPASNum.dat.gz
wget http://geolite.maxmind.com/download/geoip/api/c/GeoIP-1.4.5.tar.gz 
wget http://geolite.maxmind.com/download/geoip/api/perl/Geo-IP-1.35.tar.gz 
tar zxvf Geo-IP-1.35.tar.gz
tar zxvf GeoIP-1.4.5.tar.gz 
gunzip *.dat.gz
cd GeoIP-1.4.5 
./configure 
make 
make check 
make install
cd ../Geo-IP-1.35
perl Makefile.PL LIBS='-L/usr/local/lib'  
make 
make test 
make install

Add these lines to awstats conf file to enable the plugin:

vi /etc/awstats/awstats.www.mysite.com.conf

LoadPlugin="geoip GEOIP_STANDARD /usr/local/share/GeoIP/GeoIP.dat" 
LoadPlugin="geoip_city_maxmind GEOIP_STANDARD /usr/local/share/GeoIP/GeoLiteCity.dat" 
LoadPlugin="geoip_org_maxmind GEOIP_STANDARD /usr/local/share/GeoIP/GeoIPASNum.dat"

Nginx configuration

Create an empty directory for awstats:

mkdir /var/www/stat

Add a cgi-bin.php file:

vi /etc/nginx/cgi-bin.php

 <?php
$descriptorspec = array(
   0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
   1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
   2 => array("pipe", "w")   // stderr is a file to write to
);
$newenv = $_SERVER;
$newenv["SCRIPT_FILENAME"] = $_SERVER["X_SCRIPT_FILENAME"];
$newenv["SCRIPT_NAME"] = $_SERVER["X_SCRIPT_NAME"];
if (is_executable($_SERVER["X_SCRIPT_FILENAME"])) {
   $process = proc_open($_SERVER["X_SCRIPT_FILENAME"], $descriptorspec, $pipes, NULL, $newenv);
   if (is_resource($process)) {
       fclose($pipes[0]);
       $head = fgets($pipes[1]);
       while (strcmp($head, "\n")) {
           header($head);
           $head = fgets($pipes[1]);
       }
       fpassthru($pipes[1]);
       fclose($pipes[1]);
       fclose($pipes[2]);
       $return_value = proc_close($process);
   } else {
       header("Status: 500 Internal Server Error");
       echo("Internal Server Error");
   }
} else {
   header("Status: 404 Page Not Found");
   echo("Page Not Found");
}
?>

and create the vhost file.

The complete nginx configuration file is available here @ wiki.matteobasso.com or here @ kraba@gist

Test

Go to http://awstats.mysite.com/mysite.com and all stats appear. The link will be : http://awstats.mysite.com/cgi-bin/awstats.pl?config=mysite.com

Comments

blog comments powered by Disqus