Awstats, Nginx log and Nginx Server

Install Required Packages
yum install awstats htmldoc geoip-geolite perl-Geo-IP GeoIP httpd-tools

Install CPAN
# yum install perl-CPAN perl-CPAN-Changes perl-CPAN-Meta perl-CPAN-Meta-Requirements
# cpan CPAN
perl -MCPAN -e "install Geo::IP::PurePerl"
perl -MCPAN -e "install Geo::IP"

Awstats Config creation
# First define our website as a variable.
# We will use this value to track and store in an
# organized structure.

# Those who host other websites for people can
# change this and virtually everything below will
# and re-run everything to get stats for that too!
WEBSITE=nuxref.com

# AWSTATS Variable Data
DATADIR=/var/lib/awstats/$WEBSITE
# Make sure our environment variables are defined
# WEBSITE and DATADIR

# First we need to setup our DATADIR; this is where
# all our statistics and generated data will be placed
# into:
[ ! -d $DATADIR/static ] && \
mkdir -p $DATADIR/static
ln -snf /usr/share/awstats/wwwroot/icon \
$DATADIR/static/icon
ln -snf /usr/share/awstats/wwwroot/cgi-bin \
$DATADIR/static/cgi-bin

# Create a configuration file using our website
# based on the awws.model.conf example file that
# ships with AWStats
sed -e "s|localhost\.localdomain|$WEBSITE|g" \
/etc/awstats/awstats.model.conf > \
/etc/awstats/awstats.$WEBSITE.conf

########################################
# Now update our new configuration
########################################
# Update the LogFile with our access.log file we'll
# reference. This path doesn't exist yet but
# we'll be creating it soon enough; leave this entry
# untouched (don't change it to your real log path!):
sed -i -e "s|^\(LogFile\)=.*$|\1=\"$DATADIR/access.log\"|g" \
/etc/awstats/awstats.$WEBSITE.conf

# Disable DNS (for speed mostly)
sed -i -e "s|^\(DNSLookup\)=.*$|\1=0|g" \
/etc/awstats/awstats.$WEBSITE.conf

# For PDF Generation we need to update the relative
# paths for the icons.
sed -i -e "s|^\(DirIcons\)=.*$|\1=\"icon\"|g" \
/etc/awstats/awstats.$WEBSITE.conf
sed -i -e "s|^\(DirCgi\)=.*$|\1=\"cgi-bin\"|g" \
/etc/awstats/awstats.$WEBSITE.conf
sed -i -e "s|^\(LogFormat\)=.*$|\1=\"%host %other %logname %time1 %methodurl %other %other %code %bytesd %refererquot %uaquot %other %other\"|g" \
/etc/awstats/awstats.$WEBSITE.conf

Update GEOIP (Optional)
# Now configure our GEOIP Setup
sed -i -e '/^LoadPlugin=.*/d' /etc/awstats/awstats.$WEBSITE.conf
cat << _EOF >> /etc/awstats/awstats.$WEBSITE.conf
LoadPlugin="geoip GEOIP_STANDARD /usr/share/GeoIP/GeoIP.dat"
LoadPlugin="geoip_city_maxmind GEOIP_STANDARD /usr/share/GeoIP/GeoIPCity.dat"
_EOF

# downloads all of the latest GEO IP content to
# /usr/share/GeoIP with this simple command:
geoipupdate

# This IP information changes often; so the next
# thing you want to do is create a cronjob to have
# this tool fetch regular updates automatically for
# us to keep the GEO IP Content fresh and up to date!
cat << _EOF > /etc/cron.d/geoipdate
0 12 * * 3 root /usr/bin/geoipupdate &>/dev/null
_EOF

First Time Consolidate Data (Optional)
/usr/share/awstats/tools/logresolvemerge.pl /data/nginx/logs/access.log /data/nginx/logs/access.log-*.gz > $DATADIR/access.log

Cron Script to Update Data
vi /etc/awstats/update_$WEBSITE.sh

WEBSITE=domain.com
DATADIR=/var/lib/awstats/$WEBSITE
/usr/share/awstats/tools/logresolvemerge.pl /data/nginx/logs/access.log > $DATADIR/access.log
/usr/share/awstats/wwwroot/cgi-bin/awstats.pl -config=$WEBSITE
/usr/share/awstats/tools/awstats_buildstaticpages.pl -config=$WEBSITE -buildpdf -dir=$DATADIR/static -buildpdf=/usr/bin/htmldoc

crontab
5 * * * * /etc/awstats/update_$WEBSITE.sh &

Setup htpassword
htpasswd -c /var/lib/awstats/$WEBSITE/.htpasswd username

Setup Nginx Virtual Host
vi /usr/local/nginx/conf/http_facadehost/awstats/awstats.$WEBSITE.conf

server {
listen *:80;
server_name IPADDRESS;
auth_basic "Private Property";
auth_basic_user_file /var/lib/awstats/$WEBSITE/.htpasswd;

location /stats/$WEBSITE.com/ {
alias /var/lib/awstats/$WEBSITE.com/static/;
index awstats.$WEBSITE.html;
}

location /stats/css/ {
alias /usr/share/awstats/wwwroot/css/;
}

location /stats/icon/ {
alias /usr/share/awstats/wwwroot/icon/;
}
}

Be the first to comment

Leave a Reply

Your email address will not be published.


*