Skip to content

Deploying LangFuse LLM observability on Ubuntu 22.04 (production-ready)

Harden LangFuse on Ubuntu 22.04 with Docker Compose — PostgreSQL and ClickHouse on the internal network, encryption key persisted, OAuth, nginx + Let's Encrypt.

You’ve been asked to stand up LangFuse so the LLM pipeline finally has trace data, eval runs, and a prompt registry instead of grep-ing JSON dumps out of a chat agent’s log file. The Docker quickstart runs, the first trace ingests, the team sees latency and cost split by model. Six months later, the ClickHouse instance is bound to 0.0.0.0:9000 because the compose default never moved, the ENCRYPTION_KEY was regenerated when the host was reprovisioned so every trace metadata column with a customer prompt in it is now ciphertext you cannot read, the SDK ingest endpoint has no rate limiting in front of it, and a misbehaving worker on a busy day put 4 million spans into ClickHouse before anyone noticed the dashboard slowing down.

What’s happening

A LangFuse deployment that actually backs an LLM product needs more than docker compose up. PostgreSQL holds organization, project, user, and prompt rows; ClickHouse holds the trace and observation tables that grow the fastest. Both belong on the internal compose network — never published to the host’s public interface. The web and worker containers sit behind nginx with Let’s Encrypt; the host should publish :443 only. The ENCRYPTION_KEY is a 32-byte hex value that LangFuse uses to seal LLM-API-key columns and prompt-version secrets — if it changes, those rows become unreadable, and the only recovery is to re-enter every model credential by hand and accept that the prompt-version history loses its sealed metadata. SDK ingest endpoints under /api/public/ingestion accept high-volume span batches and have to be both authenticated (LangFuse public/secret key per project) and rate-limited at the proxy so a runaway worker cannot flood ClickHouse. OAuth SSO replaces the built-in email/password so identity rides with the IdP, and the bootstrap admin account is disabled after the first OAuth login.

How to verify

After install, the host should expose only :443 and :22:

ss -lntp | grep -E ':(80|443|3000|5432|8123|9000)\b'
# 0.0.0.0:443  nginx
# 127.0.0.1:3000  langfuse-web (via compose)

PostgreSQL on 5432 and ClickHouse on 8123/9000 must not appear on the host at all. SDK ingest must reject anonymous calls:

curl -sS -o /dev/null -w '%{http_code}\n' \
  -X POST https://langfuse.example.com/api/public/ingestion \
  -H 'Content-Type: application/json' -d '{"batch":[]}'
# 401

ClickHouse must be reachable only from the LangFuse worker container, and the trace count must move when you send a test trace:

docker compose -f /opt/langfuse/docker-compose.yml exec clickhouse \
  clickhouse-client --query "SELECT count() FROM traces"

If the public ingestion endpoint returns 200 without Authorization: Basic, every model invocation in your fleet is now writeable by anyone who can hit the URL.

The fix

Save the script below as /root/setup-langfuse-ubuntu22.sh, then run it. It installs LangFuse on a fresh Ubuntu 22.04 VM: base update, UFW (SSH + HTTP + HTTPS only), SSH hardening with root key preserved, unattended-upgrades, Docker CE + compose plugin, the LangFuse v3 compose stack with PostgreSQL + ClickHouse + Redis kept on the internal network, randomly generated SALT and ENCRYPTION_KEY written to /opt/langfuse/.env with chmod 600, OAuth variables wired for the configured IdP, nginx terminating TLS for the web UI and the public SDK ingest with HSTS and per-IP rate limiting on /api/public/, Let’s Encrypt via the certbot nginx plugin, and a poll loop that waits for :3000 and :443 before declaring success.

#!/usr/bin/env bash
set -euo pipefail

### Ubuntu 22.04 LangFuse clean VM installer
### Keeps root SSH key login enabled
### Disables SSH password login
### Enables automatic security updates
### Installs and configures Docker + LangFuse + PostgreSQL + ClickHouse + Redis + nginx + Let's Encrypt
### ENCRYPTION_KEY persisted; DB and ClickHouse internal-only; ingest rate-limited
### No local backups; assumes VM-level backup
### Reboot remains manual

if [[ "$(id -u)" -ne 0 ]]; then
  echo "Run as root."
  exit 1
fi

: "${LANGFUSE_DOMAIN:?Set LANGFUSE_DOMAIN=langfuse.example.com}"
: "${LANGFUSE_EMAIL:?Set LANGFUSE_EMAIL=ops@example.com for Let's Encrypt}"
: "${OAUTH_PROVIDER:?Set OAUTH_PROVIDER=oidc (oidc|github|google)}"
: "${OAUTH_CLIENT_ID:?Set OAUTH_CLIENT_ID}"
: "${OAUTH_CLIENT_SECRET:?Set OAUTH_CLIENT_SECRET}"
OAUTH_ISSUER="${OAUTH_ISSUER:-https://sso.example.com/application/o/langfuse/}"
LANGFUSE_VERSION="${LANGFUSE_VERSION:-3.49.1}"
LANGFUSE_NEXTAUTH_SECRET="${LANGFUSE_NEXTAUTH_SECRET:-$(openssl rand -hex 32)}"
LANGFUSE_SALT="${LANGFUSE_SALT:-$(openssl rand -hex 32)}"
LANGFUSE_ENCRYPTION_KEY="${LANGFUSE_ENCRYPTION_KEY:-$(openssl rand -hex 32)}"
LANGFUSE_PG_PASSWORD="${LANGFUSE_PG_PASSWORD:-$(openssl rand -hex 24)}"
LANGFUSE_REDIS_PASSWORD="${LANGFUSE_REDIS_PASSWORD:-$(openssl rand -hex 24)}"
LANGFUSE_CLICKHOUSE_PASSWORD="${LANGFUSE_CLICKHOUSE_PASSWORD:-$(openssl rand -hex 24)}"

echo
echo "========================================"
echo "LangFuse Ubuntu 22.04 Setup"
echo "========================================"
echo

echo "==> Updating base OS"
export DEBIAN_FRONTEND=noninteractive
apt-get update
apt-get -y upgrade

echo "==> Installing base packages"
apt-get -y install \
  vim curl wget ca-certificates gnupg2 \
  chrony fail2ban unattended-upgrades \
  ufw lsof tcpdump dnsutils tar gzip \
  iproute2 net-tools openssl jq \
  software-properties-common

echo "==> Enabling time sync"
systemctl enable --now chrony
timedatectl set-ntp true

echo "==> Applying kernel/network tuning"
cat > /etc/sysctl.d/99-langfuse.conf <<'SYSCTL'
net.core.default_qdisc = fq
net.ipv4.tcp_congestion_control = bbr

net.core.somaxconn = 4096
net.core.netdev_max_backlog = 250000
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_fin_timeout = 15
net.ipv4.ip_local_port_range = 10240 65535

net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.default.accept_redirects = 0
net.ipv6.conf.all.accept_redirects = 0
net.ipv6.conf.default.accept_redirects = 0

net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0

kernel.dmesg_restrict = 1
kernel.kptr_restrict = 2
fs.file-max = 2097152
vm.max_map_count = 262144
SYSCTL

modprobe tcp_bbr || true
echo tcp_bbr > /etc/modules-load.d/bbr.conf
sysctl --system

echo "==> Setting file limits"
cat > /etc/security/limits.d/99-langfuse.conf <<'LIMITS'
* soft nofile 64000
* hard nofile 64000
root soft nofile 64000
root hard nofile 64000
LIMITS

echo "==> Configuring UFW"
ufw --force reset
ufw default deny incoming
ufw default allow outgoing
ufw allow 22/tcp
ufw allow 80/tcp
ufw allow 443/tcp
ufw --force enable

echo "==> Hardening SSH while keeping root SSH key login enabled"
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak.$(date +%F-%H%M%S)

set_sshd_option() {
  local key="$1"
  local value="$2"
  if grep -qE "^[#[:space:]]*${key}[[:space:]]+" /etc/ssh/sshd_config; then
    sed -i "s|^[#[:space:]]*${key}[[:space:]].*|${key} ${value}|" /etc/ssh/sshd_config
  else
    echo "${key} ${value}" >> /etc/ssh/sshd_config
  fi
}

set_sshd_option PermitRootLogin prohibit-password
set_sshd_option PasswordAuthentication no
set_sshd_option PubkeyAuthentication yes
set_sshd_option KbdInteractiveAuthentication no
set_sshd_option ChallengeResponseAuthentication no
set_sshd_option X11Forwarding no
set_sshd_option ClientAliveInterval 300
set_sshd_option ClientAliveCountMax 2
set_sshd_option MaxAuthTries 4

sshd -t
systemctl restart ssh

echo "==> Configuring fail2ban"
mkdir -p /etc/fail2ban/jail.d
cat > /etc/fail2ban/jail.d/sshd.local <<'FAIL2BAN'
[sshd]
enabled = true
port = ssh
backend = systemd
maxretry = 4
findtime = 10m
bantime = 1h
FAIL2BAN

systemctl enable --now fail2ban

echo "==> Configuring unattended-upgrades for security"
cat > /etc/apt/apt.conf.d/20auto-upgrades <<'AUTOUPG'
APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Unattended-Upgrade "1";
APT::Periodic::AutocleanInterval "7";
AUTOUPG

echo "==> Installing Docker CE"
install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg \
  | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
chmod a+r /etc/apt/keyrings/docker.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu jammy stable" \
  > /etc/apt/sources.list.d/docker.list
apt-get update
apt-get -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

systemctl enable --now docker

echo "==> Preparing /opt/langfuse"
mkdir -p /opt/langfuse/volumes/{pgdata,clickhouse,redis,minio,events}
chmod 700 /opt/langfuse

cat > /opt/langfuse/.env <<ENV
LANGFUSE_VERSION=${LANGFUSE_VERSION}
LANGFUSE_DOMAIN=${LANGFUSE_DOMAIN}
NEXTAUTH_SECRET=${LANGFUSE_NEXTAUTH_SECRET}
SALT=${LANGFUSE_SALT}
ENCRYPTION_KEY=${LANGFUSE_ENCRYPTION_KEY}
PG_PASSWORD=${LANGFUSE_PG_PASSWORD}
REDIS_PASSWORD=${LANGFUSE_REDIS_PASSWORD}
CLICKHOUSE_PASSWORD=${LANGFUSE_CLICKHOUSE_PASSWORD}
OAUTH_PROVIDER=${OAUTH_PROVIDER}
OAUTH_CLIENT_ID=${OAUTH_CLIENT_ID}
OAUTH_CLIENT_SECRET=${OAUTH_CLIENT_SECRET}
OAUTH_ISSUER=${OAUTH_ISSUER}
ENV

chmod 600 /opt/langfuse/.env

cat > /opt/langfuse/docker-compose.yml <<'COMPOSE'
services:
  postgres:
    image: postgres:16-alpine
    restart: unless-stopped
    environment:
      POSTGRES_USER: langfuse
      POSTGRES_PASSWORD: ${PG_PASSWORD}
      POSTGRES_DB: langfuse
    volumes:
      - /opt/langfuse/volumes/pgdata:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U langfuse -d langfuse"]
      interval: 30s
      timeout: 5s
      retries: 5
      start_period: 20s

  clickhouse:
    image: clickhouse/clickhouse-server:24.3
    restart: unless-stopped
    environment:
      CLICKHOUSE_DB: default
      CLICKHOUSE_USER: clickhouse
      CLICKHOUSE_PASSWORD: ${CLICKHOUSE_PASSWORD}
      CLICKHOUSE_DEFAULT_ACCESS_MANAGEMENT: "1"
    volumes:
      - /opt/langfuse/volumes/clickhouse:/var/lib/clickhouse
    ulimits:
      nofile:
        soft: 262144
        hard: 262144
    healthcheck:
      test: ["CMD-SHELL", "wget -qO- http://localhost:8123/ping || exit 1"]
      interval: 30s
      timeout: 5s
      retries: 5
      start_period: 30s

  redis:
    image: redis:7-alpine
    restart: unless-stopped
    command: redis-server --requirepass ${REDIS_PASSWORD} --maxmemory-policy allkeys-lru
    volumes:
      - /opt/langfuse/volumes/redis:/data
    healthcheck:
      test: ["CMD-SHELL", "redis-cli -a ${REDIS_PASSWORD} ping | grep PONG"]
      interval: 30s
      timeout: 3s
      retries: 5
      start_period: 20s

  minio:
    image: minio/minio:RELEASE.2024-08-29T01-40-52Z
    restart: unless-stopped
    command: server /data --console-address ":9001"
    environment:
      MINIO_ROOT_USER: minio
      MINIO_ROOT_PASSWORD: ${CLICKHOUSE_PASSWORD}
    volumes:
      - /opt/langfuse/volumes/minio:/data

  langfuse-worker:
    image: langfuse/langfuse-worker:${LANGFUSE_VERSION}
    restart: unless-stopped
    depends_on:
      postgres:
        condition: service_healthy
      clickhouse:
        condition: service_healthy
      redis:
        condition: service_healthy
    environment:
      DATABASE_URL: postgresql://langfuse:${PG_PASSWORD}@postgres:5432/langfuse
      CLICKHOUSE_MIGRATION_URL: clickhouse://clickhouse:9000
      CLICKHOUSE_URL: http://clickhouse:8123
      CLICKHOUSE_USER: clickhouse
      CLICKHOUSE_PASSWORD: ${CLICKHOUSE_PASSWORD}
      CLICKHOUSE_CLUSTER_ENABLED: "false"
      REDIS_HOST: redis
      REDIS_PORT: "6379"
      REDIS_AUTH: ${REDIS_PASSWORD}
      LANGFUSE_S3_EVENT_UPLOAD_BUCKET: langfuse
      LANGFUSE_S3_EVENT_UPLOAD_REGION: auto
      LANGFUSE_S3_EVENT_UPLOAD_ACCESS_KEY_ID: minio
      LANGFUSE_S3_EVENT_UPLOAD_SECRET_ACCESS_KEY: ${CLICKHOUSE_PASSWORD}
      LANGFUSE_S3_EVENT_UPLOAD_ENDPOINT: http://minio:9000
      LANGFUSE_S3_EVENT_UPLOAD_FORCE_PATH_STYLE: "true"
      SALT: ${SALT}
      ENCRYPTION_KEY: ${ENCRYPTION_KEY}
      TELEMETRY_ENABLED: "false"
      LANGFUSE_ENABLE_EXPERIMENTAL_FEATURES: "false"

  langfuse-web:
    image: langfuse/langfuse:${LANGFUSE_VERSION}
    restart: unless-stopped
    depends_on:
      postgres:
        condition: service_healthy
      clickhouse:
        condition: service_healthy
      redis:
        condition: service_healthy
    environment:
      DATABASE_URL: postgresql://langfuse:${PG_PASSWORD}@postgres:5432/langfuse
      CLICKHOUSE_MIGRATION_URL: clickhouse://clickhouse:9000
      CLICKHOUSE_URL: http://clickhouse:8123
      CLICKHOUSE_USER: clickhouse
      CLICKHOUSE_PASSWORD: ${CLICKHOUSE_PASSWORD}
      REDIS_HOST: redis
      REDIS_PORT: "6379"
      REDIS_AUTH: ${REDIS_PASSWORD}
      LANGFUSE_S3_EVENT_UPLOAD_BUCKET: langfuse
      LANGFUSE_S3_EVENT_UPLOAD_REGION: auto
      LANGFUSE_S3_EVENT_UPLOAD_ACCESS_KEY_ID: minio
      LANGFUSE_S3_EVENT_UPLOAD_SECRET_ACCESS_KEY: ${CLICKHOUSE_PASSWORD}
      LANGFUSE_S3_EVENT_UPLOAD_ENDPOINT: http://minio:9000
      LANGFUSE_S3_EVENT_UPLOAD_FORCE_PATH_STYLE: "true"
      NEXTAUTH_URL: https://${LANGFUSE_DOMAIN}
      NEXTAUTH_SECRET: ${NEXTAUTH_SECRET}
      SALT: ${SALT}
      ENCRYPTION_KEY: ${ENCRYPTION_KEY}
      TELEMETRY_ENABLED: "false"
      LANGFUSE_INIT_USER_NAME: ""
      AUTH_DISABLE_USERNAME_PASSWORD: "false"
      AUTH_DISABLE_SIGNUP: "true"
      AUTH_OAUTH_PROVIDER: ${OAUTH_PROVIDER}
      AUTH_OAUTH_CLIENT_ID: ${OAUTH_CLIENT_ID}
      AUTH_OAUTH_CLIENT_SECRET: ${OAUTH_CLIENT_SECRET}
      AUTH_OAUTH_ISSUER: ${OAUTH_ISSUER}
    ports:
      - "127.0.0.1:3000:3000"
    ulimits:
      nofile:
        soft: 64000
        hard: 64000
COMPOSE

echo "==> Starting LangFuse stack"
docker compose --env-file /opt/langfuse/.env -f /opt/langfuse/docker-compose.yml up -d

echo "==> Waiting for LangFuse web on 127.0.0.1:3000"
for i in {1..120}; do
  if ss -lntp | grep -q '127.0.0.1:3000'; then
    break
  fi
  sleep 2
done

if ! ss -lntp | grep -q '127.0.0.1:3000'; then
  echo "LangFuse web failed to listen on 127.0.0.1:3000"
  docker compose -f /opt/langfuse/docker-compose.yml logs --tail=200 langfuse-web || true
  exit 1
fi

echo "==> Installing nginx and certbot"
apt-get -y install nginx certbot python3-certbot-nginx

cat > /etc/nginx/conf.d/langfuse-ratelimit.conf <<'RATELIMIT'
limit_req_zone $binary_remote_addr zone=langfuse_ingest:10m rate=200r/s;
RATELIMIT

cat > /etc/nginx/sites-available/langfuse <<NGINX
upstream langfuse_backend {
    server 127.0.0.1:3000;
    keepalive 32;
}

server {
    listen 80;
    listen [::]:80;
    server_name ${LANGFUSE_DOMAIN};
    return 301 https://\$host\$request_uri;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name ${LANGFUSE_DOMAIN};

    client_max_body_size 100M;
    proxy_read_timeout   300s;
    proxy_send_timeout   300s;
    proxy_connect_timeout 60s;

    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
    add_header X-Content-Type-Options nosniff always;
    add_header X-Frame-Options SAMEORIGIN always;
    add_header Referrer-Policy strict-origin-when-cross-origin always;

    location /api/public/ {
        limit_req zone=langfuse_ingest burst=400 nodelay;
        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;
        proxy_http_version 1.1;
        proxy_pass http://langfuse_backend;
    }

    location / {
        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;
        proxy_http_version 1.1;
        proxy_pass http://langfuse_backend;
    }
}
NGINX

ln -sf /etc/nginx/sites-available/langfuse /etc/nginx/sites-enabled/langfuse
rm -f /etc/nginx/sites-enabled/default

nginx -t
systemctl enable --now nginx
systemctl reload nginx

echo "==> Requesting Let's Encrypt certificate"
certbot --nginx --non-interactive --agree-tos \
  -m "${LANGFUSE_EMAIL}" -d "${LANGFUSE_DOMAIN}" --redirect

systemctl enable --now certbot.timer

echo "==> Waiting for HTTPS listener"
for i in {1..30}; do
  if ss -lntp | grep -q ':443'; then
    break
  fi
  sleep 1
done

echo "==> Final package update"
apt-get -y upgrade

echo
echo "========================================"
echo "DONE"
echo "========================================"
echo
echo "Kernel:"
uname -r

echo
echo "SSH effective settings:"
sshd -T | grep -E '^(permitrootlogin|passwordauthentication|pubkeyauthentication) ' || true

echo
echo "Listeners:"
ss -lntp | grep -E ':(80|443|3000|5432|8123|9000)\b' || true

echo
echo "Containers:"
docker compose -f /opt/langfuse/docker-compose.yml ps

echo
echo "Services:"
systemctl is-active docker nginx fail2ban chrony || true

echo
echo "Anonymous ingest must be 401:"
curl -sS -o /dev/null -w '%{http_code}\n' \
  -X POST "https://${LANGFUSE_DOMAIN}/api/public/ingestion" \
  -H 'Content-Type: application/json' -d '{"batch":[]}' || true

echo
echo "Secrets written to /opt/langfuse/.env (mode 600). Move them to your secrets manager and remove the file from this host before going live."
echo "ENCRYPTION_KEY MUST be persisted. If you lose it, every stored LLM provider API key and every encrypted prompt-version secret is unreadable ciphertext. There is no recovery path."

echo
echo "Web UI:"
echo "https://${LANGFUSE_DOMAIN}/"

echo
echo "First steps:"
echo "1. Sign in via OAuth."
echo "2. Create a project. Generate the project public + secret keys."
echo "3. Configure the SDK with LANGFUSE_HOST=https://${LANGFUSE_DOMAIN}, the public key, and the secret key."
echo "4. Send a test trace from any SDK; confirm it appears under Traces within ~30s."

Make it executable and run it:

chmod 700 /root/setup-langfuse-ubuntu22.sh
LANGFUSE_DOMAIN=langfuse.example.com \
LANGFUSE_EMAIL=ops@example.com \
OAUTH_PROVIDER=oidc \
OAUTH_CLIENT_ID=langfuse \
OAUTH_CLIENT_SECRET=... \
OAUTH_ISSUER=https://sso.example.com/application/o/langfuse/ \
/root/setup-langfuse-ubuntu22.sh

Open https://langfuse.example.com/ and complete the OAuth sign-in; the first user becomes the organization owner. Create a project, copy the public and secret keys into your SDK config, and confirm a test trace lands within ~30 seconds. Move /opt/langfuse/.env into your secrets manager and back up ENCRYPTION_KEY separately — losing it makes every stored model provider credential and every sealed prompt-version field unreadable, with no recovery path.

Run the production LangFuse on a Stack Harbor-managed environment with alerts on LLM cost spikes, ingest rate anomalies, and Sev-1 escalation: Monitoring and support.