HomeBlog

Deploy PostHog on Dokku

August 27, 2021 3 min read

This guide can also be found directly in the dokku-posthog Github repository.

What is PostHog?

PostHog is an open-source product analytics suite, built for developers. Automate the collection of every event on your website or app, with no need to send data to 3rd parties.

What is Dokku?

Dokku is the smallest PaaS implementation you've ever seen

Requirements

Setup

Note: We are going to use the domain posthog.example.com for demonstration purposes. Make sure to replace it with your own domain name.

App and plugins

Create the app

Log onto your Dokku Host to create the PostHog app:

dokku apps:create posthog

Add plugins

Install, create and link PostgreSQL plugin:

# Install plugins
dokku plugin:install https://github.com/dokku/dokku-postgres.git postgres
dokku plugin:install https://github.com/dokku/dokku-redis.git redis
# Create running plugins
dokku postgres:create posthog
dokku redis:create posthog
# Link plugins to the main app
dokku postgres:link posthog posthog
dokku redis:link posthog posthog

Configure dokku app

Environment variables

dokku config:set posthog POSTGRES_DB=posthog POSTGRES_PASSWORD=<...> POSTGRES_USER=postgres SECRET_KEY=<...>

Postgres

Extract password from existing

# Show all enironement variables to copy content of DATABASE_URL variable
dokku config posthog

Copy the password from DATABASE_URL

dokku config:set posthog POSTGRES_DB=posthog POSTGRES_PASSWORD=<found in DATABSE_URL>

Web app

dokku config:set posthog SECRET_KEY=<randomly generated secret key> SITE_URL=https://posthog.example.com IS_BEHIND_PROXY=True DISABLE_SECURE_SSL_REDIRECT=True

Set up the domain

To get the routing working, we need to apply a few settings. First we set the domain.

dokku domains:set posthog posthog.example.com

Then we set the ports to redirect to

dokku proxy:ports-set posthog http:80:8000 https:443:8000

Push PostHog to Dokku

  1. Clone the dokku-posthog repository.

  2. Set up your Dokku server as a remote.

    git remote add dokku dokku@example.com:posthog
    
  3. Push it to Dokku

    git push dokku master
    

Add SSL certificate (to enable HTTPS)

Last but not least, we can go an grab the SSL certificate from Let's Encrypt.

# Install letsencrypt plugin
dokku plugin:install https://github.com/dokku/dokku-letsencrypt.git

# Set certificate contact email
dokku config:set --no-restart posthog DOKKU_LETSENCRYPT_EMAIL=you@example.com

# Generate certificate
dokku letsencrypt posthog

Wrapping up

Your PostHog instance should now be available on https://posthog.example.com.


Subscribe to the Newsletter

Get emails from me about tech, cool open-source projects, and learnings from my projects.

Frequency: ~ 1 per month ✌️