How to Install PostHog on Debian Latest

In this tutorial, we will guide you on how to install PostHog on Debian. PostHog is an open-source, self-hosted product analytics platform that empowers users to understand their product usage in a better way. This will help software development teams to develop better feature products based on real-world data analysis.

Prerequisites

Before you begin, ensure that you have the following:

Step 1: Update the System

Update the Debian system to its latest version by running the following command:

sudo apt-get update && sudo apt-get upgrade -y

Step 2: Install Required Dependencies

PostHog is built using the latest technology stacks such as Python, NodeJS, and others. We need to install these dependencies before installing PostHog.

Install PostHog dependencies by running the following command:

sudo apt-get install -y python3 python3-distutils python3-venv python3-dev libpq-dev libffi-dev libssl-dev postgresql postgresql-contrib nginx

Step 3: Create a New PostgreSQL Database

PostHog requires a PostgreSQL database to store data. We will create a new Postgres user and database for PostHog.

Log in as the default PostgreSQL user postgres by running:

sudo su postgres

Then create a new database and user using the following commands:

createuser posthog --pwprompt
createdb posthog -O posthog

Enter a strong password and save it for future use.

Exit the PostgreSQL prompt by running:

exit

Step 4: Install PostHog

Now that we have installed all the required dependencies, let us start the installation of PostHog.

Create a new user for PostHog and switch to it by running the following command:

sudo adduser posthog
sudo su posthog 

Clone the PostHog repository by running the following command:

git clone https://github.com/PostHog/posthog.git

Change the directory to the PostHog folder:

cd posthog

Create a new virtual environment and activate it by running:

python3 -m venv .env
source .env/bin/activate

Install PostHog and its dependencies using the setup.py file by running:

python setup.py develop

Set the environmental variables for PostHog by running:

export POSTHOG_DATABASE_URL=postgres://posthog:$PASSWORD@localhost/posthog
export SECRET_KEY=mysecretkey

Replace $PASSWORD with the PostgreSQL password created earlier and set a unique secret key for PostHog.

Finally, run the following command to start the PostHog server:

python server.py

Step 5: Configure Nginx for PostHog

We will configure the Nginx web server to proxy the PostHog application.

Create a new Nginx server configuration file by running the following command:

sudo nano /etc/nginx/sites-available/posthog

Paste the following configuration content into the file:

server {
    listen 80;
    server_name yourdomain.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;
    }

    location /static/ {
        alias /home/posthog/posthog/static/;
    }

    location /media/ {
        alias /home/posthog/posthog/media/;
    }
}

Replace yourdomain.com with your actual domain name.

Save and close the file. Then, activate the server config by creating a symbolic link to the sites-enabled folder:

sudo ln -s /etc/nginx/sites-available/posthog /etc/nginx/sites-enabled/

Remove the default Nginx configuration by running:

sudo rm /etc/nginx/sites-enabled/default

Test the Nginx configuration by running:

sudo nginx -t

Restart the Nginx service to apply the changes:

sudo systemctl restart nginx

Step 6: Access the PostHog Dashboard

You have now successfully installed and configured PostHog on your Debian Server. To access the PostHog dashboard, go to http://yourdomain.com.

You can now start analyzing your product usage by exploring the PostHog dashboard.

Congratulations on your successful installation of PostHog on Debian!

If you want to self-host in an easy, hands free way, need an external IP address, or simply want your data in your own hands, give IPv6.rs a try!

Alternatively, for the best virtual desktop, try Shells!