> For the complete documentation index, see [llms.txt](https://adarshsr.gitbook.io/writeups/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://adarshsr.gitbook.io/writeups/walk-through/ctfd-setup-guide-docker-and-https.md). Agents can also search and read this documentation through its [MCP server](https://adarshsr.gitbook.io/writeups/~gitbook/mcp).

# CTFd Setup Guide (Docker & HTTPS)

Fresh Ubuntu server to a live CTFd instance with HTTPS. Replace ctf.example.com and you\@example.com with your real domain and email everywhere below.

### 1. Remove old Docker packages

```bash
for pkg in docker.io docker-doc docker-compose podman-docker containerd runc; do sudo apt-get remove -y $pkg; done
```

### 2. Install Docker

```bash
# add Docker's GPG key
sudo apt-get update
sudo apt-get install -y ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

# add Docker's apt repo
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update

# install docker
sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

# check it works
sudo docker run hello-world
```

### 3. Run Docker without sudo

```bash
sudo groupadd docker 2>/dev/null
sudo usermod -aG docker $USER
newgrp docker
docker run hello-world
```

### 4. Clone CTFd and set a real SECRET\_KEY

Clone the repo first, but don't start it yet — set a proper secret key so you only need to start the containers once.

```bash
git clone https://github.com/CTFd/CTFd.git
cd CTFd
python3 -c "import secrets; print(secrets.token_hex(32))"
```

The default `docker-compose.yml` ships a placeholder secret key. Replace it — weak key = forgeable sessions/cookies. Copy the output above, open the file, paste it as the `SECRET_KEY` value under `services.ctfd.environment`:

```bash
nano docker-compose.yml
```

### 5. Start CTFd

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

Check `http://<server-ip>` in a browser — CTFd setup wizard should load. 4 containers should be running: `ctfd`, `db`, `cache`, `nginx`.

### 6. Point your domain at the server

In your DNS provider (not on the server), add:

| Type | Name            | Value                   |
| ---- | --------------- | ----------------------- |
| A    | ctf.example.com | your server's public IP |

Confirm it resolves:

```bash
dig +short ctf.example.com
```

If using Cloudflare: keep the record **DNS-only (grey cloud)** until after step 7 — the cert challenge needs to reach your server directly.

### 7. Get an HTTPS certificate

```bash
sudo apt-get install -y certbot
docker compose stop nginx
sudo certbot certonly --standalone -d ctf.example.com --agree-tos -m you@example.com --preferred-challenges http --non-interactive
```

Cert saved to `/etc/letsencrypt/live/ctf.example.com/`. Certbot auto-renews it going forward.

### 8. Turn on HTTPS in nginx

Replace `conf/nginx/http.conf`:

```bash
cat > conf/nginx/http.conf <<'EOF'
worker_processes 4;
events { worker_connections 1024; }
http {
  upstream app_servers { server ctfd:8000; }

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

  server {
    listen 443 ssl;
    server_name ctf.example.com;
    ssl_certificate     /etc/letsencrypt/live/ctf.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/ctf.example.com/privkey.pem;

    gzip on;
    client_max_body_size 4G;

    location /events {
      proxy_pass http://app_servers;
      proxy_set_header Connection '';
      proxy_http_version 1.1;
      chunked_transfer_encoding off;
      proxy_buffering off;
      proxy_cache off;
      proxy_redirect off;
      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-Host $server_name;
      proxy_set_header X-Forwarded-Proto https;
      proxy_set_header X-Forwarded-Prefix "";
    }

    location / {
      proxy_pass http://app_servers;
      proxy_redirect off;
      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-Host $server_name;
      proxy_set_header X-Forwarded-Proto https;
      proxy_set_header X-Forwarded-Prefix "";
    }
  }
}
EOF
```

Then update `docker-compose.yml` so nginx exposes port 443 and can read the cert files:

```bash
python3 - <<'EOF'
p = 'docker-compose.yml'
s = open(p).read()
old = '''  nginx:
    image: nginx:stable
    restart: always
    volumes:
      - ./conf/nginx/http.conf:/etc/nginx/nginx.conf
    ports:
      - 80:80
    depends_on:
      - ctfd'''
new = '''  nginx:
    image: nginx:stable
    restart: always
    volumes:
      - ./conf/nginx/http.conf:/etc/nginx/nginx.conf
      - /etc/letsencrypt:/etc/letsencrypt:ro
    ports:
      - 80:80
      - 443:443
    depends_on:
      - ctfd'''
assert old in s, "nginx block didn't match, edit docker-compose.yml manually"
open(p, 'w').write(s.replace(old, new))
print('patched')
EOF
```

### 9. Restart and verify

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

curl -I http://ctf.example.com    # should redirect (301) to https
curl -I https://ctf.example.com   # should return 200
```

CTFd is now live at `https://ctf.example.com`.

### 10. Troubleshooting

| Problem                 | Likely cause                           | Fix                                                              |
| ----------------------- | -------------------------------------- | ---------------------------------------------------------------- |
| Cloudflare shows 521    | Proxy on, no cert on origin yet        | Finish steps 7-8, or set Cloudflare SSL mode to Flexible         |
| Certbot challenge fails | DNS not propagated, or port 80 blocked | `dig +short ctf.example.com`, check security group allows 80/443 |
| nginx won't start       | Wrong cert path in `http.conf`         | `docker compose logs nginx`                                      |
| Cert about to expire    | Renewal timer broken                   | `systemctl status certbot.timer`                                 |

Manual renew + reload if needed:

```bash
sudo certbot renew --dry-run
docker compose restart nginx
```
