Matomo Self-Hosting: A Privacy-Friendly Alternative to Google Analytics in Docker

🎧 Listen to the 60-Second Audio Recap:

Why Self-Host Matomo?

When you self-host Matomo, you get a privacy-friendly alternative to Google Analytics that keeps all data on your own server. Matomo is an open-source web analytics platform. It allows you to keep all your analytics data on your own server, enhancing privacy and ensuring compliance with regulations like GDPR. Following the Schrems II ruling, which raised legal concerns about using Google Analytics in the EU, Matomo offers a solution that emphasizes data ownership and privacy compliance.

Prerequisites & Minimal Hardware

Required Software

  • Docker and Docker Compose with Portainer. Refer to our Docker + Portainer on Proxmox LXC guide for setup.
  • A domain or subdomain for your Matomo instance (e.g., stats.yourdomain.com) with HTTPS enabled.
  • A reverse proxy or Cloudflare Tunnel for managing TLS certificates.

Hardware Requirements

A basic server capable of running Docker containers is sufficient for hosting Matomo. Ensure your server meets the minimal specifications for running Docker efficiently.

Method 1: The Quick Start (Beginner Friendly)

Setting Up Matomo Container

We’ll start with a Docker Compose setup that includes Matomo and its required MariaDB database. Matomo has no built-in database, so the database container is mandatory.

version: '3.7'
services:
  db:
    image: mariadb:11
    container_name: matomo_db
    restart: unless-stopped
    command: --max-allowed-packet=64MB
    environment:
      MARIADB_ROOT_PASSWORD: ChooseAStrongRootPassword
      MARIADB_DATABASE: matomo
      MARIADB_USER: matomo
      MARIADB_PASSWORD: ChooseAStrongPassword
    volumes:
      - /mnt/snelle_data/App_Data/matomo_db:/var/lib/mysql

  matomo:
    image: matomo:latest
    container_name: matomo
    restart: unless-stopped
    depends_on:
      - db
    ports:
      - "8080:80"
    volumes:
      - /mnt/snelle_data/App_Data/matomo:/var/www/html

Accessing Matomo

  1. Open your web browser and navigate to your Matomo instance using your domain (e.g., http://stats.yourdomain.com:8080).
  2. Complete the graphical web installer. When it asks for database details, enter host db, username matomo, and the password you set in the compose file.

Adding Tracking Code

  1. Once Matomo is set up, copy the tracking code provided in the dashboard.
  2. Insert this tracking code into the HTML of the website you wish to track.
  3. Verify the tracking setup by visiting the site and checking the Matomo dashboard for real-time visitor logs.

Method 2: The Pro Setup (Advanced Config/Docker Compose)

Deploying Multi-Container Stack

For a more advanced setup, deploy Matomo with a separate MariaDB container using the following Docker Compose configuration:

version: '3.7'
services:
  db:
    image: mariadb:11
    container_name: matomo_db
    restart: unless-stopped
    command: --max-allowed-packet=64MB
    environment:
      MARIADB_ROOT_PASSWORD: ChooseAStrongRootPassword
      MARIADB_DATABASE: matomo
      MARIADB_USER: matomo
      MARIADB_PASSWORD: ChooseAStrongPassword
    volumes:
      - /mnt/snelle_data/App_Data/matomo_db:/var/lib/mysql

  matomo:
    image: matomo:latest
    container_name: matomo
    restart: unless-stopped
    depends_on:
      - db
    environment:
      MATOMO_DATABASE_HOST: db
      MATOMO_DATABASE_ADAPTER: mysql
      MATOMO_DATABASE_USERNAME: matomo
      MATOMO_DATABASE_PASSWORD: ChooseAStrongPassword
      MATOMO_DATABASE_DBNAME: matomo
    volumes:
      - /mnt/snelle_data/App_Data/matomo:/var/www/html
    ports:
      - "8080:80"

Configuring Database

  1. Access the Matomo web installer and input the database details: host as ‘db’, user as ‘matomo’, and the password you set in the Docker Compose file.
  2. Create your super-user account and add the first website to track.

Proxy Header Configuration

To track real visitor IPs, configure Matomo to recognize the correct proxy headers. Add the following to config/config.ini.php under [General]:

[General]
assume_secure_protocol = 1
proxy_client_headers[] = "HTTP_X_FORWARDED_FOR"
proxy_host_headers[] = "HTTP_X_FORWARDED_HOST"

Enabling Archive Cron

  1. Disable browser-triggered archiving in Matomo under Administration.
  2. Set up a cron job for periodic archiving using the command:
docker exec -u www-data matomo php /var/www/html/console core:archive

Configuration & Validation (How to test it)

Tracking Verification

Place the tracking code on a test page, visit it, and check Matomo’s visitor log to see if your visit is logged in real-time.

Proxy Verification

Ensure that the correct visitor IPs are logged, not the proxy IPs, by checking the visitor’s location in Matomo.

Privacy Verification

Open your browser’s developer tools (Network tab) and ensure there are no requests to google-analytics.com or googletagmanager.com.

The Ugly Truth (Honesty check / Quirks)

Performance Considerations

Matomo requires more resources than a simple Google Analytics snippet. It’s a full-fledged PHP application with a database that requires maintenance, especially on high-traffic sites. Smaller sites run smoothly, but larger sites may need regular tuning, particularly with the archive cron job.

Common Pitfalls

Incorrect proxy-header configuration can result in all visitors appearing as your reverse proxy IP. Ensure your configuration is correct to maintain accurate statistics.

Troubleshooting Common Errors

Trusted Hosts Error

If you encounter a ‘trusted hosts’ error, add your domain under Administration > General Settings > Trusted Hostnames or manually in config/config.ini.php under [General]:

trusted_hosts[] = "yourdomain.com"

Proxy IP Issue

If all visitors appear with the same IP (your proxy’s), ensure the proxy headers are correctly configured as shown earlier.

Slow or Empty Reports

If reports are slow or empty, disable browser-triggered archiving and ensure the cron job for archiving is active.

Conclusion & Next Steps

Summary

By following this guide, you have set up a privacy-friendly web analytics platform with Matomo, maintaining full control over your data without relying on Google.

Next Steps

  • Activate IP anonymization and cookieless tracking to potentially eliminate the need for a cookie banner.
  • If you use WordPress, consider installing the official Matomo plugin for integrated analytics within your dashboard.

Matomo is one piece of a Google-free stack. To replace more Google services on your own server, see our guides on Immich (a Google Photos alternative) and Nextcloud (a Google Drive alternative), or revisit the Docker + Portainer on Proxmox LXC foundation guide.