Hosting Matomo with Docker Compose: A Step-by-Step Guide
Posted on May 30, 2024 • 4 min read • 683 wordsIn 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.
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.
First, create a directory for your Matomo project and navigate into it:
mkdir matomo-docker
cd matomo-docker
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:
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
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;
}
}
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.
After a few minutes, you can access Matomo at http://<your-domain>:8085
. Follow the on-screen instructions to complete the setup.
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: