Skip to content

Installing Grafana on Ubuntu with the auth, datasource, and reverse-proxy setup you want from day one

Install Grafana from the official APT repository on Ubuntu 24.04 with a Postgres-backed database, reverse proxy on Nginx, and OAuth wired so dashboards survive a node rebuild.

The Grafana install most teams ship is the one from the homepage tutorial — apt-install, default SQLite database in /var/lib/grafana/grafana.db, admin password “admin,” and a bind on 0.0.0.0:3000. Six months later a node rebuild loses every dashboard, and the lone admin account turns out to be shared. This article walks through a Grafana install on Ubuntu 24.04 that puts the database somewhere durable, fronts the binary with Nginx, and ties login to your existing identity provider — the shape we use in client engagements.

How to verify

After the install, all of the below should pass.

systemctl status grafana-server --no-pager
ss -lntp | grep 3000
curl -fsS http://127.0.0.1:3000/api/health | jq
sudo -u grafana psql -h 127.0.0.1 -U grafana -d grafana -c '\dt' 2>&1 | head -10
journalctl -u grafana-server --since "10 min ago" | grep -Ei 'err|fail' || echo 'clean'
curl -sI https://grafana.example.com/login | head -5

/api/health returns {"database": "ok", "version": "<x>"}. If database says anything else, the Postgres connection failed — the server still starts but every dashboard write will error.

What’s happening

Grafana is a Go web app that renders dashboards from datasources. The thing that breaks most installs is the storage layer: by default Grafana writes dashboards, users, datasources, and folders to a single SQLite file under /var/lib/grafana. SQLite is fine for one operator on one node; it falls over the moment you want HA, an immutable host image, or just a backup story that does not involve copying a binary file out of a running directory. Pointing Grafana at a Postgres or MySQL backend solves all three problems: the database is somewhere a backup job already covers, multiple Grafana nodes can share state, and you can rebuild the host without losing dashboards.

The other choice that matters is how users log in. The default admin account is fine for the install hour, but every additional account should come from your IdP — Google, GitHub, Microsoft, Authentik, Keycloak — through the auth.generic_oauth block. Once that is wired, no one ever creates a local Grafana user again.

The procedure

  1. Add the official APT repository and install. Grafana publishes its own repo; the version in Ubuntu’s universe is out of date.

    sudo mkdir -p /etc/apt/keyrings
    wget -q -O - https://apt.grafana.com/gpg.key | gpg --dearmor | sudo tee /etc/apt/keyrings/grafana.gpg >/dev/null
    echo "deb [signed-by=/etc/apt/keyrings/grafana.gpg] https://apt.grafana.com stable main" | \
      sudo tee /etc/apt/sources.list.d/grafana.list
    sudo apt update
    sudo apt install -y grafana
  2. Provision a Postgres database for Grafana. Either on the same host or, more commonly, on a managed Postgres you already operate.

    -- as a Postgres superuser
    CREATE USER grafana WITH PASSWORD 'replace-me-with-a-real-secret';
    CREATE DATABASE grafana OWNER grafana;
  3. Configure Grafana to use it. Edit /etc/grafana/grafana.ini — only the sections you need to touch.

    # /etc/grafana/grafana.ini
    [server]
    protocol = http
    http_addr = 127.0.0.1
    http_port = 3000
    domain = grafana.example.com
    root_url = https://grafana.example.com/
    
    [database]
    type = postgres
    host = 127.0.0.1:5432
    name = grafana
    user = grafana
    password = replace-me-with-a-real-secret
    ssl_mode = require
    
    [security]
    admin_user = stackharbor-bootstrap
    admin_password = $__file{/etc/grafana/admin_password}
    cookie_secure = true
    strict_transport_security = true
    content_security_policy = true
    
    [users]
    allow_sign_up = false
    auto_assign_org_role = Viewer
    
    [auth.anonymous]
    enabled = false
    
    [auth.generic_oauth]
    enabled = true
    name = Authentik
    client_id = grafana
    client_secret = $__file{/etc/grafana/oauth_secret}
    scopes = openid profile email groups
    auth_url = https://idp.example.com/application/o/authorize/
    token_url = https://idp.example.com/application/o/token/
    api_url = https://idp.example.com/application/o/userinfo/
    role_attribute_path = contains(groups[*], 'grafana-admins') && 'GrafanaAdmin' || contains(groups[*], 'grafana-editors') && 'Editor' || 'Viewer'
    allow_assign_grafana_admin = true

    The $__file{...} syntax reads the secret from a file at startup so the value is never visible in ps output. Put the secrets in /etc/grafana/admin_password and /etc/grafana/oauth_secret, both chmod 0600, owner grafana:grafana.

  4. Front it with Nginx + TLS. Grafana stays on 127.0.0.1:3000; the proxy holds the certificate.

    server {
      listen 443 ssl http2;
      server_name grafana.example.com;
      ssl_certificate     /etc/letsencrypt/live/grafana.example.com/fullchain.pem;
      ssl_certificate_key /etc/letsencrypt/live/grafana.example.com/privkey.pem;
      location / {
        proxy_pass http://127.0.0.1:3000;
        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;
      }
      # Grafana Live (websockets)
      location /api/live/ {
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_pass http://127.0.0.1:3000;
      }
    }

    Without the websocket Upgrade headers on /api/live/, the dashboard stream silently degrades to long-polling and live panels stall.

  5. Start, enable, and add datasources via the UI or provisioning. For Prometheus:

    # /etc/grafana/provisioning/datasources/prometheus.yaml
    apiVersion: 1
    datasources:
      - name: Prometheus
        type: prometheus
        url: http://127.0.0.1:9090
        access: proxy
        isDefault: true
        editable: false

    File-based provisioning means a host rebuild gives you the datasources back automatically; UI-added datasources live in the database and depend on the Postgres backup.

  6. Disable local sign-ups and remove the bootstrap admin. Once a real admin has logged in through OAuth, the stackharbor-bootstrap account in step 3 can be disabled — but leave it as a break-glass account documented in your runbook.

Common pitfalls

  • Leaving the default SQLite database and assuming nightly host backups capture it; rebuilds without the Postgres step lose every dashboard.
  • Putting secrets in grafana.ini instead of $__file{...} references; the file is world-readable in some packaging defaults.
  • Forgetting the websocket location block; Grafana Live and the alert preview stream both fail silently.
  • Setting root_url to http://... while terminating TLS in Nginx; cookie flags and redirect URLs end up wrong and OAuth login loops.
  • Letting Grafana create local users for everyone “just in case OAuth breaks”; you end up with shared accounts and stale roles.

For dashboards built against Prometheus, see grafana-dashboards-scratch. For the alerting that fires off these dashboards, see prometheus-alerting-rules.


Stack Harbor installs Grafana this way as part of the managed operations stack we run for clients — durable backend, IdP-tied login, provisioned datasources, and a configuration that survives a node rebuild without manual recovery.