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
We’ve partnered with UpCloud to offer you €25 in free credits—get started with cloud servers and storage for self-hosting and more!
Step 1: Initial Server Setup
Start with a fresh Ubuntu 24.04 server installation:
# 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
# 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:
# 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
mkdir -p ~/plausible
cd ~/plausible
Create the Docker Compose File
Create a docker-compose.yml
file:
nano docker-compose.yml
Add the following content:
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:
mkdir -p ~/plausible/clickhouse
Create the ClickHouse config file:
nano ~/plausible/clickhouse/clickhouse-config.xml
Add:
<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:
nano ~/plausible/clickhouse/clickhouse-user-config.xml
Add:
<yandex>
<profiles>
<default>
<log_queries>0</log_queries>
</default>
</profiles>
</yandex>
Customize Your Configuration
Edit the docker-compose.yml
file to replace:
your-domain.com
with your actual domainyour-email@example.com
with your emailyour-secure-password
with a strong password- Generate a secret key by running:
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:
cd ~/plausible
docker compose up -d
Check the logs to ensure everything is working:
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:
sudo apt install -y nginx
Install Certbot for SSL:
sudo apt install -y certbot python3-certbot-nginx
Create an Nginx configuration for Plausible:
sudo nano /etc/nginx/sites-available/plausible
Add:
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:
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
sudo certbot --nginx -d your-domain.com
Follow the prompts to complete the SSL setup.
Step 7: Final Plausible Configuration
- Visit
https://your-domain.com
in your browser. - Log in with the email and password you set in the Docker Compose file.
- 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:
<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:
- Edit your docker-compose.yml file:
nano ~/plausible/docker-compose.yml
- Change
DISABLE_REGISTRATION=false
toDISABLE_REGISTRATION=true
. - Restart the containers:
cd ~/plausible docker compose down docker compose up -d
Step 10: Automatic Updates
Create a script to update Plausible:
nano ~/update_plausible.sh
Add:
#!/bin/bash
cd ~/plausible
docker compose pull
docker compose down
docker compose up -d
Make it executable:
chmod +x ~/update_plausible.sh
Add a cron job to update weekly:
crontab -e
Add:
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:
nano ~/backup_plausible.sh
Add:
#!/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:
chmod +x ~/backup_plausible.sh
Set up a cron job for daily backups:
crontab -e
Add:
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.
Leave a Reply