Piwik Analytics for Ghost using Docker and Caddy

Piwik Analytics for Ghost using Docker and Caddy

If you want to see how many people are visiting your site you will need some kind of analytics. Piwik is great because it is open source and works well with Docker.

Read on for documented steps to install Piwik with Docker and Caddy. Piwik depends on MySQL so I'm going to be installing that too.

Swap Space

This whole process would have been pretty straightforward if I had listened to my good friend Darren and set up a memory swap space weeks ago. I am currently running the cheapest Digital Ocean offering which only has 512mb of ram. I ran into a lot of issues with the MySQL container crashing and ending up in an unrecoverable state. Fortunately Docker made it simple and carefree to haphazardly create and delete containers repeatedly until I realized the problem.

A ram
Old Ram by Yellowstone Gate / CC BY 2.0

On a side note, the only reason I eventually ended up with the solution was on a hunch. I still haven't found any way to troubleshoot Docker containers other than log messages provided by the container itself. I would like to research this further..

TLDR; Set up a swap space on your server and avoid headaches - 1GB was fine for this exercise. Create a Swap File on Cloud Linux Server to Prevent Out of Memory

Set up the Containers

Three containers are required for Piwik -

The order of container creation matters in this case because of dependencies. Piwik depends on MySQL. Caddy uses a virtual volume directly from Piwik.

If you run into trouble with containers crashing, try creating or increasing server RAM swap space

MySQL

docker run --name mysqlcontainer -v /var/lib/mysql-docker:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=SuperSecretPassword -d mysql

Piwik

docker run --name piwik -v /var/lib/piwik-docker:/var/www/html --link mysqlcontainer:mysql -d piwik

Caddy

docker run -d -p 80:80 -p 443:443 --name caddyphp -v /etc/caddy/config/caddyFile:/etc/Caddyfile -v /etc/caddy/srv/:/srv -v /etc/caddy/certs:/root/.caddy --volumes-from piwik abiosoft/caddy:php

For this dependency tree and from PHP dependencies I had to rebuild my Caddy container. I included the Piwik volume using the --volumes-from flag. I don't believe the Piwik container comes with its own web server so Caddy is directly serving the files. Since Piwik is written with PHP, Caddy needs a PHP interpreter so after some duck-duck-going I used the :php tag from the aboisoft/caddy template. The :php tag supposedly includes some extra PHP stuff within the container, but per my various tests it may not be necessary for Piwik.

Caddy Configuration

Since Piwik is written in PHP, some additional instructions are required for hosting its content in the CaddyFile.

piwik.myserver.com {
  root /var/www/html #Virtual Dir Shared with Piwik and Caddy containers
  fastcgi / 172.17.0.4:9000 php #Special configuration
}

Read more at the CaddyFile Docs

Piwik Configuration

At this point if you navigate to Piwik it should walk you through a wizard. The Piwik Docker Documentation can walk you through this:

Once started, you'll arrive at the configuration wizard. At the Database Setup step, please enter the following:

Database Server: db
Login: root
Password: MYSQL_ROOT_PASSWORD
Database Name: piwik (or you can choose)
And leave the rest as default.

Remaining Piwik Setup

The rest of the setup for Piwik is documented on their website. Refer to Piwik Installation Documentation http://piwik.org/docs/installation/#super-user

Picnic-Table-2_Large by TeacherPouch LLC / CC BY-SA-NC 3.0

Ghost Integration

Eventually Piwik will present you with some code to embed in your site. You can either embed this directly in the header, or do it the way I like to do it by using a script reference and storing the javascript file inside of a Ghost theme

For the embed:

<script type="text/javascript" src="/assets/js/demro/piwikBootstrap.js"></script>
//document.addEventListener("DOMContentLoaded", function(event) { 
 var _paq = _paq || [];
  _paq.push(['trackPageView']);
  _paq.push(['enableLinkTracking']);
  (function() {
    var u="//piwik.myserver.com/";
    _paq.push(['setTrackerUrl', u+'piwik.php']);
    _paq.push(['setSiteId', '1']);
    var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
    g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
  })();
//});

piwikBootstrap.js - Store this inside of your Ghost theme in a javascript folder. Don't forget to use a symbolic link just in case.

From here you should start seeing hits on your website translate to blips on the Piwik dashboard. Keep in mind that if you're using uBlock Origin or some other ad blocking software your browser will not generate hits. You will need to temporarily disable these addons, use private browsing, a mobile device or some other workaround.