Here are the steps to set up Matomo with Docker Compose and MySQL:
Install Docker Compose: If you haven’t installed Docker Compose already, install it now.
Create a Docker Compose file: Create a new file called docker-compose.yml in a directory of your choice, and add the following contents:
version: '3'
services:
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: your_mysql_password
MYSQL_DATABASE: matomo
MYSQL_USER: matomo_user
MYSQL_PASSWORD: matomo_password
volumes:
- db_data:/var/lib/mysql
matomo:
image: matomo
ports:
- "8080:80"
restart: always
environment:
MATOMO_DATABASE_HOST: db
MATOMO_DATABASE_PORT: 3306
MATOMO_DATABASE_USER: matomo_user
MATOMO_DATABASE_PASSWORD: matomo_password
MATOMO_DATABASE_NAME: matomo
MATOMO_DATABASE_TABLES_PREFIX: matomo_
MATOMO_PROXY_CLIENT_HEADERS: HTTP_X_FORWARDED_FOR
MATOMO_LOG_LEVEL: INFO
MATOMO_SUPERUSER_LOGIN: admin
MATOMO_SUPERUSER_PASSWORD: your_admin_password
depends_on:
- db
volumes:
db_data:
This file defines two services: db and matomo. The db service is a MySQL database container that will store Matomo’s data, and the matomo service is the Matomo web application container. The ports section maps the container’s port 80 to the host’s port 8080, so you can access Matomo through http://localhost:8080.
docker-compose.yml file, and run the following command:docker-compose up -d
This will start the two containers in the background. If this is the first time you’re running the command, Docker Compose will download the mysql and matomo images from the Docker Hub.
http://localhost:8080 (or the port you specified in the docker-compose.yml file). Follow the instructions to complete the Matomo installation. When asked for the database settings, use the following values:dbmatomo_usermatomo_passwordmatomomatomo_And that’s it! You should now have Matomo up and running with Docker Compose and MySQL.
If you already have a MySQL server running outside of Docker that you want to use with Matomo, you can modify the docker-compose.yml file to use your existing MySQL server instead of creating a new MySQL container. Here’s how:
Open the docker-compose.yml file and remove the db service definition.
Modify the matomo service definition to include the necessary environment variables to connect to your existing MySQL server. Replace the MATOMO_DATABASE_HOST, MATOMO_DATABASE_USER, MATOMO_DATABASE_PASSWORD, and MATOMO_DATABASE_NAME environment variables with the appropriate values for your MySQL server. For example:
version: '3'
services:
matomo:
image: matomo
ports:
- "8080:80"
restart: always
environment:
MATOMO_DATABASE_HOST: your_mysql_host
MATOMO_DATABASE_PORT: 3306
MATOMO_DATABASE_USER: your_mysql_user
MATOMO_DATABASE_PASSWORD: your_mysql_password
MATOMO_DATABASE_NAME: your_matomo_database_name
MATOMO_DATABASE_TABLES_PREFIX: matomo_
MATOMO_PROXY_CLIENT_HEADERS: HTTP_X_FORWARDED_FOR
MATOMO_LOG_LEVEL: INFO
MATOMO_SUPERUSER_LOGIN: admin
MATOMO_SUPERUSER_PASSWORD: your_admin_password
Save the modified docker-compose.yml file.
Open a terminal, navigate to the directory where you saved the docker-compose.yml file, and run the following command:
docker-compose up -d
This will start the Matomo container with the specified environment variables to connect to your existing MySQL server.
http://localhost:8080 (or the port you specified in the docker-compose.yml file). Follow the instructions to complete the Matomo installation. When asked for the database settings, use the same values you used for the MATOMO_DATABASE_* environment variables in the docker-compose.yml file.That’s it! You should now have Matomo up and running with Docker Compose and your existing MySQL server.
Running Matomo with Docker behind a reverse proxy outside of Docker is a common use case. Here’s how you can set it up:
docker-compose.yml file to include the VIRTUAL_HOST environment variable for the matomo service. This tells Docker that the container should be accessible through the specified hostname (e.g., matomo.example.com) that will be used by the reverse proxy.version: '3'
services:
matomo:
image: matomo
ports:
- "80"
restart: always
environment:
MATOMO_DATABASE_HOST: your_mysql_host
MATOMO_DATABASE_PORT: 3306
MATOMO_DATABASE_USER: your_mysql_user
MATOMO_DATABASE_PASSWORD: your_mysql_password
MATOMO_DATABASE_NAME: your_matomo_database_name
MATOMO_DATABASE_TABLES_PREFIX: matomo_
MATOMO_PROXY_CLIENT_HEADERS: HTTP_X_FORWARDED_FOR
MATOMO_LOG_LEVEL: INFO
MATOMO_SUPERUSER_LOGIN: admin
MATOMO_SUPERUSER_PASSWORD: your_admin_password
VIRTUAL_HOST: matomo.example.com
Save the modified docker-compose.yml file.
Configure your reverse proxy to forward requests to matomo.example.com to the IP address of the Docker host running the matomo container. The specific configuration depends on the reverse proxy software you’re using. For example, for Nginx, you can create a new server block in the nginx.conf file with the following configuration:
server {
listen 80;
server_name matomo.example.com;
location / {
proxy_pass http://docker_host_ip:80;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Replace docker_host_ip with the IP address of the Docker host running the matomo container.
Reload the Nginx configuration by running sudo service nginx reload (or equivalent command for your reverse proxy software).
Test the setup by visiting http://matomo.example.com in a web browser. Matomo should be accessible through the reverse proxy now.
That’s it! You should now have Matomo running with Docker behind a reverse proxy outside of Docker.
If you’re using Apache2 as your reverse proxy, you can use the mod_proxy module to forward requests to the Matomo container running with Docker. Here’s how you can set it up:
docker-compose.yml file to include the VIRTUAL_HOST environment variable for the matomo service, as described in the previous answer.version: '3'
services:
matomo:
image: matomo
ports:
- "80"
restart: always
environment:
MATOMO_DATABASE_HOST: your_mysql_host
MATOMO_DATABASE_PORT: 3306
MATOMO_DATABASE_USER: your_mysql_user
MATOMO_DATABASE_PASSWORD: your_mysql_password
MATOMO_DATABASE_NAME: your_matomo_database_name
MATOMO_DATABASE_TABLES_PREFIX: matomo_
MATOMO_PROXY_CLIENT_HEADERS: HTTP_X_FORWARDED_FOR
MATOMO_LOG_LEVEL: INFO
MATOMO_SUPERUSER_LOGIN: admin
MATOMO_SUPERUSER_PASSWORD: your_admin_password
VIRTUAL_HOST: matomo.example.com
Save the modified docker-compose.yml file.
Enable the proxy and proxy_http Apache2 modules by running the following command:
sudo a2enmod proxy proxy_http
matomo.linux-server-admin.com in this example) and forwards requests to the Matomo container running with Docker. Here’s an example configuration file:<VirtualHost *:80>
ServerName matomo.linux-server-admin.com
ProxyPreserveHost On
ProxyPass / http://docker_host_ip:80/
ProxyPassReverse / http://docker_host_ip:80/
</VirtualHost>
Replace docker_host_ip with the IP address of the Docker host running the matomo container.
sudo a2ensite matomo.linux-server-admin.com.conf
sudo service apache2 reload
http://matomo.linux-server-admin.com in a web browser. Matomo should be accessible through the Apache2 reverse proxy now.That’s it! You should now have Matomo running with Docker behind an Apache2 reverse proxy.
If Matomo is not starting up with the provided docker-compose.yml, there could be several reasons why. Here are some common issues and their solutions:
If the Matomo container exits immediately after starting, it’s likely that there’s an issue with the database connection. Make sure that the MATOMO_DATABASE_HOST, MATOMO_DATABASE_USER, MATOMO_DATABASE_PASSWORD, and MATOMO_DATABASE_NAME environment variables are set correctly and match the MySQL database you’re using.
Also, check the MySQL server logs to see if there are any errors related to the Matomo database connection.
If the Matomo container fails to start due to a port conflict, make sure that the ports section in the docker-compose.yml file is set correctly. By default, the matomo service is configured to listen on port 80, so make sure that no other service is already using that port.
You can also change the port mapping to use a different port if port 80 is already in use. For example, you can change the port mapping to 8080:80 to use port 8080 instead.
If the Matomo container fails to start due to a missing volume, make sure that the volumes section in the docker-compose.yml file is set correctly.
By default, the matomo service is configured to mount a volume named matomo_data to /var/www/html. Make sure that the volume exists and that the path is correct.
You can also change the volume mapping to use a different volume or path if necessary.
If the Matomo container fails to start due to permission issues, make sure that the user and group settings in the docker-compose.yml file are set correctly.
By default, the matomo service is configured to run as user www-data and group www-data. Make sure that these settings match the user and group that owns the matomo_data volume.
You can also change the user and group settings to use a different user or group if necessary.
If you’re still having issues, check the logs for the matomo container by running the following command:
docker-compose logs matomo
This should give you more information about why the container is failing to start.
Feel free to contact us. Find all contact information on our contact page.