How to Self-Host Matomo with Caddy Server and Docker

One of the wonderful things about the modern web is that you can host your own services with relative ease, taking control of your own data and customizing things to suit your needs. Today, we'll be discussing how to self-host Matomo, a powerful open-source analytics platform, using the Caddy Server and Docker.

1. Create Subdomain

The first step is to create a subdomain for your Matomo service. This can be anything you prefer, but for the sake of our tutorial, we'll use matomo.yourdomain.com. You'll need to create this subdomain in your domain provider's dashboard.

2. Set CNAME of Subdomain to IP of Server

After you've created your subdomain, you'll need to point it to the IP address of your server. You do this by setting a CNAME record in your domain provider's DNS settings. The CNAME record should point your Matomo subdomain to the IP address of your server.

3. Check that Subdomain Points to IP

To confirm that your CNAME record is working correctly, you can use the dig command in the terminal:

dig +short matomo.yourdomain.com

This should return your server's IP address.

4. SSH into Server

Next, you'll need to connect to your server using SSH (Secure Shell). This is a secure network protocol that allows you to control your server remotely.

ssh username@your-server-ip

5. Install Docker-Compose

Once you're connected to your server, it's time to install Docker Compose. This tool allows you to define and run multi-container Docker applications.

sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

6. Create Docker-Compose.yaml with Caddy Server, Matomo, and MariaDB Docker Image

Next, we need to create a docker-compose.yaml file that defines our services. This file will include Caddy Server, Matomo, and MariaDB.

version: "3.7" 
services: 
  caddy: 
    image: caddy:2.4.5-alpine 
    restart: unless-stopped 
    ports: 
      - "80:80" 
      - "443:443" 
    depends_on: 
      - matomo 
    volumes: 
      - ./caddy/Caddyfile:/etc/caddy/Caddyfile 
      - ./caddy/site:/srv 
      - ./caddy/caddy_data:/data 
      - ./caddy/caddy_config:/config 
 
  matomo: 
    container_name: matomo 
    image: matomo 
    environment: 
      - MATOMO_DATABASE_HOST=matomo_db 
    env_file: 
      - ./db.env 
    depends_on: 
      - matomo_db 
    restart: unless-stopped 
 
  matomo_db: 
    container_name: matomo_db 
    image: mariadb 
    command: --max-allowed-packet=64MB 
    environment: 
      - MYSQL_ROOT_PASSWORD=inventa96 
    env_file: 
      - ./db.env 
    restart: unless-stopped 
 
volumes: 
  data: 
    driver: local 
  caddy_data: 
    external: true 
  caddy_config: 

7. Create .env file

Create a .env file to store environment variables that your Docker services will use. These may include settings for Matomo, such as the database host, username, and password, as well as any Caddy-specific settings.

MYSQL_PASSWORD=youpassword
MYSQL_DATABASE=matomo
MYSQL_USER=matomo
MATOMO_DATABASE_ADAPTER=mysql
MATOMO_DATABASE_TABLES_PREFIX=matomo_
MATOMO_DATABASE_USERNAME=matomo
MATOMO_DATABASE_PASSWORD=youpassword
MATOMO_DATABASE_DBNAME=matomo
DOMAIN=matomo.yourdomain.com

Replace youpassword and matomo.yourdomain.com with your actual password and domain, respectively.

8. Create Caddyfile

The Caddyfile is a text file that configures the Caddy server. It will include settings such as the domain name (your Matomo subdomain), the location of your SSL certificate and key, and any other configuration options you need for your setup.

{
    email your@email.com
}
matomo.yourdomain.com {
    reverse_proxy matomo:80
}

Replace your@email.com and matomo.yourdomain.com with your actual email and domain, respectively.

9. Run Docker Compose

Now that all the files are ready, you can start your Docker services using Docker Compose. Run the following command in the same directory as your docker-compose.yaml file:

docker-compose up -d

10. Go to your Subdomain and Setup Matomo

Finally, navigate to matomo.yourdomain.com in your web browser to start setting up Matomo. You'll be prompted to enter the database details you specified in the .env file, after which you can proceed to set up your Matomo analytics as desired.

Congratulations, you've now successfully self-hosted your Matomo instance using Caddy Server and Docker! This setup allows you to gain deep insights into your web traffic while retaining full control and ownership over your data. Enjoy your new analytics capabilities!