Self-Hosted Analytics Using Piwik and Dokku

Hi friend! :)

This is a guide to help you host your own analytics using piwik and dokku.

This guide is a little rough around the edges, I can’t guarantee that everything works. If it does or does not, message me! <3

Last tested with Piwik 2.14.3 and Dokku v0.3.26.

Prerequisites

This guide assumes:

For reference, here are the vanilla Piwiki installation instructions.

Step 1: Fork piwik repository

Go to piwik/piwik and click on the top right “Fork” button to fork the project as your current user. Now, in your new piwik repo, click on “Settings”, then rename your repo to analytics.<your-dokku-domain.tld>.

Next, clone your piwik repo to your local machine with

1
2
git clone https://github.com/<your-name>/analytics.<your-dokku-domain.tld>
cd analytics.<your-dokku-domain.tld>

Add the dokku remotes

1
git remote add dokku dokku@<your-dokku-domain.tld>:analytics

Push to dokku!

1
git push dokku master

It won’t work, but soon. :p

Step 2: Install dokku plugins

Let’s install some plugins. :) To do this, login to our dokku server with ssh root@<your-dokku-domain.tld>.

1
cd /var/lib/dokku/plugins

mariadb

1
2
3

git clone https://github.com/krisrang/dokku-mariadb mariadb
dokku plugins-install

step 3. configure server

time to configure, so our analytics app has what it needs.

first make some directories on the server:

1
2
3
4
5
6
7
mkdir /var/www/analytics/
mkdir /var/www/analytics/tmp/assets
mkdir /var/www/analytics/tmp/cache
mkdir /var/www/analytics/tmp/logs
mkdir /var/www/analytics/tmp/tcpdf
mkdir /var/www/analytics/tmp/templates_c
mkdir /var/www/analytics/config

now from our local machine within our forked repo, let’s copy the global config to the server. (note: we will need to do this if we ever update our repo to match upstream updates.)

1
scp config/global.ini.php root@<your-dokku-domain.tld>:/var/www/analytics/config

next, change the permissions of our new directories and file.

1
2
chown -R nobody:nogroup /var/www/analytics/
chmod -R 0755 /var/www/analytics/

then, we configure our app to use our new directories:

1
2
dokku docker-options:add analytics "-v /var/www/analytics/tmp:/app/tmp"
dokku docker-options:add analytics "-v /var/www/analytics/config:/app/config"

restart!

1
dokku ps:restart analytics

weee, our app should be up! browse to http://analytics.<your-dokku-domain.tld>. :)

you should be presented with a Piwik welcome screen.

Piwik welcome screen

Piwik will perform a system check to make sure all is well. here’s what i got.

First half of Piwik system check
Second half of Piwik system check

next you will stumble upon a form asking for the database.

let’s create a database! on the server:

1
2
dokku mariadb:start
dokku mariadb:create analytics

after it creates and starts a database container, let’s fetch the generated credentials so we can input them into the form.

1
dokku config analytics

output of env

“Database Host” is <DB_HOST>:<DB_PORT>

enter the details and you’ll be on your way. wooo, analytics!

at this point everything should work, until you ever rebuild your app, in which case the database configuration might change without Piwik knowing about it. to solve this issue, edit /var/www/analytics/config/config.ini.php and replace the corresponding lines:

1
2
3
4
5
[database]
host = ${DB_HOST}
username = ${DB_USER}
password = ${DB_PASS}
dbname = ${DB_NAME}

sweet as. :)

another thing we can do, recommended by Immortalin, is to use the database to store session files. in config/config.ini.php, under the [General] section (add this section if it doesn’t already exist):

1
session_save_handler = dbtable

again, this guide is a little rough around the edges, i can’t guarantee that everything works. if it does or does not, message me! <3

avatar

life as a hippie hacker

patterns of consciousness in a sea of matter