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.
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:
1mkdir matomo-docker
2cd matomo-docker
docker-compose.yml
FileCreate 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:
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
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}
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.
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: