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

Posted on May 30, 2024 • 4 min read • 767 words

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:

1mkdir matomo-docker
2cd matomo-docker

Step 2: Create the docker-compose.yml File

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

 1version: "3"
 2
 3services:
 4  db:
 5    image: mariadb:lts
 6    command: --max-allowed-packet=64MB
 7    restart: always
 8    volumes:
 9      - db:/var/lib/mysql
10    environment:
11      - MYSQL_ROOT_PASSWORD=rootpw
12    env_file:
13      - ./db.env
14
15  app:
16    image: matomo:fpm-alpine
17    restart: always
18    links:
19      - db
20    volumes:
21      - matomo:/var/www/html
22    environment:
23      - MATOMO_DATABASE_HOST=db
24      - PHP_MEMORY_LIMIT=2048M
25    env_file:
26      - ./db.env
27
28  web:
29    image: nginx:alpine
30    restart: always
31    volumes:
32      - matomo:/var/www/html:ro
33      - ./matomo.conf:/etc/nginx/conf.d/default.conf:ro
34    healthcheck:
35      test: ["CMD", "curl", "-f", "http://<your-domain>:8085"]
36      interval: 1m
37      timeout: 10s
38      retries: 3
39      start_period: 40s
40    ports:
41      - 8085:80
42
43volumes:
44  db:
45  matomo:

Step 3: Create the Environment File

Create a db.env file with the following content:

1MYSQL_PASSWORD=matomo
2MYSQL_DATABASE=matomo
3MYSQL_USER=matomo
4MATOMO_DATABASE_ADAPTER=mysql
5MATOMO_DATABASE_TABLES_PREFIX=matomo_
6MATOMO_DATABASE_USERNAME=matomo
7MATOMO_DATABASE_PASSWORD=matomo
8MATOMO_DATABASE_DBNAME=matomo

Step 4: Create the Nginx Configuration File

Create a matomo.conf file with the following content:

 1upstream php-handler {
 2  server app:9000;
 3}
 4
 5server {
 6  listen 80;
 7  client_header_timeout 1000000;
 8  client_body_timeout 1000000;
 9  send_timeout 1000000;
10  fastcgi_read_timeout 1000000;
11
12  add_header Referrer-Policy origin;
13  root /var/www/html;
14  index index.php;
15  try_files $uri $uri/ =404;
16
17  location ~ ^/(index|matomo|piwik|js/index|plugins/HeatmapSessionRecording/configs).php {
18    fastcgi_split_path_info ^(.+\.php)(/.+)$;
19    try_files $fastcgi_script_name =404;
20    include fastcgi_params;
21    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
22    fastcgi_param PATH_INFO $fastcgi_path_info;
23    fastcgi_param HTTP_PROXY "";
24    fastcgi_pass php-handler;
25  }
26
27  location ~* ^.+\.php$ {
28    deny all;
29    return 403;
30  }
31
32  location ~ /(config|tmp|core|lang) {
33    deny all;
34    return 403;
35  }
36
37  location ~ /\.ht {
38    deny all;
39    return 403;
40  }
41
42  location ~ js/container_.*_preview\.js$ {
43    expires off;
44    add_header Cache-Control 'private, no-cache, no-store';
45  }
46
47  location ~ \.(gif|ico|jpg|png|svg|js|css|htm|html|mp3|mp4|wav|ogg|avi|ttf|eot|woff|woff2|json)$ {
48    allow all;
49    expires 1h;
50    add_header Pragma public;
51    add_header Cache-Control "public";
52  }
53
54  location ~ /(libs|vendor|plugins|misc/user) {
55    deny all;
56    return 403;
57  }
58
59  location ~/(.*\.md|LEGALNOTICE|LICENSE) {
60    default_type text/plain;
61  }
62}

Step 5: Start the Containers

Run the following command to start the containers:

1docker-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

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:

Nextlevel v/Peter Schneider

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