# Set up a Tessh sync server (instructions for an AI agent)

You are helping the user run their own Tessh sync server on the machine you have a shell on.
Tessh is an SSH client; this server syncs the user's encrypted vault between their devices.
The server only stores encrypted data. It runs as one Docker container with one SQLite file.

Work through the steps in order. Ask the user before you install software, change an existing
web server or reverse proxy, open firewall ports, or touch anything outside the folder you create.
Explain each step in one line as you go.

## 1. Check the machine

- Confirm Docker and Docker Compose v2 work: `docker compose version`.
  If they're missing, tell the user and ask before installing Docker.
- Check that port 8080 is free: `ss -ltn | grep :8080` (Linux) or `lsof -iTCP:8080 -sTCP:LISTEN` (macOS).
  If it's taken, change the left side of the port mapping in step 2 (for example `"8081:8080"`)
  and use that port everywhere below.
- Ask the user which HTTPS address the server should have, for example `https://sync.example.com`.
  The Tessh apps need HTTPS. If they have no domain but use Tailscale, `tailscale serve` can provide
  an HTTPS address on their tailnet.

## 2. Install

```sh
mkdir -p ~/tessh-server && cd ~/tessh-server
curl -fsSLO https://tessh.dev/self-host/compose.yml
```

Edit `compose.yml`:

- Uncomment `environment:` and set `PUBLIC_URL` to the HTTPS address from step 1.
  The setup link the server prints then uses that address.
- If a reverse proxy will run on this same machine, change the port to `"127.0.0.1:8080:8080"`
  so the server is only reachable through the proxy.

Leave everything else as it is. Then start it:

```sh
docker compose up -d
```

## 3. Add HTTPS

Put a reverse proxy in front of port 8080. It must forward `X-Forwarded-Proto` and
`X-Forwarded-Host`, and serve the server from the root of a domain or subdomain, not under a path.

- **No proxy yet:** Caddy is the simplest. It gets and renews the certificate itself:

  ```caddyfile
  sync.example.com {
  	reverse_proxy 127.0.0.1:8080
  }
  ```

  The domain's DNS must point at this machine and ports 80 and 443 must be reachable.
- **Existing Caddy, nginx, Traefik or similar:** ask the user, then add a site for the address
  that proxies to port 8080. The nginx example is in
  https://github.com/tessh-dev/tessh-server/blob/main/docs/install.md
- **Tailscale only:** `tailscale serve --bg 8080` gives `https://<machine>.<tailnet>.ts.net`.
  Use that as `PUBLIC_URL`.

Check it: `curl -fsS https://<address>/v1/health` should answer.

## 4. Hand over the setup link

```sh
docker compose logs tessh-server
```

The log prints a setup link (`https://<address>/start#XXXXXXXX`) and the same setup code for
typing into the Tessh app. Give the user both, exactly as printed. They work once, for 24 hours.

Tell the user:

1. Open the link and press **Open in Tessh**, or scan the QR code with their phone.
   No Tessh app yet? Download it from https://tessh.dev/download
2. Create their Tessh account in the app. That account owns the server.
3. The same email and master password sign in to the admin page at `https://<address>/admin`.

Don't try to open the link or create the account yourself: the account's master password is
the user's and should never pass through you.

## If something goes wrong

- Log: `docker compose logs tessh-server`
- The setup link expired: `docker compose restart tessh-server` prints a new one.
- Updating later: `docker compose pull && docker compose up -d`. Data stays in the `tessh-data` volume.
- Full docs: https://github.com/tessh-dev/tessh-server
