CodePlea icon
CodePlea
Random thoughts on programming
22 Dec 2016

GoAccess, Analytics, and PHP


GoAccess is an excellent open source web log analyzer. It makes nice, fancy charts and such:

Screenshot of GoAccess output

GoAccess has several modes of running, including outputting HTML, or severing HTML live with its built-in webserver. I knew I wanted to use GoAccess to analyze my server logs, but I wasn't sure what the best way to use it was.

The built-in webserver has the advantage of always being up-to-date. It has several drawbacks though. Getting it configured to start with the server would be a pain. Keeping it running for each of my sites on different ports would be a mess. Although it does support HTTPS, having it locate my certs would be even more hassle. Finally, I don't know if it can be password protected or not. I could work around a lot of that with a reverse proxy, but then that's a pain to setup and maintain too.

I like easy and simple things. I don't like to setup complicated solutions, because they usually end up being fragile and requiring more maintenance.

So I was looking at having GoAccess run from a cron job, creating a new HTML report every day, and having my webserver serve that. The biggest issues with this is that it'll always be a bit outdated. Even if I run the cron job every hour, it still won't have that "live" feel that I want.

My final solution, I think is a good one. I decided to have GoAccess run from a PHP script on demand. I already have PHP installed, it already runs with HTTPS as I want, and it's trivial to add password protection to a PHP page.

It turns out that having PHP call GoAccess and display the HTML output is simple.

<?php
$output = shell_exec('zcat -f /var/PATH/TO/ACCESS.LOG* | goaccess -a');
echo "$output";
?>

The zcat part is so that GoAccess will look at the older rotated logs too, not just the most recent one.

That's really all it takes! Super simple, not much to go wrong, and all I have to do is visit that page to get current analytics. For me, this was the best way to use GoAccess with the least amount of hassle.


Like this post? Consider following me on Twitter or following me on Github. Don't forget to subscribe to my feed.