# Install

Install Cohorly on a Linux host with Docker Compose: download the deployment files, fill in .env, start the stack, check that it is up, create the first account and send the first event.

You need the host and domain from [Requirements and sizing](/self-hosting/requirements),
and the images and license key from [Images and license key](/self-hosting/images).

## 1. Download the deployment files

Make a directory for the instance and put four files in it. Pick the directory
name with care: Docker Compose uses it as the project name, which prefixes the
names of your containers and volumes (`cohorly_selfhost-pgdata` for a directory
called `cohorly`).

```bash
mkdir /opt/cohorly && cd /opt/cohorly
```

| Download                                                                     | Save as                      | What it is                                                                                              |
| ---------------------------------------------------------------------------- | ---------------------------- | ------------------------------------------------------------------------------------------------------- |
| [docker-compose.yml](/self-hosting/files/docker-compose.yml)                 | `docker-compose.yml`         | The stack: PostgreSQL, API, error worker, dashboard, Caddy.                                             |
| [Caddyfile.txt](/self-hosting/files/Caddyfile.txt)                           | `Caddyfile`                  | Which paths go to the API and which to the dashboard.                                                   |
| [env.example](/self-hosting/files/env.example)                               | `.env`                       | Your settings and secrets.                                                                              |
| [docker-compose.offline.yml](/self-hosting/files/docker-compose.offline.yml) | `docker-compose.offline.yml` | Optional, for [checking that nothing leaves your network](/self-hosting/network#check-it-on-your-host). |

Save `Caddyfile.txt` as `Caddyfile` and `env.example` as `.env`. The
`docker-compose.yml` file also lists `build` sections that point into Cohorly's
source tree. Ignore them: you never build, and every command in this guide
passes `--no-build`.

## 2. Fill in `.env`

Open `.env`. These values have no default and the stack refuses to start
without them:

| Variable                 | What to put                                                                        |
| ------------------------ | ---------------------------------------------------------------------------------- |
| `COHORLY_DOMAIN`         | The domain you pointed at the host, for example `cohorly.example.com`.             |
| `POSTGRES_PASSWORD`      | A long random password: `openssl rand -hex 24`.                                    |
| `COHORLY_API_KEY`        | The admin key of the instance: `openssl rand -hex 32`.                             |
| `BETTER_AUTH_SECRET`     | `openssl rand -base64 32`. Changing it later means clearing the `auth_jwks` table. |
| `COHORLY_MCP_JWT_SECRET` | `openssl rand -base64 32`.                                                         |

Also set, from your contract:

- `COHORLY_LICENSE_KEY` (or `COHORLY_LICENSE_KEY_FILE`): your
  [license key](/self-hosting/images#license-key).
- `COHORLY_SERVER_IMAGE`, `COHORLY_DASHBOARD_IMAGE` and `COHORLY_IMAGE_TAG`:
  the images and version, as described in
  [Getting the images](/self-hosting/images#getting-the-images).

We recommend two more, so the API and the worker run without superuser rights
in the database: `COHORLY_DATABASE_MIGRATION_URL` and `COHORLY_DATABASE_URL`,
both `postgres://<role>:<password>@postgres:5432/cohorly`, with the role
`cohorly_owner` in the first and `cohorly_app` in the second, and each
password from its own `openssl rand -hex 24`. See
[Operator settings](/self-hosting/configuration#settings-in-env).

And pick the PostgreSQL profile that fits your host
([PostgreSQL settings](/self-hosting/requirements#postgresql-settings)). The
default is the 2 GB `small` profile.

`.env` holds secrets. Make it readable by you only: `chmod 600 .env`.

Leave every other value as it is for now. [Operator settings](/self-hosting/configuration)
explains them, and you can change any of them later.

## 3. Start the stack

Get the images onto the host if you have not (`docker login` and
`docker compose pull server error-worker dashboard` for a registry, `docker load` for the offline package),
then start:

```bash
docker compose up -d --no-build
```

The first start takes a minute or two. The services start in order: PostgreSQL,
the API (which creates the database schema), then the dashboard, the error
worker and the proxy. Check them:

```bash
docker compose ps
```

All five services (`postgres`, `server`, `error-worker`, `dashboard`, `caddy`)
should be `Up` and `healthy`.

If `server` keeps restarting, read why:

```bash
docker compose logs server
```

A license problem prints one of the lines listed in
[License key](/self-hosting/images#license-key). Compose also refuses to start
and names the missing variable when one of the required values in `.env` is
empty.

## 4. Check that it is up

```bash
curl https://cohorly.example.com/health
```

```json
{"ok":true,"db":true}
```

`db: true` means the API reached PostgreSQL. Then open
`https://cohorly.example.com` in a browser: you should see the sign-in page,
served with a valid certificate.

### What Caddy does

Caddy is the only service that publishes ports (80 and 443). It:

- Gets a certificate from Let's Encrypt for `COHORLY_DOMAIN` when the container
  starts, renews it, and redirects HTTP to HTTPS. If the certificate does not
  arrive, check that the `A` record points at this host, that ports 80 and 443
  are open, and `docker compose logs caddy`.
- Sends the API paths (`/track`, `/engage`, `/alias`, `/replay`, `/health`,
  `/mcp`, `/flags/*`, `/signup/validate-invite`, `/api/*` and
  `/.well-known/*`) to the API, and everything else to the dashboard. The
  dashboard calls the API on the same domain, so there is no API address to set
  and no rebuild when the domain changes.
- Does not route `/admin`, `/contact` or `/status`, so the admin API, the
  contact form and the status endpoints are not reachable from the internet.
- Is the only edge: it overwrites `X-Forwarded-For` with the address it saw, so
  per-client rate limits (for example 10 sign-ins a minute per address) key on
  the real client. Do not put another proxy that passes client headers through
  in front of it without understanding what that does to those limits.

If ports 80 and 443 are taken on the host, set `COHORLY_HTTP_PORT`,
`COHORLY_HTTPS_PORT` and `COHORLY_PUBLIC_PORT` in `.env` (the last one is the
port in the public URL). The redirect from HTTP to HTTPS does not carry a custom
port, so open the `https` address with its port. Let's Encrypt still reaches the
instance on the public ports 80 and 443, so something in front of the host has
to forward them to the ports you chose.

## First access

The stack sets `COHORLY_DEPLOYMENT_MODE=self-host`. Who can create the first
account depends on `COHORLY_BOOTSTRAP_ADMIN_EMAIL`.

### With `COHORLY_BOOTSTRAP_ADMIN_EMAIL` empty (the default)

The first account that signs up creates the organization with no invite code
and becomes its owner. Everyone after needs an invite code or an organization
invite.

Until that first owner exists, anyone who can reach the instance can take it.
Open `https://cohorly.example.com/signup` and create your account right after
the first start, before you share the address.

1. Open `/signup` and create your account.
2. Follow the setup: name your organization, create a project, and copy the
   project token it shows.

### With `COHORLY_BOOTSTRAP_ADMIN_EMAIL` set

Setting it closes the codeless path for everyone, so nobody can create the
first organization by simply signing up. The first organization then needs an
invite code, which you mint on the host. `/admin` is not routed by the proxy,
so run this inside the API container:

```bash
docker compose exec -T server node -e "fetch('http://server:4000/admin/invites',{method:'POST',headers:{authorization:'Bearer '+process.env.COHORLY_API_KEY,'content-type':'application/json'},body:JSON.stringify({maxUses:1})}).then(r=>r.json()).then(j=>console.log(j.code))"
```

It prints an eight-character code that can be used once. Give it to the person
who will own the instance: they sign up, and enter the code in the setup.

The address in `COHORLY_BOOTSTRAP_ADMIN_EMAIL` becomes owner of the
bootstrap organization, the one with the lowest id, when that organization has
no owner yet and the address signs in with its e-mail verified. Without SMTP,
the verification link is only in the server log
([E-mail](/self-hosting/configuration#e-mail-smtp)). On a new instance there is
no such organization, so the invite code is how the first owner gets in.

## Send the first event

Copy the project token from the setup (it is also in the project settings), and
send an event to your own domain:

```bash
curl -X POST https://cohorly.example.com/track \
  -H "Content-Type: application/json" \
  -H "X-Cohorly-Token: <project-token>" \
  -d '{"event": "Signed Up", "properties": {"distinct_id": "user_123", "plan": "pro"}}'
```

The answer is `{"status":1,"inserted":1}`. In the setup, **Check event stream**
confirms it, and the event appears under **Events** in the dashboard.

From here, instrument your apps with an [SDK](/sdks/web), or send events
directly with the [HTTP API](/api/ingestion), using your own domain instead of
the one in those examples.
