# SSL and Domain Setup

> Set up a domain and SSL for self-hosted LearnHouse with automatic Let's Encrypt certificates or your own reverse proxy, and set the cookie domain.

Securing your LearnHouse instance with SSL is strongly recommended for production deployments. This guide covers domain configuration and SSL setup options.

Make sure your DNS records point to the server running LearnHouse before enabling SSL.

## Option 1: Automatic SSL with the setup wizard

The setup wizard can handle certificates for you. When you enter a domain other than `localhost`, choose **Automatic SSL (Let's Encrypt via Caddy)** and enter an email address for Let's Encrypt notices.

The generated stack then runs Caddy instead of nginx. Caddy listens on port 80 (for the certificate challenge and redirects) and on the HTTPS port you chose (default 443), obtains a certificate for your domain, and renews it automatically. Certificates are stored in a Docker volume, so they survive restarts.

Both ports must be free on the server and reachable from the internet.

## Option 2: Your own reverse proxy

Choose **I'll handle SSL myself** in the wizard (or pass `--https manual` to `setup --ci`) when TLS ends in a proxy you run (or a service like Cloudflare). LearnHouse then serves plain HTTP on the port you picked, and the generated URLs use `https://`. Point your proxy at that port.

If the proxy runs on the same server, it needs ports 80 and 443 itself, so give LearnHouse another port (for example 8080) and use that port below instead of 80.

### Caddy

```text title="Caddyfile"
learn.example.com {
    reverse_proxy localhost:80
}
```

### Nginx

An example nginx configuration with Let's Encrypt (via Certbot). The `/collab` path carries WebSocket traffic for real-time editing, so the proxy must pass the upgrade headers:

```nginx
server {
    listen 443 ssl;
    server_name learn.example.com;

    ssl_certificate /etc/letsencrypt/live/learn.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/learn.example.com/privkey.pem;

    client_max_body_size 6G;

    location / {
        proxy_pass http://localhost:80;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        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;
    }
}

server {
    listen 80;
    server_name learn.example.com;
    return 301 https://$server_name$request_uri;
}
```

`client_max_body_size 6G` matches the upload limit of the nginx inside the LearnHouse container, so large video uploads are not cut off by your proxy.

### Traefik

If you use Traefik as your reverse proxy, route your domain to the LearnHouse port and let Traefik handle Let's Encrypt certificates through its built-in ACME support.

## Variables to check

The setup wizard writes all of these for you. If you configure SSL by hand, check them in `.env`:

| Variable | Value for HTTPS |
| --- | --- |
| `NEXT_PUBLIC_LEARNHOUSE_HTTPS` | `true` |
| `NEXT_PUBLIC_LEARNHOUSE_API_URL`, `NEXT_PUBLIC_LEARNHOUSE_BACKEND_URL`, `NEXTAUTH_URL` | `https://` URLs |
| `NEXT_PUBLIC_COLLAB_URL` | `wss://learn.example.com/collab` |
| `LEARNHOUSE_SSL` | `true`. The API uses it for links it builds outside a request, for example in background emails. |
| `LEARNHOUSE_FRONTEND_DOMAIN`, `LEARNHOUSE_MEDIA_URL` | Your domain, and `https://learn.example.com`. Links and logos in emails are built from them. |
| `LEARNHOUSE_ALLOWED_REGEXP` | A pattern for your own domain, for example `^https://(?:[a-z0-9-]+\.)*example\.com$`. The default matches any origin. |

> To switch an existing installation from HTTP to HTTPS, or to another domain, re-run `npx learnhouse setup` on the same installation instead of editing the URLs one by one. The wizard regenerates every URL and the proxy configuration, and keeps your data, your secrets and any variables you added to `.env` yourself.

## Cookie Domain

A Community install serves one organization on one domain, so session cookies are host-only and `LEARNHOUSE_COOKIE_DOMAIN` is not used (the API logs a warning if it is set). Older CLI versions wrote it; re-running `setup` removes it.

`LEARNHOUSE_COOKIE_DOMAIN` only matters for Enterprise multi-organization mode, where organizations live on subdomains. There, a parent value such as `.example.com` makes the API log a warning until you set `LEARNHOUSE_COOKIE_DOMAIN_ALLOW_BROAD=true`, and a single-label value such as `.com` stops it from starting.
