Plausible Analytics is a lightweight, open-source web analytics platform. It's privacy-focused and provides a simple alternative to Google Analytics. This guide will walk you through the steps to install Plausible Analytics on an Ubuntu 24.04 server.
Prerequisites
Before starting, ensure you have the following:
- A server running Ubuntu 24.04 with root or sudo privileges.
- A domain name pointed to your server's IP address (e.g., analytics.example.com).
- Docker and Docker Compose installed on your server.
- Basic knowledge of using the Linux command line.
Step 1: Update System Packages
Start by updating your server's package index and upgrading installed packages:
sudo apt update && sudo apt upgrade -y
Restart your server if any kernel updates were installed:
sudo reboot
Step 2: Install Docker and Docker Compose
Install Docker
If Docker is not already installed, follow these steps:
Install required dependencies:
sudo apt install -y ca-certificates curl gnupg
Add Docker's official GPG key:
sudo install -m 0755 -d /etc/apt/keyrings curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg sudo chmod a+r /etc/apt/keyrings/docker.gpg
Set up the Docker repository:
echo \\"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable\\" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
Install Docker:
sudo apt update sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Verify Docker Installation
Check Docker's version to confirm successful installation:
docker --version
Install Docker Compose (Standalone)
If Docker Compose is not already available, install it manually:
Download the Docker Compose binary:
sudo curl -L "https://github.com/docker/compose/releases/download/v2.22.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
Apply executable permissions:
sudo chmod +x /usr/local/bin/docker-compose
Verify the installation:
Once you have completed the setup, ensure everything is working as expected:
Check if all containers are running:
docker ps
You should see containers for the application and database running.
Access the web interface:
Open your browser and navigate to your domain (e.g., https://analytics.example.com). You should see the Plausible Analytics login or setup page.
Inspect logs for errors:
docker-compose logs -f
Review the output to confirm that there are no errors in the containers. docker-compose --version ```
Step 3: Set Up Plausible Analytics
Clone the Plausible Analytics Repository
Navigate to a suitable directory and clone the official Plausible Analytics repository:
git clone https://github.com/plausible/hosting.git plausible-analytics cd plausible-analytics
Configure the .env File
Copy the example environment file:
cp example.env .env
Edit the .env file using your preferred text editor:
nano .env
Update the following variables:
SECRET_KEY_BASE: Generate a random string using a tool like OpenSSL:
openssl rand -base64 64
BASE_URL: Set to your domain name (e.g., https://analytics.example.com).
POSTGRES_USER and POSTGRES_PASSWORD: Set custom credentials for the PostgreSQL database.
Save and exit the file.
Update the Docker Compose Configuration (Optional)
If needed, modify the docker-compose.yml file to fit your requirements, such as changing ports or adding custom volumes.
Step 4: Set Up a Reverse Proxy with HTTPS
Plausible Analytics uses HTTP by default. You can secure your installation with HTTPS using Nginx and Let's Encrypt.
Install Nginx and Certbot
Install Nginx and Certbot:
sudo apt install -y nginx certbot python3-certbot-nginx
Configure Nginx
Create a new Nginx configuration file for your domain:
sudo nano /etc/nginx/sites-available/analytics.example.com
Add the following configuration:
server { server_name analytics.example.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; } listen 80; }
Enable the configuration and reload Nginx:
sudo ln -s /etc/nginx/sites-available/analytics.example.com /etc/nginx/sites-enabled/ sudo nginx -t sudo systemctl reload nginx
Obtain an SSL Certificate
Run Certbot to obtain and install an SSL certificate:
sudo certbot --nginx -d analytics.example.com
Test automatic certificate renewal:
sudo certbot renew --dry-run
Step 5: Start Plausible Analytics
Use Docker Compose to start the services:
docker-compose up -d
Verify that the containers are running:
docker ps
Step 6: Access the Plausible Analytics Dashboard
Open your web browser and navigate to your domain (e.g., https://analytics.example.com).
Follow the on-screen instructions to create an admin account and set up your first site.
Step 7: Add Plausible Analytics to Your Website
To start tracking analytics, add the Plausible Analytics script to your website's <head> section:
<script async defer data-domain="yourdomain.com" src="https://analytics.example.com/js/script.js"></script>
Replace yourdomain.com with your website's domain name.
Troubleshooting
If you encounter issues, check the following:
Docker logs:
docker-compose logs -f
Nginx logs:
sudo tail -f /var/log/nginx/error.log
Environment variables: Ensure all required variables are correctly set in the .env file.
Conclusion
You have successfully installed Plausible Analytics on your Ubuntu
24.04 server. Enjoy a privacy-focused, lightweight analytics solution
for your website!