---
title: "Deploy NHI Security"
description: "The services NHI Security runs as, the two sign-in modes that shape it, the Docker Compose and Kubernetes files that ship, backups, and what is unproven."
keywords: ["self-hosting NHI Security", "AUTH_MODE", "docker compose", "kubernetes", "backup", "first operator", "deployment status"]
---

NHI Security is a separate set of services from TRUSTIVAN AppSec: its own API, its own console and its own PostgreSQL database. You deploy it in one of two shapes. **Beside AppSec**, it has no hostname of its own: the AppSec dashboard proxies `/nhi` to it and TRUSTIVAN signs people in. **Standalone**, it brings its own TLS edge and its own password sign-in.

<Warning>
**NHI Security has not been deployed to production.** The deployment files on this page ship in the repository under `nhi/deploy/`. The standalone Compose shape has been run only in local containers and the Kubernetes manifests only against a local k3s cluster. No certificate has been issued through the shipped edge configuration, and `deploy.sh` has not been run on a remote host. Treat every standalone procedure here as unproven until you have run it yourself, and read [what is not proven](#what-is-not-proven) first.
</Warning>

## What you run

| Service | What it is | Listens on | Networks |
|---|---|---|---|
| `postgres` | PostgreSQL 16 with data checksums. An init script creates the two application logins | `5432`, never published | `data` |
| `migrate` | A one-shot container that applies migrations as the database owner, under a PostgreSQL advisory lock, then exits | none | `data` |
| `api` | The NHI Security API | `4000`, never published | `data`, `internal` |
| `web` | The console, served under the base path `/nhi` | `3100`, never published | `internal`, `edge` |
| `caddy` | The TLS edge. Standalone shape only | `80`, `443` | `edge` |

The browser only ever talks to the console's origin. The console forwards `/nhi/api/*` to the API's `/api/*`, so the session cookie stays first-party and the API needs no public address. That is why these docs spell one route two ways: `/api/v1/findings` is the route as the API serves it, and `/nhi/api/v1/findings` is the same route as a browser or a script reaches it through the console's origin. `internal` and `data` are internal Docker networks: the edge cannot reach the API or the database, and the console cannot reach the database.

The `api` and `web` containers run with a read-only root filesystem, `no-new-privileges`, and memory, CPU and process limits. The defaults suit a host with 4 vCPU and 8 GB of memory; the variables that change them are in the [configuration reference](/nhi-security/configuration#resource-limits).

### Three database connections

NHI Security uses three PostgreSQL roles, and the split is what makes organization isolation enforceable:

| Role | Used by | Why |
|---|---|---|
| The owner (`POSTGRES_OWNER_USER`, default `angusham_owner`) | `migrate` only | Creates roles, policies and functions. The API never receives this credential, because an owner bypasses row-level security |
| `angusham_app_login` | The API, for every organization-scoped request | `NOSUPERUSER`, `NOBYPASSRLS`, so row-level security applies to it |
| `angusham_platform_login` | The API, for [platform operator](/nhi-security/platform-operators) requests | Holds no privileges on organization tables |

## Choose a sign-in mode

`AUTH_MODE` decides who signs people in. Set the same value on **both** the `api` and the `web` service: the API needs it to authenticate requests, and the console needs it to know where to send someone with no session.

| | `AUTH_MODE=trustivan` | `AUTH_MODE=local` (the default) |
|---|---|---|
| Who signs people in | TRUSTIVAN AppSec | NHI Security's own password sessions |
| How a request is authenticated | The API forwards the caller's `trustivan_session` cookie to `TRUSTIVAN_API_ORIGIN` at `GET /api/v1/me/context` and acts on the answer | A session cookie NHI Security issued itself |
| Users, invitations, roles | Managed in AppSec. See [Administration](/nhi-security/administration) | Managed in the NHI Security console |
| NHI Security's sign-in, sign-out, password and session-list routes | Answer `404` | Available |
| Operator console at `/nhi/platform` | Not served | Served |
| Required settings | `TRUSTIVAN_API_ORIGIN` on the API. The API refuses to start without it | None beyond the base set |

In `trustivan` mode a state-changing request must carry an `X-CSRF-Token` header equal to the `trustivan_csrf` cookie, and must come from an origin listed in `CORS_ALLOWED_ORIGINS`. If TRUSTIVAN cannot be reached, errors or times out, the request is refused. An earlier answer is never reused in its place. [Overview](/nhi-security/overview#how-it-relates-to-trustivan-appsec) describes what the user experiences.

## Run it beside TRUSTIVAN AppSec

This is the shape `https://app.trustivan.com/nhi` describes: one origin, one session, two consoles. It uses the same Compose file as the standalone shape with an overlay, `infra/docker/nhi.override.yml`, that changes three things: the `api` and `web` services join the Docker network the AppSec containers share (`trustivan-public`, which must already exist), both are put into `trustivan` mode, and scheduled discovery is turned off.

<Steps>
  <Step title="Point the dashboard at the NHI Security console">
    The AppSec dashboard proxies `/nhi` and everything under it to the address in `NHI_WEB_URL`. Left empty (the default), the dashboard is built without NHI Security: it hides the NHI Security switch and `/nhi` says NHI Security is not enabled on this deployment. The value is read when the dashboard is **built**, so changing it means rebuilding the dashboard image, not restarting it.
  </Step>
  <Step title="Write the environment file">
    Copy `nhi/deploy/.env.production.example`, fill it in as described in the [configuration reference](/nhi-security/configuration), and add the overlay's own variables:

    ```bash
    # Where the NHI Security API reaches the TRUSTIVAN API: its container name on the
    # shared network, as an origin only, with no path.
    NHI_IDP_ORIGIN=http://backend:8080
    # Network aliases for this stack's API and console. Unique per stack on the shared network.
    NHI_API_ALIAS=nhi-app-api
    NHI_WEB_ALIAS=nhi-app-web
    ```

    Set `PUBLIC_ORIGIN` to the address people open the dashboard at. The API accepts state-changing requests only from that origin. `NHI_WEB_URL` in the previous step must name the console by `NHI_WEB_ALIAS` and port `3100`.
  </Step>
  <Step title="Build the images from the repository root">
    Both Dockerfiles (`nhi/deploy/docker/api.Dockerfile`, `nhi/deploy/docker/web.Dockerfile`) expect the repository root as their build context, which is how the Compose file builds them.
  </Step>
  <Step title="Start four services by name">
    ```bash
    docker compose \
      -f nhi/deploy/docker-compose.prod.yml \
      -f infra/docker/nhi.override.yml \
      --env-file /path/to/nhi.env -p trustivan-nhi \
      up -d postgres migrate api web
    ```

    Name the services. A bare `up -d` also starts `caddy`, which publishes ports `80` and `443` and collides with whatever already terminates TLS for the dashboard.
  </Step>
</Steps>

<Note>
If you run two NHI Security stacks on one Docker network, never address either by its Compose service name. Both register `api` and `web`, and Docker's DNS returns either one. That is what the two aliases are for.
</Note>

<Warning>
If signed-in people see an error on `/nhi` and `GET /nhi/api/v1/auth/session` answers `503` with `reason: identity_provider_unavailable`, NHI Security cannot reach `TRUSTIVAN_API_ORIGIN`, usually because a redeploy took the AppSec API container off the shared Docker network. Every container still reports healthy. Reconnect it with `docker network connect`. `infra/scripts/smoke.sh` fails a deploy in this state, and `infra/scripts/edge-reconcile.sh` reattaches the container if you run it on a timer.
</Warning>

For evaluation on one machine, the repository's root `docker-compose.yml` already includes the four NHI Security services in `trustivan` mode (`make docker-up`), with the API on `127.0.0.1:4000` and the console on `127.0.0.1:3100`. See [Install with Docker Compose](/installation/docker-compose) for the AppSec side.

## Run it standalone

Standalone, NHI Security serves `https://<your domain>/nhi` from its own Caddy edge and signs people in itself.

<Steps>
  <Step title="Prepare the host">
    Install Docker with the Compose plugin. Point your domain at the host's public address **before** the first start: Caddy proves control of the domain to the certificate authority on ports `80` and `443`, and a domain that does not resolve yet fails issuance.
  </Step>
  <Step title="Write the environment file">
    ```bash
    cp nhi/deploy/.env.production.example nhi/deploy/.env.production
    chmod 600 nhi/deploy/.env.production
    openssl rand -base64 32   # CREDENTIAL_ENCRYPTION_KEY
    ```

    Fill in `PUBLIC_DOMAIN`, `PUBLIC_ORIGIN`, `TLS_EMAIL`, the three database passwords and `CREDENTIAL_ENCRYPTION_KEY`. `APP_ENV` must be `production` or `staging`. Every variable is described in the [configuration reference](/nhi-security/configuration).
  </Step>
  <Step title="Deploy">
    ```bash
    cd nhi && make deploy
    ```

    `deploy/scripts/deploy.sh` checks the environment file, builds both images from the working tree, starts PostgreSQL, applies migrations, starts the API, then the console, then Caddy, and waits until `GET /api/v1/readyz` reports `ready`. If migrations fail, nothing is restarted and the running version is untouched. `--no-build` starts images that are already built or pulled.
  </Step>
  <Step title="Create the first platform operator">
    ```bash
    make bootstrap-admin EMAIL=ops@example.com NAME="Platform Operator"
    ```

    The command prints a generated password once and stores only its verifier. It refuses to run once any operator exists. The operator then provisions your organization and invites its first administrator: see [Platform operators](/nhi-security/platform-operators).
  </Step>
</Steps>

<Warning>
`deploy/caddy/Caddyfile` still health-checks the console at `/sign-in`. The console serves its pages under `/nhi`, and its own container health check asks for `/nhi/platform/sign-in`. Confirm that the edge reports the console healthy on your build, and correct `health_uri` if it does not, before you rely on this shape.
</Warning>

### Kubernetes

`nhi/deploy/kubernetes/` holds Kustomize manifests for the same topology: a base (namespace, API, console, Caddy, a migration Job and default-deny NetworkPolicies), an in-cluster PostgreSQL component with a backup CronJob, and `local` and `production` overlays. `make k8s-validate` renders every overlay offline and holds it to the deployment's invariants, and `make k8s-deploy OVERLAY=production` validates, applies, waits for migrations and verifies from inside the Pods.

Two constraints are enforced by that validation rather than left to convention: the production overlay must name both images by digest (a tag or `:latest` is refused), and the API must run exactly one replica. There is no Helm chart, and no Kubernetes equivalent of `restore.sh`.

## Health checks

Both routes are unauthenticated, so an orchestrator can call them.

| Route | Answers |
|---|---|
| `GET /api/v1/healthz` | `200` with `{"status":"ok","uptimeSeconds":...}` while the process is running |
| `GET /api/v1/readyz` | `200` with `"status":"ready"` when the database answers, `503` with `"status":"not_ready"` when it does not |

## Back up and restore

The scripts in `nhi/deploy/scripts/` read `deploy/.env.production` themselves. Each has a `make` target.

| Command | What it does |
|---|---|
| `make backup` | `pg_dump` in custom format to `deploy/backups`, verified readable, with a minimum-size check, a free-space check and retention (14 days by default) |
| `make backup-check` | Fails when the last successful backup is older than expected (26 hours by default) |
| `make restore-rehearsal` | Restores a dump into a throwaway database beside the live one, verifies it and drops it |
| `make restore DUMP=<file>` | Replaces the database from a dump, after you type the database name to confirm and after a safety dump of what is about to be replaced |

Three things to plan for:

- **Nothing schedules any of this.** No cron entry or timer ships. Until you install one, no backup is taken and nothing notices.
- **A backup is plaintext unless you set `BACKUP_PUBLIC_KEY`.** It is an [age](https://github.com/FiloSottile/age) recipient (a public key). When set, `backup.sh` encrypts the dump, checks the result and removes the plaintext; the host then holds nothing that can read its own backups. When unset, the script says so on every run. Restoring an encrypted dump needs `BACKUP_IDENTITY_FILE`.
- **A dump does not contain `CREDENTIAL_ENCRYPTION_KEY`.** Stored connector credentials are in the dump as ciphertext. Back the key up separately, somewhere the database backup is not. A restore onto a host with a different key completes, and every stored credential in it is unreadable.

## What is not proven

- **No standalone deployment has run on a public host.** Certificate issuance, the HSTS edge and `deploy.sh` on a remote machine are untested.
- **The Kubernetes path has run against one local, single-node k3s cluster** and no other.
- **`deploy.sh` builds from the working tree.** Running a published image through it is a manual procedure: set `IMAGE_API` and `IMAGE_WEB`, pull, then run with `--no-build`. That procedure has not been exercised.
- **`postgres:16-alpine` and `caddy:2-alpine` are not pinned** in the Compose file, and `deploy.sh` never pulls them.
- **Remediation execution cannot be switched on from the environment file.** `docker-compose.prod.yml` does not pass `REMEDIATION_EXECUTION_ENABLED` to the API, so it stays off unless you edit the Compose file as well. See [Remediation](/nhi-security/remediation#execution-is-off-by-default).
- **The API runs as one replica.** The rate limiter counts per process, so a second replica would double every client's ceiling, and scheduled discovery runs inside the same process. See [Discovery and synchronization](/nhi-security/discovery#scheduled-discovery).

## Next steps

<Columns cols={2}>
  <Card title="Configuration reference" icon="sliders-horizontal" href="/nhi-security/configuration">
    Every environment variable, its default, and what refuses to start.
  </Card>
  <Card title="Platform operators" icon="user-cog" href="/nhi-security/platform-operators">
    Create the first operator and provision an organization.
  </Card>
  <Card title="Administration" icon="users" href="/nhi-security/administration">
    Users, teams, roles and organization settings.
  </Card>
  <Card title="Limitations" icon="triangle-alert" href="/nhi-security/limitations">
    What NHI Security does not do today.
  </Card>
</Columns>
