Home » Blog » How to self-host Matomo Analytics using Docker

How to self-host Matomo Analytics using Docker

Matomo (aka. Matomo Analytics, formerly Piwik) is a free and open-source web analytics software that can be self-hosted. This guide will walk you through the steps to deploy Matomo Analytics using Docker.

What you need

You need an up-to-date version of Docker and Docker Compose, installed on your web-server (e.g., running on Ubuntu 20.04) and a domain, pointed to your server’s IP address.

  • Docker installed on your server
  • A domain name pointed to your server’s IP address

Note: We’ll use Hetzner Cloud as our hosting provider, but you can use any other provider as well.

Deploy Matomo Analytics using docker-compose

Step 1: Install Docker on your web-server or virtual machine

Simply copy and paste the code below into your cmd/terminal and follow the instructions.

Bash
apt-get update
apt-get upgrade
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
apt install docker-compose-plugin

Step 2: Create the directory for your new Matomo project

Next, simply add a directory for your project to be installed in.

Bash
mkdir matomo-docker
cd matomo-docker

Step 3: Set up and configure the required docker-compose.yml file

Now, create a new docker-compose.yml file and tweak its values to your liking.

Create the file

Bash
nano docker-compose.yml

Paste in the required content to set up Matomo Analytics

Please note that you need to change the MYSQL_ROOT_PASSWORD as well as <your-domain> to your custom values (your desired password and your own domain respectively).

Bash
version: "3"

services:
  db:
    image: mariadb:lts
    command: --max-allowed-packet=64MB
    restart: always
    volumes:
      - db:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=rootpw
    env_file:
      - ./db.env

  app:
    image: matomo:fpm-alpine
    restart: always
    links:
      - db
    volumes:
      - matomo:/var/www/html
    environment:
      - MATOMO_DATABASE_HOST=db
      - PHP_MEMORY_LIMIT=2048M
    env_file:
      - ./db.env

  web:
    image: nginx:alpine
    restart: always
    volumes:
      - matomo:/var/www/html:ro
      - ./matomo.conf:/etc/nginx/conf.d/default.conf:ro
    healthcheck:
      test: ["CMD", "curl", "-f", "http://<your-domain>:8085"]
      interval: 1m
      timeout: 10s
      retries: 3
      start_period: 40s
    ports:
      - 8085:80

volumes:
  db:
  matomo:

Note: You can save file changes (write lines) by pressing Ctrl + O and confirm by pressing Enter.

Step 4: Set up and configure up your Matomo Analytics environment variables

Create and configure a new db.env file.

Create the environment file

Bash
nano db.env

Paste the required content and customize the values

Now, paste in the following content and customize (at least) the MYSQL_PASSWORD and MATOMO_DATABASE_PASSWORD values.

Bash
MYSQL_PASSWORD=matomo
MYSQL_DATABASE=matomo
MYSQL_USER=matomo
MATOMO_DATABASE_ADAPTER=mysql
MATOMO_DATABASE_TABLES_PREFIX=matomo_
MATOMO_DATABASE_USERNAME=matomo
MATOMO_DATABASE_PASSWORD=matomo
MATOMO_DATABASE_DBNAME=matomo

Step 5: Set up Nginx for Matomo Analytics

You need to configure the matomo.conf file of your Matomo Analytics installation.

Create the Nginx configuration file

Bash
nano matomo.conf

Copy and paste the following content

Bash
upstream php-handler {
  server app:9000;
}

server {
  listen 80;
  client_header_timeout 1000000;
  client_body_timeout 1000000;
  send_timeout 1000000;
  fastcgi_read_timeout 1000000;

  add_header Referrer-Policy origin;
  root /var/www/html;
  index index.php;
  try_files $uri $uri/ =404;

  location ~ ^/(index|matomo|piwik|js/index|plugins/HeatmapSessionRecording/configs).php {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    try_files $fastcgi_script_name =404;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    fastcgi_param HTTP_PROXY "";
    fastcgi_pass php-handler;
  }

  location ~* ^.+\.php$ {
    deny all;
    return 403;
  }

  location ~ /(config|tmp|core|lang) {
    deny all;
    return 403;
  }

  location ~ /\.ht {
    deny all;
    return 403;
  }

  location ~ js/container_.*_preview\.js$ {
    expires off;
    add_header Cache-Control 'private, no-cache, no-store';
  }

  location ~ \.(gif|ico|jpg|png|svg|js|css|htm|html|mp3|mp4|wav|ogg|avi|ttf|eot|woff|woff2|json)$ {
    allow all;
    expires 1h;
    add_header Pragma public;
    add_header Cache-Control "public";
  }

  location ~ /(libs|vendor|plugins|misc/user) {
    deny all;
    return 403;
  }

  location ~/(.*\.md|LEGALNOTICE|LICENSE) {
    default_type text/plain;
  }
}

Step 6: Start the Docker containers

Finally, you can run the following command to start the Docker containers.

Bash
docker-compose up -d

Congrats, you’re all set! ✨

FAQ

What is Matomo?

Matomo is an open-source web analytics platform that allows businesses to track and analyze their website’s traffic, user behavior, and performance. It is a privacy-friendly alternative to tools like Google Analytics, offering complete data ownership and GDPR compliance.

Is Matomo free to use?

Yes, Matomo is free and open-source when self-hosted. Additionally, a cloud-hosted version is available with various pricing plans, catering to users who prefer a managed solution.

What are the system requirements for self-hosting Matomo?

Minimum requirements:
2GB RAM
1 CPU core
10GB storage (depending on data size)
PHP 7.4 or newer
MySQL 5.7+ or MariaDB 10.1+

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *