# Install

> From a release of klang-server to a running install with its first admin, in eight steps.

## 1. Get the release

Each release is a tag of the repository. Clone the one you want and go into the kit:

```bash
git clone --branch v0.0.31 --depth 1 https://github.com/klang-gg/klang-server.git
cd klang-server/selfhost
```

The images of each release are on GitHub’s registry, named
`ghcr.io/klang-gg/klang-<name>:<version>` for `api`, `gateway`, `bot-gateway`, `web` and
`admin`, for `linux/amd64` and `linux/arm64`. The other services use the official images of
Caddy, Postgres, Valkey, Silo and LiveKit, each at an exact version. The store (`s3`) adds one
line to Silo’s image, and Compose builds it by itself the first time it starts.

## 2. Create `.env`

```bash
cp .env.example .env
sh init-env.sh
```

`init-env.sh` replaces every `change-me` with a fresh random secret and makes the file readable
only by you. The API, the gateways, Valkey and the S3 setup refuse to start if a `change-me` is
left.

> **`.env` holds the keys of your install.** Never share it or commit it. Keep a copy with your
> [backups](https://docs.klang.gg/self-hosting/backups/): without the same `BETTER_AUTH_SECRET`, `WEBHOOK_TOKEN_KEY` and
> `APP_SIGNING_KEY_KEY`, a restored install signs everyone out and its webhooks and bot keys stop
> working.

## 3. Fill it in

Open `.env` and set at least the four hostnames. Everything else is optional.

| Group         | Variables                                                                                                                                                                                                                                          | What to do                                                                                    |
| ------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------- |
| Hostnames     | `KLANG_DOMAIN`, `KLANG_ADMIN_DOMAIN`, `KLANG_FILES_DOMAIN`, `KLANG_VOICE_DOMAIN`                                                                                                                                                                   | **Required.** The four of [Requirements](https://docs.klang.gg/self-hosting/requirements/#four-hostnames)          |
| Caddy         | `CADDY_GLOBAL_OPTIONS`                                                                                                                                                                                                                             | Recommended: `email you@example.org`, for Let’s Encrypt’s expiry notices                      |
| Caddy         | `HTTP_PORT`, `HTTPS_PORT`                                                                                                                                                                                                                          | Leave them commented for 80 and 443. Other ports are only for trying it out                   |
| Secrets       | `POSTGRES_PASSWORD`, `VALKEY_PASSWORD`, `BETTER_AUTH_SECRET`, `WEBHOOK_TOKEN_KEY`, `APP_SIGNING_KEY_KEY`, `IMAGE_PROXY_KEY`, `LIVEKIT_API_KEY`, `LIVEKIT_API_SECRET`, `S3_ROOT_USER`, `S3_ROOT_PASSWORD`, `S3_API_ACCESS_KEY`, `S3_API_SECRET_KEY` | Filled by `init-env.sh`. Don’t change them once the install is running                        |
| Voice         | `LIVEKIT_USE_EXTERNAL_IP`, `LIVEKIT_NODE_IP`, `LIVEKIT_TCP_PORT`, `LIVEKIT_UDP_PORT`, `LIVEKIT_TURN_PORT`                                                                                                                                          | Usually as they are. See [Voice and TURN](https://docs.klang.gg/self-hosting/voice/)                               |
| Email         | `SMTP_URL`, `EMAIL_FROM`, `RESEND_API_KEY`                                                                                                                                                                                                         | Optional. See [Email](https://docs.klang.gg/self-hosting/email/)                                                   |
| Your install  | `CONTACT_EMAIL`, `TERMS_URL`, `PRIVACY_URL`                                                                                                                                                                                                        | Recommended. See [Configuration](https://docs.klang.gg/self-hosting/configuration/#your-terms-and-contact)         |
| Features      | `BETA_CLOSED`, `DISCOVERY_ENABLED`, `BOTS_ENABLED`, `BOTS_BETA`, `KLIPY_API_KEY`, `IGDB_CLIENT_ID`, `IGDB_CLIENT_SECRET`, `GAMES_CATALOG_URL`                                                                                                      | Optional. See [Configuration](https://docs.klang.gg/self-hosting/configuration/)                                   |
| Limits        | `LIMIT_UPLOAD_MB`, `LIMIT_STORAGE_GB`, `LIMIT_SCREEN_SHARE`, `LIMIT_CAMERA`, `LIMIT_VOICE_KBPS`, `LIMIT_STREAMS_PER_CHANNEL`                                                                                                                       | Optional. Unset, 500 MB per file, 100 GB per person and the best quality                      |
| Against abuse | `ANONYMOUS_SIGNUPS_PER_HOUR`, `GATEWAY_CONNECTIONS_PER_5_MIN`, `SIGNUP_SHARED_IP_MIN_DEVICES`, `SIGNUP_SHARED_IP_WINDOW_DAYS`                                                                                                                      | Optional. See [Configuration](https://docs.klang.gg/self-hosting/configuration/#against-abuse)                     |
| Reverse proxy | `CADDY_TRUSTED_PROXIES`, `TRUST_PROXY_HOPS`                                                                                                                                                                                                        | Only behind your own proxy. See [Behind your own reverse proxy](https://docs.klang.gg/self-hosting/reverse-proxy/) |
| Images        | `KLANG_IMAGES`, `KLANG_VERSION`, `LOG_LEVEL`                                                                                                                                                                                                       | Leave them. `KLANG_VERSION` stays commented: the release you cloned names its images          |

### Who can sign up

By default anyone who reaches the app can create an account. For a private community (friends, a
club, a team), close it before you start: set `BETA_CLOSED=true`, and accounts can only be
created with an invite code. You still create the first account in step 6, with a code from the
command line.

## 4. Get the images

```bash
docker compose pull
```

It pulls this release’s images and the pinned images of the other services. `s3` shows as
skipped, because Compose builds it.

Or build them from the repository instead (slower, see the
[requirements](https://docs.klang.gg/self-hosting/requirements/#the-server)):

```bash
docker compose build
```

## 5. Start it

```bash
docker compose up -d
docker compose ps
```

The first start creates the database and runs the migrations (`migrate`), creates the bucket
(`s3-init`), and only then starts the API and the gateways. `migrate` and `s3-init` exit when
they’re done, so `Exited (0)` is right for them. Every other service should be `running` or
`healthy` within a minute. Caddy asks Let’s Encrypt for the four certificates as the first
requests arrive.

## 6. Register

Open `https://<KLANG_DOMAIN>` and create your account with a username, an email and a password.

With `BETA_CLOSED=true`, sign-up asks for an invite code. Make one for yourself first:

```bash
docker compose exec api node dist/cli/beta.js create 1
```

It prints a code like `K7QPM-2XRTD`, good for one account. Later you make the codes for other
people in the panel, under **Closed beta**.

## 7. Make it the admin

```bash
docker compose exec api node dist/cli/create-admin.js <your username or email>
```

It works once: it makes the account you name the admin (a registered account with a password, not
a guest one) and refuses when there’s already an admin. Other staff members are added from the
panel, which keeps a record of who did it.

## 8. Open the panel

Go to `https://<KLANG_ADMIN_DOMAIN>` and sign in with the same account. The first time, the panel
asks you to set up two-factor authentication with an authenticator app (it asks for your
password first). Every staff action needs it.

Next: [Configuration](https://docs.klang.gg/self-hosting/configuration/), and set up [backups](https://docs.klang.gg/self-hosting/backups/)
before people start using it.
