So, I run a few websites 😉 and most of those websites (including this one) I have Google Analytics installed.
But server logs can provide different information. So I’m going to setup graphing and tracking my server access logs.
So my steps are going to be:
- Install Awstats, along with some prerequisites for CPAN, and creating a password file.
- Install CPAN The Comprehensive Perl Archive Network, along with the GeoIP plugin.
- Write a bash script to create, update, and graph, some results (as well as a wee PHP script to make life easier).
- Profit!
Prerequisites
I’m not going to talk you through setting up a Linux server, or Nginx, PHP-FPM (Fastcgi).
I will assume that you have a running server.
Your linux server can be any flavour you like, I prefer Debian and Ubuntu distros, and I prefer aptitude as a package manager; you can use whatever you like 🤗.
Assumptions
- I’ll assume we store the Awstats DB in /var/lib/awstats.
- I’ll assume we store the static reports in /var/cache/awstats.
Installing the packages
Here we go:
sudo aptitude install awstats libgeoip-dev build-essential openssl
Installing the GeoIP plugin
So first we need to setup CPAN:
Now, while in the CPAN shell, let’s install some stuff.
Firstly, CPAN itself, and then the GeoIP plugin.
cpan[1]> make install
cpan[1]> install Bundle::CPAN
That’ll get you up and running ready to install from cpan.
Now let’s install the GeoIP plugin:
cpan[1]> install Geo::IP
Setting up Nginx
So, a few prerequisites are needed for Nginx to behave properly.
Firstly, make sure you have PHP running using fastcgi (I’m not showing you how to do this in this doc).
Next, let’s make sure logging is separate for each website:
In my server {
block I have the following lines for logging:
I’m going to add some config to my server block specifically for awstats:
Now reload nginx systemctl reload nginx.service
Password protect our stats
Let’s not go public with our stats 😆
In the Nginx config above I provided for password protection, so let’s create that .htpasswd file:
Setting up Awstats
Right, so for whatever reason, awstats installs a cron job that doesn’t seem to work.
So let’s turn it off:
echo "" > /etc/cron.d/awstats
phew 😆
Now let’s create an awstats config for our website.
Awstats expects the config to be named “awstats.website.ext.conf”.
Something like:
- awstats.terminaladdict.com.conf
- awstats.example.co.nz.conf
- awstats.subdomain.example.org.nz.conf
I’m creating a config for terminaladdict.com, so here we have it:
Here’s a warning ! If your website is super busy, probably disable DNSLookup, by setting it to 0
DNSLooup = 1 will slow things down A LOT
The bash script
While I was setting this up I read many articles about configuring nginx to using cgi-bin.php with fastcgi.
For me, it just didn’t work, and I got bored trying to figure out why it didn’t work!
So I wrote my own little bash script to update awstats db, generate static reports, and build an index.php for some of the directories, just for ease of use.
Here is my script:
What the script does is:
- Set up some vairable, including a variable named PHPFILE, that creates a PHP file for some directories.
- in a for loop, for each awstats.{websitename}.conf do
- make a directory if needed (mkdir -p)
- a new for loop, that creates a PHP file in the root directory, and the $YEAR directory
- Updates the stats
$AWSTATS -config=$c -update
- create some soft links for ease of use
- a nested for loop that creates reports for the year, and the month
- 1).
$AWSTATS -config=$c -output=$stat -month=all -staticlinks > $DIR/$c/$YEAR/awstats.$c.$stat.html
- 2).
$AWSTATS -config=$c -output=$stat -staticlinks > $DIR/$c/$YEAR/$MONTH/awstats.$c.$stat.html
- profit!
And here is the result:
Automating
And of course, chuck it in cron so it updates regularly.
I run mine at 17 minutes passed the hour, because - well I dunno - 17 seemed like a fun number to use 🤪.
I stored the script in /usr/local/bin and called it awstats.update_and_build.sh
Happy stats collecting !
Leave a Comment
Liked what you've read?
It only takes a second of your time, but it means the world to me.
Don't comment on Facebook, comment here !
I don't keep any of your information, I only ask for a name, and an email to stop spammers!
Pretty please !! :)