The Nextlevel Blog logo
  • About 
  • Research 
  • Tags 
  • Blog 
  1.   Blog
  1. Home
  2. Blog
  3. Hosting Matomo with Docker Compose: A Step-by-Step Guide

Hosting Matomo with Docker Compose: A Step-by-Step Guide

Posted on May 30, 2024 • 4 min read • 683 words
Selfhosted   Privacy   Web  
Selfhosted   Privacy   Web  
Share via
The Nextlevel Blog
Link copied to clipboard

On this page
Why Host Matomo Yourself?   Pros:   Cons:   Prerequisites   Step-by-Step Setup   Step 1: Create the Project Directory   Step 2: Create the docker-compose.yml File   Step 3: Create the Environment File   Step 4: Create the Nginx Configuration File   Step 5: Start the Containers   Step 6: Access Matomo   Further Reading  
Hosting Matomo with Docker Compose: A Step-by-Step Guide

In this tutorial, we will walk you through the process of setting up Matomo, an open-source web analytics platform, using Docker Compose. This setup will include a MariaDB database, the Matomo application, and an Nginx web server to serve as a reverse proxy.

Why Host Matomo Yourself?  

Pros:  

  • Data Ownership: When you host Matomo yourself, you have complete control over your analytics data. This is crucial for businesses that prioritize data privacy and security, as it ensures that sensitive information is not shared with third-party services.
  • Customization: Self-hosting allows you to customize the setup to fit your specific needs. You can modify the configuration, add plugins, and integrate Matomo with other tools and services in your infrastructure.
  • Cost: While there are costs associated with self-hosting (such as server costs), it can be more cost-effective in the long run compared to subscription-based third-party analytics services. This is especially true for high-traffic websites where third-party services can become expensive.
  • Compliance: Hosting your own analytics platform can help with compliance requirements, such as GDPR, by ensuring that data is stored and processed in accordance with local regulations.

Cons:  

  • Maintenance: Self-hosting requires regular updates and maintenance to ensure the system is secure and running smoothly. This includes updating the Matomo software, the underlying database, and the web server.
  • Complexity: The initial setup can be complex, especially for beginners. It involves configuring multiple services and ensuring they work together seamlessly.
  • Resource Usage: Running Matomo on your own server consumes resources such as CPU, memory, and storage. This might be a limitation if you have a small server or limited resources.

Prerequisites  

Before you start, ensure you have Docker and Docker Compose installed on your system. You can follow the official Docker installation guides for your operating system.

Step-by-Step Setup  

Step 1: Create the Project Directory  

First, create a directory for your Matomo project and navigate into it:

mkdir matomo-docker
cd matomo-docker

Step 2: Create the docker-compose.yml File  

Create a docker-compose.yml file with the following content:

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:

Step 3: Create the Environment File  

Create a db.env file with the following content:

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 4: Create the Nginx Configuration File  

Create a matomo.conf file with the following content:

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 5: Start the Containers  

Run the following command to start the containers:

docker-compose up -d

This command will pull the necessary images and start the containers in the background.

Step 6: Access Matomo  

After a few minutes, you can access Matomo at http://<your-domain>:8085. Follow the on-screen instructions to complete the setup.

Further Reading  

  • Matomo Official Documentation
  • Docker Compose Documentation
  • MariaDB Official Documentation

By following these steps, you should have a fully functional Matomo instance running on Docker. This setup provides a scalable and flexible solution for web analytics, giving you full control over your data and analytics environment.

More information:

  • https://gist.github.com/davidalves1/ac1a7282f65b7cd79da612a1f4857537
  • https://mariadb.com/kb/en/setting-up-a-lamp-stack-with-docker-compose/
  • https://www.beekeeperstudio.io/blog/how-to-use-mariadb-with-docker
  • https://www.restack.io/docs/matomo-knowledge-matomo-docker-compose-guide
  • https://data-marketing-school.com/en/blog/matomo/docker/
  • https://jiv-e.hashnode.dev/how-to-run-matomo-in-docker-locally
  • https://geshan.com.np/blog/2024/03/nginx-docker-compose/
  • https://dev.to/aminnairi/quick-web-server-with-nginx-on-docker-compose-43ol
  • https://gcore.com/learning/reverse-proxy-with-docker-compose/
  • https://docs.docker.com/compose/environment-variables/envvars-precedence/
  • https://docs.docker.com/compose/environment-variables/envvars/
  • https://stackoverflow.com/questions/29377853/how-can-i-use-environment-variables-in-docker-compose
  • https://www.restack.io/docs/matomo-knowledge-matomo-docker-nginx-integration
  • https://forum.matomo.org/t/docker-matomo-and-nginx-host/33846
 Dockerizing Fabric - A Comprehensive AI Augmentation Setup
Hosting Home Assistant with Docker Compose: A Step-by-Step Guide 
On this page:
Why Host Matomo Yourself?   Pros:   Cons:   Prerequisites   Step-by-Step Setup   Step 1: Create the Project Directory   Step 2: Create the docker-compose.yml File   Step 3: Create the Environment File   Step 4: Create the Nginx Configuration File   Step 5: Start the Containers   Step 6: Access Matomo   Further Reading  
Nextlevel v/Peter Schneider

I work on everything cyber security and development, CVR: 42051993, mail: info@nextlevel-blog.de, phone: 60 59 76 35

Copyright © 2025 Peter Schneider. | Powered by Hinode.
The Nextlevel Blog
Code copied to clipboard