How to Self-Host PostHog on a VPS (Complete Guide)
PostHog is a powerful analytics platform designed for developers who want to self-host their applications while maintaining control over their data. This guide will take you through the steps needed to install PostHog on a Virtual Private Server (VPS). Weโll cover everything from choosing a provider to running the PostHog application using Docker.
Choosing Your VPS Provider
Before we dive into the installation, you need to choose a VPS provider. Below are some popular options that cater to developers and homelabbers alike:
| Provider | Price | Best For |
|---|---|---|
| Contabo VPS | 5.99 EUR/mo | Budget-friendly solutions |
| Hetzner Cloud | 4.15 EUR/mo | High performance at low cost |
| DigitalOcean | 6 USD/mo | Easy-to-use interface |
| Vultr | 6 USD/mo | Wide range of locations |
| Linode (Akamai Cloud) | 5 USD/mo | Reliable uptime and performance |
For a full VPS comparison, visit our full VPS comparison.
Prerequisites
- VPS Setup: Make sure your VPS is running on Ubuntu 20.04 or later.
- Domain: Itโs recommended to have a domain name for easy access to your PostHog instance.
- Docker and Docker Compose: Ensure Docker and Docker Compose are installed on your VPS.
To install Docker and Docker Compose, use the following commands:
sudo apt update
sudo apt install -y apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt update
sudo apt install -y docker-ce
sudo apt install -y docker-compose
Installing PostHog
Now that your environment is set up, youโre ready to install PostHog:
1. Clone the Repository
Open your terminal and execute the following command to clone the PostHog repository:
git clone https://github.com/PostHog/posthog.git
cd posthog
2. Create a .env File
Create a .env file in the root of the cloned directory to configure your PostHog environment. Here is a basic example:
# PostHog Configuration
POSTHOG_INSTANCE_ID=your_instance_id
POSTHOG_SECRET_KEY=your_secret_key
# Adjust the Redis and PostgreSQL settings as needed
3. Set Up Docker Compose
Next, create a docker-compose.yml file. Below is a sample configuration:
version: '3.8'
services:
web:
image: posthog/posthog
env_file: .env
ports:
- "8000:8000"
depends_on:
- redis
- postgres
redis:
image: redis:6.0
ports:
- "6379:6379"
postgres:
image: postgres:13
environment:
POSTGRES_DB: posthog
POSTGRES_USER: posthog
POSTGRES_PASSWORD: your_postgres_password
ports:
- "5432:5432"
networks:
default:
driver: bridge
4. Start the Services
Now, start your PostHog instance using Docker Compose:
docker-compose up -d
This command will download the necessary images and start the services in detached mode.
5. Access PostHog
Open your web browser and go to http://your-domain-or-ip:8000. You should see the PostHog interface where you can begin tracking your data.
Configuring PostHog
Once you have PostHog running, you may want to perform initial configurations:
- User Authentication: Set up a user account and manage access rights.
- Integrate with Your Apps: Use the PostHog JavaScript library to add tracking to your applications.
- Data Management: Familiarize yourself with the dashboard to manage events and properties.
FAQ
Can I run PostHog on a low-end VPS?
Absolutely, PostHog can run on a low-end VPS, but performance may vary based on the number of events you track and the number of concurrent users. If youโre just starting, consider using providers like Hetzner Cloud or Contabo, which offer affordable plans with enough resources for small to medium-sized installations.
How do I migrate PostHog to another VPS?
To migrate PostHog to another VPS, you need to back up your database and configuration files. Export your PostgreSQL database using pg_dump, transfer it to the new server, and restore it using psql. Donโt forget to also move your .env file and docker-compose.yml file to the new environment.
Is PostHog secure to self-host?
Self-hosting PostHog can be secure if you follow best practices such as using HTTPS, configuring firewalls, and keeping your software up to date. Make sure to properly secure your PostgreSQL database and Redis instance, and consider using a service like Letโs Encrypt for SSL certificates to ensure encrypted connections.
Conclusion
Self-hosting PostHog on a VPS gives you full control over your analytics data and can save costs in the long run. Following this guide, you can quickly set up and start using PostHog for your applications. If you encounter any issues, the r/selfhosted community and the awesome-selfhosted repository are great resources for troubleshooting and finding additional tools. Happy self-hosting!