Fly.io
Fly.io 部署
Section titled “Fly.io 部署”目标: 在 OpenClaw 机器上运行 Gateway(网关) Fly.io,配置持久化存储、自动 HTTPS 以及 Discord/渠道访问权限。
- 已安装 flyctl CLI
- Fly.io 账户(免费层级即可)
- 模型认证:您所选模型提供商的 API 密钥
- 渠道凭据:Discord 机器人令牌,Telegram 令牌等。
新手快速入门
Section titled “新手快速入门”- 克隆仓库 → 自定义
fly.toml - 创建应用 + 卷 → 设置密钥
- 使用
fly deploy部署 - 通过 SSH 登录以创建配置或使用控制 UI
创建 Fly 应用
Terminal window # Clone the repogit clone https://github.com/openclaw/openclaw.gitcd openclaw# Create a new Fly app (pick your own name)fly apps create my-openclaw# Create a persistent volume (1GB is usually enough)fly volumes create openclaw_data --size 1 --region iad提示: 选择一个距离您较近的区域。常见选项:
lhr(伦敦),iad(弗吉尼亚),sjc(圣何塞)。配置 fly.toml
编辑
fly.toml以匹配您的应用名称和需求。安全提示: 默认配置会暴露一个公开 URL。如需无公网 IP 的加固部署,请参阅 私有部署 或使用
fly.private.toml。app = "my-openclaw" # Your app nameprimary_region = "iad"[build]dockerfile = "Dockerfile"[env]NODE_ENV = "production"OPENCLAW_PREFER_PNPM = "1"OPENCLAW_STATE_DIR = "/data"NODE_OPTIONS = "--max-old-space-size=1536"[processes]app = "node dist/index.js gateway --allow-unconfigured --port 3000 --bind lan"[http_service]internal_port = 3000force_https = trueauto_stop_machines = falseauto_start_machines = truemin_machines_running = 1processes = ["app"][[vm]]size = "shared-cpu-2x"memory = "2048mb"[mounts]source = "openclaw_data"destination = "/data"关键设置:
设置 原因 --bind lan绑定到 0.0.0.0以便 Fly 的代理可以访问网关--allow-unconfigured在没有配置文件的情况下启动(您将在之后创建) internal_port = 3000必须与 --port 3000(或OPENCLAW_GATEWAY_PORT)匹配以通过 Fly 健康检查memory = "2048mb"512MB 太小;推荐 2GB OPENCLAW_STATE_DIR = "/data"在卷上持久化状态 设置密钥
Terminal window # Required: Gateway token (for non-loopback binding)fly secrets set OPENCLAW_GATEWAY_TOKEN=$(openssl rand -hex 32)# Model provider API keysfly secrets set ANTHROPIC_API_KEY=sk-ant-...# Optional: Other providersfly secrets set OPENAI_API_KEY=sk-...fly secrets set GOOGLE_API_KEY=...# Channel tokensfly secrets set DISCORD_BOT_TOKEN=MTQ...注意:
- 非回环绑定 (
--bind lan) 出于安全考虑需要OPENCLAW_GATEWAY_TOKEN。 - 请像对待密码一样对待这些令牌。
- 对于所有 API 密钥和令牌,优先使用环境变量而不是配置文件。这可以将密钥保留在
openclaw.json之外,防止其被意外泄露或记录。
- 非回环绑定 (
部署
Terminal window fly deploy首次部署会构建 Docker 镜像(约 2-3 分钟)。后续部署会更快。
部署完成后,请验证:
Terminal window fly statusfly logs您应该会看到:
[gateway] listening on ws://0.0.0.0:3000 (PID xxx)[discord] logged in to discord as xxx创建配置文件
通过 SSH 进入机器以创建合适的配置:
Terminal window fly ssh console创建配置目录和文件:
Terminal window mkdir -p /datacat > /data/openclaw.json << 'EOF'{"agents": {"defaults": {"model": {"primary": "anthropic/claude-opus-4-6","fallbacks": ["anthropic/claude-sonnet-4-6", "openai/gpt-4o"]},"maxConcurrent": 4},"list": [{"id": "main","default": true}]},"auth": {"profiles": {"anthropic:default": { "mode": "token", "provider": "anthropic" },"openai:default": { "mode": "token", "provider": "openai" }}},"bindings": [{"agentId": "main","match": { "channel": "discord" }}],"channels": {"discord": {"enabled": true,"groupPolicy": "allowlist","guilds": {"YOUR_GUILD_ID": {"channels": { "general": { "allow": true } },"requireMention": false}}}},"gateway": {"mode": "local","bind": "auto"},"meta": {}}EOF注意: 使用
OPENCLAW_STATE_DIR=/data时,配置路径为/data/openclaw.json。注意: Discord 令牌可以来自以下任一位置:
- 环境变量:
DISCORD_BOT_TOKEN(推荐用于密钥) - 配置文件:
channels.discord.token
如果使用环境变量,则无需将令牌添加到配置中。网关会自动读取
DISCORD_BOT_TOKEN。重启以应用更改:
Terminal window exitfly machine restart- 环境变量:
访问 Gateway(网关)
在浏览器中打开:
Terminal window fly open或访问
https://my-openclaw.fly.dev/粘贴您的网关令牌(来自
OPENCLAW_GATEWAY_TOKEN的那个)以进行身份验证。Terminal window fly logs # Live logsfly logs --no-tail # Recent logsSSH 控制台
Section titled “SSH 控制台”Terminal window fly ssh console
”应用未在预期地址上监听”
Section titled “”应用未在预期地址上监听””网关正绑定到 127.0.0.1 而不是 0.0.0.0。
修复: 将 --bind lan 添加到 fly.toml 中的进程命令里。
健康检查失败 / 连接被拒绝
Section titled “健康检查失败 / 连接被拒绝”Fly 无法在配置的端口上访问网关。
修复: 确保 internal_port 与 Gateway(网关) 端口匹配(设置 --port 3000 或 OPENCLAW_GATEWAY_PORT=3000)。
OOM / 内存问题
Section titled “OOM / 内存问题”容器不断重启或被终止。迹象:SIGABRT、v8::internal::Runtime_AllocateInYoungGeneration 或静默重启。
修复: 在 fly.toml 中增加内存:
[[vm]] memory = "2048mb"或更新现有机器:
fly machine update <machine-id> --vm-memory 2048 -y注意: 512MB 太小。1GB 可能可行,但在负载较大或启用详细日志时可能会 OOM。建议使用 2GB。
Gateway(网关) 锁问题
Section titled “Gateway(网关) 锁问题”Gateway(网关) 拒绝启动,并提示“already running”错误。
这种情况发生在容器重启但 PID 锁文件仍保留在卷上时。
修复: 删除锁文件:
fly ssh console --command "rm -f /data/gateway.*.lock"fly machine restart <machine-id>锁文件位于 /data/gateway.*.lock(不在子目录中)。
如果使用 --allow-unconfigured,Gateway(网关) 会创建一个最小配置。重启时应读取位于 /data/openclaw.json 的自定义配置。
验证配置是否存在:
fly ssh console --command "cat /data/openclaw.json"通过 SSH 写入配置
Section titled “通过 SSH 写入配置”fly ssh console -C 命令不支持 Shell 重定向。要写入配置文件:
# Use echo + tee (pipe from local to remote)echo '{"your":"config"}' | fly ssh console -C "tee /data/openclaw.json"
# Or use sftpfly sftp shell> put /local/path/config.json /data/openclaw.json注意: 如果文件已存在,fly sftp 可能会失败。请先删除:
fly ssh console --command "rm /data/openclaw.json"状态未持久化
Section titled “状态未持久化”如果在重启后丢失凭据或会话,说明状态目录正在写入容器文件系统。
修复: 确保在 fly.toml 中设置了 OPENCLAW_STATE_DIR=/data 并重新部署。
# Pull latest changesgit pull
# Redeployfly deploy
# Check healthfly statusfly logs更新机器命令
Section titled “更新机器命令”如果需要在未完全重新部署的情况下更改启动命令:
# Get machine IDfly machines list
# Update commandfly machine update <machine-id> --command "node dist/index.js gateway --port 3000 --bind lan" -y
# Or with memory increasefly machine update <machine-id> --vm-memory 2048 --command "node dist/index.js gateway --port 3000 --bind lan" -y注意: 在 fly deploy 之后,机器命令可能会重置为 fly.toml 中的内容。如果您进行了手动更改,请在部署后重新应用。
私有部署(强化)
Section titled “私有部署(强化)”默认情况下,Fly 会分配公共 IP,使您的 Gateway(网关) 可通过 https://your-app.fly.dev 访问。这很方便,但也意味着您的部署会被互联网扫描仪(如 Shodan、Censys 等)发现。
要进行无公网暴露的强化部署,请使用私有模板。
何时使用私有部署
Section titled “何时使用私有部署”- 您仅进行出站调用/消息(无入站 Webhook)
- 您使用 ngrok 或 Tailscale 隧道进行任何 Webhook 回调
- 您通过 SSH、代理或 WireGuard 而非浏览器来访问网关
- 您希望部署对互联网扫描仪隐藏
使用 fly.private.toml 代替标准配置:
# Deploy with private configfly deploy -c fly.private.toml或转换现有部署:
# List current IPsfly ips list -a my-openclaw
# Release public IPsfly ips release <public-ipv4> -a my-openclawfly ips release <public-ipv6> -a my-openclaw
# Switch to private config so future deploys don't re-allocate public IPs# (remove [http_service] or deploy with the private template)fly deploy -c fly.private.toml
# Allocate private-only IPv6fly ips allocate-v6 --private -a my-openclaw在此之后,fly ips list 应该仅显示 private 类型的 IP:
VERSION IP TYPE REGIONv6 fdaa:x:x:x:x::x private global访问私有部署
Section titled “访问私有部署”由于没有公共 URL,请使用以下方法之一:
选项 1:本地代理(最简单)
# Forward local port 3000 to the appfly proxy 3000:3000 -a my-openclaw
# Then open http://localhost:3000 in browser选项 2:WireGuard VPN
# Create WireGuard config (one-time)fly wireguard create
# Import to WireGuard client, then access via internal IPv6# Example: http://[fdaa:x:x:x:x::x]:3000选项 3:仅 SSH
fly ssh console -a my-openclaw私有部署的 Webhooks
Section titled “私有部署的 Webhooks”如果您需要在没有公开暴露的情况下接收 webhook 回调(Twilio、Telnyx 等):
- ngrok 隧道 - 在容器内或作为 sidecar 运行 ngrok
- Tailscale Funnel - 通过 Tailscale 暴露特定路径
- 仅出站 - 某些提供商(Twilio)在没有 webhook 的情况下也能正常进行出站呼叫
使用 ngrok 的语音呼叫配置示例:
{ plugins: { entries: { "voice-call": { enabled: true, config: { provider: "twilio", tunnel: { provider: "ngrok" }, webhookSecurity: { allowedHosts: ["example.ngrok.app"], }, }, }, }, },}ngrok 隧道在容器内运行,并提供公共 webhook URL,而无需暴露 Fly 应用本身。将 webhookSecurity.allowedHosts 设置为公共隧道主机名,以便接受转发的主机头。
| 方面 | 公开 | 私有 |
|---|---|---|
| 互联网扫描仪 | 可被发现 | 隐藏 |
| 直接攻击 | 可能 | 已阻止 |
| 控制 UI 访问 | 浏览器 | 代理/VPN |
| Webhook 投递 | 直接 | 通过隧道 |
- Fly.io 使用 x86 架构(非 ARM)
- Dockerfile 兼容这两种架构
- 对于 WhatsApp/Telegram 新手引导,请使用
fly ssh console - 持久数据存储在
/data的卷上 - Signal 需要 Java + signal-cli;请使用自定义镜像并将内存保持在 2GB 以上。
使用推荐配置(shared-cpu-2x,2GB RAM):
- 每月约 $10-15,具体取决于使用情况
- 免费层包含一定额度
详情请参阅 Fly.io 定价。
- 设置消息频道:频道
- 配置 Gateway(网关):Gateway(网关) 配置
- 保持 OpenClaw 更新:更新