How to self-host Plausible using Docker

Plausible Analytics is a lightweight, open-source alternative to Google Analytics that respects user privacy. This guide will walk you through setting up your own Plausible Analytics instance on an Ubuntu 24.04 server.

Benefits of self-hosting Plausible:

  • Complete data ownership
  • No third-party cookies
  • GDPR compliant
  • Lightweight (~1KB script)
  • No need for consent banners
  • Customizable and extensible

Prerequisites

  • A cloud server running Ubuntu 24.04 LTS (DigitalOcean, Linode, AWS, etc.)
  • A domain name pointed to your server
  • Basic familiarity with terminal commands
  • SSH access to your server
  • Ports 80 and 443 open on your firewall

Step 1: Initial Server Setup

Start with a fresh Ubuntu 24.04 server installation:

Bash
# Update your system
sudo apt update && sudo apt upgrade -y

# Set up a non-root user with sudo privileges (if not already done)
sudo adduser plausible
sudo usermod -aG sudo plausible

# Install essential packages
sudo apt install -y curl git ufw

Configure the Firewall

Bash
# Enable UFW and allow SSH, HTTP, and HTTPS
sudo ufw allow OpenSSH
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable

Step 2: Install Docker and Docker Compose

Plausible is easiest to deploy using Docker:

Bash
# Install Docker
sudo apt install -y apt-transport-https ca-certificates software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io

# Install Docker Compose
sudo apt install -y docker-compose-plugin

# Add your user to the docker group
sudo usermod -aG docker $USER

# Apply the group changes to current session
newgrp docker

Step 3: Set Up Plausible Analytics

Create a Directory for Plausible

Bash
mkdir -p ~/plausible
cd ~/plausible

Create the Docker Compose File

Create a docker-compose.yml file:

Bash
nano docker-compose.yml

Add the following content:

Bash
version: '3.3'
services:
  mail:
    image: bytemark/smtp
    restart: always

  plausible_db:
    image: postgres:14
    restart: always
    volumes:
      - db-data:/var/lib/postgresql/data
    environment:
      - POSTGRES_PASSWORD=postgres

  plausible_events_db:
    image: clickhouse/clickhouse-server:23.3.7.5-alpine
    restart: always
    volumes:
      - event-data:/var/lib/clickhouse
      - ./clickhouse/clickhouse-config.xml:/etc/clickhouse-server/config.d/logging.xml:ro
      - ./clickhouse/clickhouse-user-config.xml:/etc/clickhouse-server/users.d/logging.xml:ro
    ulimits:
      nofile:
        soft: 262144
        hard: 262144

  plausible:
    image: plausible/analytics:latest
    restart: always
    command: sh -c "sleep 10 && /entrypoint.sh db createdb && /entrypoint.sh db migrate && /entrypoint.sh db init-admin && /entrypoint.sh run"
    depends_on:
      - plausible_db
      - plausible_events_db
      - mail
    ports:
      - "8000:8000"
    environment:
      - BASE_URL=https://your-domain.com # Set your domain
      - SECRET_KEY_BASE=your-secret-key-here # Set your secret key 
      - DISABLE_REGISTRATION=false # Set to true after setting up your admin account
      - DISABLE_SUBSCRIPTION=true
      - ADMIN_USER_EMAIL=your-email@example.com
      - ADMIN_USER_NAME=adminuser
      - ADMIN_USER_PWD=your-secure-password
      - DATABASE_URL=postgres://postgres:postgres@plausible_db:5432/plausible
      - CLICKHOUSE_DATABASE_URL=http://plausible_events_db:8123/plausible

volumes:
  db-data:
    driver: local
  event-data:
    driver: local

Create ClickHouse Configuration

First, create the directories:

Bash
mkdir -p ~/plausible/clickhouse

Create the ClickHouse config file:

Bash
nano ~/plausible/clickhouse/clickhouse-config.xml

Add:

Bash
<yandex>
    <logger>
        <level>warning</level>
        <console>true</console>
    </logger>
    <query_log>
        <database>system</database>
        <table>query_log</table>
    </query_log>
</yandex>

Create the ClickHouse user config:

Bash
nano ~/plausible/clickhouse/clickhouse-user-config.xml

Add:

Bash
<yandex>
    <profiles>
        <default>
            <log_queries>0</log_queries>
        </default>
    </profiles>
</yandex>

Customize Your Configuration

Edit the docker-compose.yml file to replace:

  1. your-domain.com with your actual domain
  2. your-email@example.com with your email
  3. your-secure-password with a strong password
  4. Generate a secret key by running:
Bash
openssl rand -base64 64 | tr -d '\n' ; echo

And replace your-secret-key-here with the generated key.

Step 4: Launch Plausible

Start the containers:

Bash
cd ~/plausible
docker compose up -d

Check the logs to ensure everything is working:

Bash
docker compose logs -f

At this point, Plausible should be running on port 8000.

Step 5: Set Up a Reverse Proxy with Nginx

Install Nginx:

Bash
sudo apt install -y nginx

Install Certbot for SSL:

Bash
sudo apt install -y certbot python3-certbot-nginx

Create an Nginx configuration for Plausible:

Bash
sudo nano /etc/nginx/sites-available/plausible

Add:

Bash
server {
    listen 80;
    server_name your-domain.com;
    
    location / {
        proxy_pass http://localhost:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Replace your-domain.com with your actual domain.

Enable the site:

Bash
sudo ln -s /etc/nginx/sites-available/plausible /etc/nginx/sites-enabled/
sudo nginx -t  # Check configuration
sudo systemctl restart nginx

Step 6: Set Up SSL with Certbot

Bash
sudo certbot --nginx -d your-domain.com

Follow the prompts to complete the SSL setup.

Step 7: Final Plausible Configuration

  1. Visit https://your-domain.com in your browser.
  2. Log in with the email and password you set in the Docker Compose file.
  3. After logging in, you can create your first site by entering the domain name you want to track.

Step 8: Adding the Tracking Script to Your Website

After setting up your first site, Plausible will provide you with a JavaScript snippet to add to your website’s HTML. It will look something like:

Bash
<script defer data-domain="yourdomain.com" src="https://your-domain.com/js/script.js"></script>

Add this script to the <head> section of all pages you want to track.

Step 9: Securing Your Plausible Instance

Once you’ve created your account, you should disable new registrations to prevent unauthorized users from creating accounts on your instance:

  1. Edit your docker-compose.yml file: nano ~/plausible/docker-compose.yml
  2. Change DISABLE_REGISTRATION=false to DISABLE_REGISTRATION=true.
  3. Restart the containers: cd ~/plausible docker compose down docker compose up -d

Step 10: Automatic Updates

Create a script to update Plausible:

Bash
nano ~/update_plausible.sh

Add:

Bash
#!/bin/bash
cd ~/plausible
docker compose pull
docker compose down
docker compose up -d

Make it executable:

Bash
chmod +x ~/update_plausible.sh

Add a cron job to update weekly:

Bash
crontab -e

Add:

Bash
0 2 * * 0 ~/update_plausible.sh >> ~/plausible_updates.log 2>&1

This will update Plausible every Sunday at 2:00 AM.

Troubleshooting

Issue: Can’t access Plausible after setup

  • Check if the containers are running: docker compose ps
  • Check container logs: docker compose logs plausible
  • Verify Nginx configuration: sudo nginx -t
  • Check firewall settings: sudo ufw status

Issue: Database connection errors

  • Ensure the PostgreSQL container is running: docker compose ps plausible_db
  • Check PostgreSQL logs: docker compose logs plausible_db

Issue: ClickHouse errors

  • Verify ClickHouse is running: docker compose ps plausible_events_db
  • Check ClickHouse logs: docker compose logs plausible_events_db

Backup Strategy

Create a backup script:

Bash
nano ~/backup_plausible.sh

Add:

Bash
#!/bin/bash
DATE=$(date +%Y%m%d)
BACKUP_DIR=~/plausible_backups

# Create backup directory if it doesn't exist
mkdir -p $BACKUP_DIR

# Backup PostgreSQL database
cd ~/plausible
docker compose exec -T plausible_db pg_dump -U postgres plausible > $BACKUP_DIR/plausible_postgres_$DATE.sql

# Backup ClickHouse data (this is a simplified approach)
cd ~/plausible
docker compose stop plausible_events_db
tar -czf $BACKUP_DIR/plausible_clickhouse_$DATE.tar.gz -C ~/plausible/event-data .
docker compose start plausible_events_db

# Backup Docker Compose configuration
cp ~/plausible/docker-compose.yml $BACKUP_DIR/docker-compose_$DATE.yml

# Remove backups older than 30 days
find $BACKUP_DIR -type f -mtime +30 -delete

Make it executable:

Bash
chmod +x ~/backup_plausible.sh

Set up a cron job for daily backups:

Bash
crontab -e

Add:

Bash
0 1 * * * ~/backup_plausible.sh >> ~/plausible_backups.log 2>&1

Conclusion

Congratulations! You’ve successfully set up a self-hosted Plausible Analytics instance on your Ubuntu 24.04 server. Your analytics data is now completely under your control, and you’re tracking visitors without compromising their privacy.

Remember to:

  • Keep your server updated
  • Regularly back up your data
  • Monitor server resource usage
  • Check for Plausible updates

For further information, visit the Plausible documentation.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *