Skip to content

Deploying self-hosted Sentry on Ubuntu 22.04 (production-ready)

Stand up the official getsentry/self-hosted Sentry stack on Ubuntu 22.04 — Postgres, Redis, ClickHouse via Docker Compose, nginx + Let's Encrypt, SMTP wiring.

You followed the getsentry/self-hosted README, ran install.sh, set an admin password, captured a first event — done in an afternoon. Six months later the volume holding ClickHouse is at 92 %, the install hasn’t been upgraded past its minor version because the upgrade path requires reading release notes nobody has, the SMTP wiring was left at the default placeholder so issue alerts have never reached anyone, and the web UI is exposed on port 9000 from the public internet because the team never put nginx in front. The stack runs. Nobody trusts it.

What’s happening

Self-hosted Sentry is a Docker Compose stack — web, worker, cron, relay, plus Postgres, Redis, Kafka, ClickHouse — and treating it like a single container is the bug. The compose stack must run behind a reverse proxy on a domain with a real TLS cert (Sentry’s CSRF protection bites the moment you put it behind a proxy without setting system.url-prefix). Email needs a working SMTP relay or the issue-alert pipeline is broken silently. ClickHouse retention is set per-project in the UI but the partition retention in the docker-compose env is what actually frees disk. Upgrades are version-by-version through the install.sh script — skipping a release is unsupported. None of this is exotic if you’ve operated it; it’s a graveyard if you treat the install script as the whole job.

How to verify

After bringing the stack up, the four data services should be running and the web service should respond locally:

docker compose ps | grep -E '(web|worker|relay|postgres|redis|clickhouse)'

Sentry exposes a health endpoint that should return JSON, not a 502:

curl -s http://127.0.0.1:9000/_health/
# {"healthy": true, "problems": []}

The first send-test verifies the relay accepts ingest. From any host:

sentry-cli --url https://sentry.example.com send-event -m "self-test from $(hostname)"

If port 9000 is bound to 0.0.0.0 and the firewall does not block it, the install is publicly accessible — fix that before issuing the first DSN.

The fix

Save the script below as /root/setup-sentry-ubuntu2204.sh, then run it. It performs the full installer flow on a fresh Ubuntu 22.04 VM with at least 8 GB RAM and 64 GB free disk (the official minimums for the self-hosted stack): base update, UFW, SSH hardening (root key login kept, password disabled), unattended-upgrades for security, Docker engine + compose plugin from the upstream Docker repo, a clone of getsentry/self-hosted pinned to a current 24.x tag, a generated .env with secret key + SMTP + system.url-prefix, install.sh run non-interactively, nginx + certbot for the external UI, and LimitNOFILE=64000 on Docker. The script waits for the web container to become healthy before continuing.

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

### Ubuntu 22.04 Sentry self-hosted clean VM installer
### Pins getsentry/self-hosted to a 24.x release tag
### Docker Compose stack: web, worker, cron, relay + Postgres/Redis/ClickHouse/Kafka
### Web listens on 127.0.0.1:9000; only nginx is public
### TLS via Let's Encrypt
### Keeps root SSH key login; disables password auth
### No local backups; assumes VM-level backup
### Minimum: 4 vCPU, 8 GB RAM, 64 GB disk

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

: "${SENTRY_DOMAIN:?Set SENTRY_DOMAIN, e.g. sentry.example.com}"
: "${SENTRY_EMAIL:?Set SENTRY_EMAIL for Let's Encrypt}"
: "${SENTRY_ADMIN_EMAIL:?Set SENTRY_ADMIN_EMAIL for the initial superuser}"
: "${SMTP_HOST:?Set SMTP_HOST}"
: "${SMTP_PORT:?Set SMTP_PORT, e.g. 587}"
: "${SMTP_USER:?Set SMTP_USER}"
: "${SMTP_PASS:?Set SMTP_PASS}"
: "${SMTP_FROM:?Set SMTP_FROM, e.g. sentry@example.com}"

SENTRY_VERSION="${SENTRY_VERSION:-24.10.0}"

echo
echo "========================================"
echo "Sentry self-hosted Ubuntu 22.04 Setup"
echo "Domain: $SENTRY_DOMAIN"
echo "Version: $SENTRY_VERSION"
echo "========================================"
echo

export DEBIAN_FRONTEND=noninteractive

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

echo "==> Installing base packages"
apt-get -y install \
  curl wget git ca-certificates gnupg \
  chrony fail2ban unattended-upgrades \
  lsof tcpdump dnsutils iproute2 net-tools jq \
  nginx \
  ufw

systemctl enable --now chronyd 2>/dev/null || systemctl enable --now chrony

echo "==> sysctl + file limits"
cat > /etc/sysctl.d/99-sentry.conf <<'SYSCTL'
net.core.somaxconn = 4096
net.core.netdev_max_backlog = 250000
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_syncookies = 1
fs.file-max = 2097152
fs.inotify.max_user_watches = 524288
vm.max_map_count = 262144
kernel.dmesg_restrict = 1
SYSCTL
sysctl --system

cat > /etc/security/limits.d/99-sentry.conf <<'LIMITS'
* soft nofile 64000
* hard nofile 64000
LIMITS

echo "==> Hardening SSH (keep root key)"
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak.$(date +%F-%H%M%S)
set_sshd() {
  local k="$1" v="$2"
  if grep -qE "^[#[:space:]]*${k}[[:space:]]+" /etc/ssh/sshd_config; then
    sed -i "s|^[#[:space:]]*${k}[[:space:]].*|${k} ${v}|" /etc/ssh/sshd_config
  else
    echo "${k} ${v}" >> /etc/ssh/sshd_config
  fi
}
set_sshd PermitRootLogin prohibit-password
set_sshd PasswordAuthentication no
set_sshd PubkeyAuthentication yes
set_sshd KbdInteractiveAuthentication no
set_sshd X11Forwarding no
set_sshd ClientAliveInterval 300
set_sshd MaxAuthTries 4
sshd -t
systemctl restart ssh

echo "==> fail2ban + unattended-upgrades"
cat > /etc/fail2ban/jail.d/sshd.local <<'F2B'
[sshd]
enabled = true
port = ssh
backend = systemd
maxretry = 4
findtime = 10m
bantime = 1h
F2B
systemctl enable --now fail2ban

cat > /etc/apt/apt.conf.d/20auto-upgrades <<'AUTO'
APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Unattended-Upgrade "1";
AUTO
systemctl enable --now unattended-upgrades

echo "==> UFW: 22, 80, 443 only"
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 "==> Installing Docker engine + compose plugin"
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 $(. /etc/os-release && echo $VERSION_CODENAME) 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

mkdir -p /etc/systemd/system/docker.service.d
cat > /etc/systemd/system/docker.service.d/limits.conf <<'DLIM'
[Service]
LimitNOFILE=64000
DLIM
systemctl daemon-reload
systemctl enable --now docker

echo "==> Cloning getsentry/self-hosted @ ${SENTRY_VERSION}"
mkdir -p /opt
cd /opt
if [[ ! -d /opt/self-hosted ]]; then
  git clone https://github.com/getsentry/self-hosted.git
fi
cd /opt/self-hosted
git fetch --tags
git checkout "${SENTRY_VERSION}"

echo "==> Generating .env"
SENTRY_SECRET_KEY="$(openssl rand -hex 32)"

cat > /opt/self-hosted/.env.custom <<ENV
SENTRY_EVENT_RETENTION_DAYS=90
SENTRY_BIND=127.0.0.1:9000
SENTRY_IMAGE=getsentry/sentry:${SENTRY_VERSION}
SNUBA_IMAGE=getsentry/snuba:${SENTRY_VERSION}
RELAY_IMAGE=getsentry/relay:${SENTRY_VERSION}
SYMBOLICATOR_IMAGE=getsentry/symbolicator:${SENTRY_VERSION}
COMPOSE_PROFILES=feature-complete
ENV

# Append secret + url prefix + SMTP into the main .env which install.sh reads
if [[ ! -f /opt/self-hosted/.env ]]; then
  cp /opt/self-hosted/.env.example /opt/self-hosted/.env 2>/dev/null || touch /opt/self-hosted/.env
fi

update_env() {
  local k="$1" v="$2"
  if grep -qE "^${k}=" /opt/self-hosted/.env; then
    sed -i "s|^${k}=.*|${k}=${v}|" /opt/self-hosted/.env
  else
    echo "${k}=${v}" >> /opt/self-hosted/.env
  fi
}

update_env SENTRY_SECRET_KEY        "${SENTRY_SECRET_KEY}"
update_env SENTRY_EVENT_RETENTION_DAYS 90
update_env SENTRY_BIND              "127.0.0.1:9000"
update_env SENTRY_IMAGE             "getsentry/sentry:${SENTRY_VERSION}"
update_env SNUBA_IMAGE              "getsentry/snuba:${SENTRY_VERSION}"
update_env RELAY_IMAGE              "getsentry/relay:${SENTRY_VERSION}"
update_env SYMBOLICATOR_IMAGE       "getsentry/symbolicator:${SENTRY_VERSION}"

echo "==> Writing sentry.conf.py overrides"
mkdir -p /opt/self-hosted/sentry
cat > /opt/self-hosted/sentry/sentry.conf.py.overrides <<PYCONF
# Loaded by config.yml override layer.
SENTRY_OPTIONS["system.url-prefix"] = "https://${SENTRY_DOMAIN}"
SENTRY_OPTIONS["mail.host"]    = "${SMTP_HOST}"
SENTRY_OPTIONS["mail.port"]    = ${SMTP_PORT}
SENTRY_OPTIONS["mail.username"] = "${SMTP_USER}"
SENTRY_OPTIONS["mail.password"] = "${SMTP_PASS}"
SENTRY_OPTIONS["mail.use-tls"] = True
SENTRY_OPTIONS["mail.from"]    = "${SMTP_FROM}"
PYCONF

# Merge overrides into the canonical sentry.conf.py if not already merged
if [[ -f /opt/self-hosted/sentry/sentry.conf.py ]] && ! grep -q 'system.url-prefix' /opt/self-hosted/sentry/sentry.conf.py; then
  echo "" >> /opt/self-hosted/sentry/sentry.conf.py
  cat /opt/self-hosted/sentry/sentry.conf.py.overrides >> /opt/self-hosted/sentry/sentry.conf.py
fi

# Also write config.yml SMTP block in case sentry.conf.py is regenerated
cat > /opt/self-hosted/sentry/config.yml.overrides <<YML
system.url-prefix: 'https://${SENTRY_DOMAIN}'
mail.backend: 'smtp'
mail.host: '${SMTP_HOST}'
mail.port: ${SMTP_PORT}
mail.username: '${SMTP_USER}'
mail.password: '${SMTP_PASS}'
mail.use-tls: true
mail.from: '${SMTP_FROM}'
YML
if [[ -f /opt/self-hosted/sentry/config.yml ]] && ! grep -q 'system.url-prefix' /opt/self-hosted/sentry/config.yml; then
  echo "" >> /opt/self-hosted/sentry/config.yml
  cat /opt/self-hosted/sentry/config.yml.overrides >> /opt/self-hosted/sentry/config.yml
fi

echo "==> Running install.sh (non-interactive, this takes 10-25 minutes)"
cd /opt/self-hosted
./install.sh --no-user-prompt --skip-commit-check

echo "==> Starting the stack"
docker compose up -d

echo "==> Waiting for /_health/ to return healthy"
for i in {1..120}; do
  if curl -fsS http://127.0.0.1:9000/_health/ 2>/dev/null | grep -q 'healthy'; then
    echo "  sentry web healthy"
    break
  fi
  sleep 5
done

echo "==> Creating initial superuser"
docker compose run --rm web createuser \
  --email "${SENTRY_ADMIN_EMAIL}" \
  --superuser \
  --no-password \
  --force-update || true
# Operator must set the password via the password-reset link sent over SMTP

echo "==> nginx + Let's Encrypt"
apt-get -y install certbot python3-certbot-nginx

cat > /etc/nginx/sites-available/sentry <<NGINX_BOOT
server {
    listen 80;
    server_name ${SENTRY_DOMAIN};
    location /.well-known/acme-challenge/ { root /var/www/html; }
    location / { return 200 'bootstrap'; }
}
NGINX_BOOT
ln -sf /etc/nginx/sites-available/sentry /etc/nginx/sites-enabled/sentry
rm -f /etc/nginx/sites-enabled/default
mkdir -p /var/www/html
nginx -t && systemctl reload nginx

if [[ ! -d /etc/letsencrypt/live/${SENTRY_DOMAIN} ]]; then
  certbot certonly --webroot -w /var/www/html \
    -d "${SENTRY_DOMAIN}" -m "${SENTRY_EMAIL}" --agree-tos --non-interactive
fi

cat > /etc/nginx/sites-available/sentry <<NGINX
server {
    listen 80;
    server_name ${SENTRY_DOMAIN};
    location /.well-known/acme-challenge/ { root /var/www/html; }
    location / { return 301 https://\$host\$request_uri; }
}

server {
    listen 443 ssl http2;
    server_name ${SENTRY_DOMAIN};

    ssl_certificate /etc/letsencrypt/live/${SENTRY_DOMAIN}/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/${SENTRY_DOMAIN}/privkey.pem;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers HIGH:!aNULL:!MD5;
    add_header Strict-Transport-Security "max-age=31536000" always;

    client_max_body_size 100M;
    keepalive_timeout 5;

    location / {
        proxy_pass http://127.0.0.1:9000/;
        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 https;
        proxy_read_timeout 300s;
        proxy_send_timeout 300s;
    }
}
NGINX

nginx -t && systemctl reload nginx
systemctl enable --now certbot.timer 2>/dev/null || true

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

echo
echo "========================================"
echo "DONE"
echo "========================================"
echo
echo "Listeners:"
ss -lntp | grep -E ':(9000|443|80)\b' || true
echo
echo "Containers:"
docker compose -f /opt/self-hosted/docker-compose.yml ps
echo
echo "Sentry health:"
curl -s http://127.0.0.1:9000/_health/ || true
echo
echo "Public URL: https://${SENTRY_DOMAIN}/"
echo
echo "Next steps:"
echo "  1) Use the SMTP password-reset flow to set the password for ${SENTRY_ADMIN_EMAIL}"
echo "  2) Create an organization + first project in the UI"
echo "  3) Save the DSN somewhere you'll find it later"
echo "  4) Plan upgrades version-by-version: \`cd /opt/self-hosted && git checkout <next-tag> && ./install.sh\`"

Make it executable and run it:

chmod 700 /root/setup-sentry-ubuntu2204.sh
SENTRY_DOMAIN=sentry.example.com SENTRY_EMAIL=ops@example.com \
  SENTRY_ADMIN_EMAIL=admin@example.com \
  SMTP_HOST=smtp.example.com SMTP_PORT=587 \
  SMTP_USER=sentry@example.com SMTP_PASS='secret' \
  SMTP_FROM=sentry@example.com \
  /root/setup-sentry-ubuntu2204.sh

The script prints the listener state, the docker compose ps summary, the health JSON, and the public URL. The admin user is created without a password — request a reset from the login page; the email comes through the SMTP wiring the script just configured, which doubles as the smoke test for issue alerts later. SENTRY_EVENT_RETENTION_DAYS=90 controls ClickHouse partition retention; raise it deliberately, because each project’s effective volume scales linearly with this. Upgrades are version-by-version through install.sh; never skip a tag.

Stack Harbor’s Monitoring & Support practice runs self-hosted Sentry with sized retention, upgrade cadence, and integrated alert routing into the on-call pipeline — book a stack review.