Fly.io 部署
目标: 在 Fly.io 机器上运行 OpenClaw Gateway(网关),配置持久化存储、自动 HTTPS 以及 Discord/频道访问。
你需要什么
- 已安装 flyctl CLI
- Fly.io 账户(免费套餐可用)
- 模型认证:Anthropic API key(API 密钥)或其他提供商密钥
- 频道凭证:Discord bot token(机器人令牌)、Telegram token(令牌)等
新手快速路径
- 克隆仓库 → 自定义 fly.toml
- 创建应用 + 卷 → 设置密钥
- 使用 fly deploy 部署
- SSH 进入以创建配置或使用控制 UI
1) 创建 Fly 应用
# 克隆仓库
git clone https://github.com/openclaw/openclaw.git
cd openclaw
# 创建新的 Fly 应用(选择你自己的名字)
fly apps create my-openclaw
# 创建持久化卷(1GB 通常够用)
fly volumes create openclaw_data --size 1 --region iad
提示: 选择离你近的区域。常见选项:lhr(伦敦)、iad(弗吉尼亚)、sjc(圣何塞)。
2) 配置 fly.toml
编辑 fly.toml 以匹配你的应用名称和需求。
安全提示: 默认配置会暴露公共 URL。对于无公共 IP 的加固部署,请参阅 私有部署(加固) 或使用 fly.private.toml。
app = "my-openclaw" # 你的应用名称
primary_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 = 3000
force_https = true
auto_stop_machines = false
auto_start_machines = true
min_machines_running = 1
processes = ["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" | 在卷上持久化状态 |
3) 设置密钥
# 必需:网关令牌(用于非回环绑定)
fly secrets set OPENCLAW_GATEWAY_TOKEN=$(openssl rand -hex 32)
# 模型提供商 API 密钥
fly secrets set ANTHROPIC_API_KEY=sk-ant-...
# 可选:其他提供商
fly secrets set OPENAI_API_KEY=sk-...
fly secrets set GOOGLE_API_KEY=...
# 频道令牌
fly secrets set DISCORD_BOT_TOKEN=MTQ...
注意:
- 非回环绑定(--bind lan)需要 OPENCLAW_GATEWAY_TOKEN 以确保安全。
- 将这些令牌视为密码。
- 优先使用环境变量而非配置文件 来存储所有 API 密钥和令牌。这样可以避免密钥出现在 openclaw.json 中,防止意外暴露或记录日志。
4) 部署
fly deploy
首次部署会构建 Docker 镜像(约 2-3 分钟)。后续部署更快。
部署后验证:
fly status
fly logs
你应该看到:
[gateway] listening on ws://0.0.0.0:3000 (PID xxx)
[discord] logged in to discord as xxx
5) 创建配置文件
SSH 进入机器以创建正确的配置:
fly ssh console
创建配置目录和文件:
mkdir -p /data
cat > /data/openclaw.json << 'EOF'
{
"agents": {
"defaults": {
"model": {
"primary": "anthropic/claude-opus-4-5",
"fallbacks": ["anthropic/claude-sonnet-4-5", "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": {
"lastTouchedVersion": "2026.1.29"
}
}
EOF
注意: 使用 OPENCLAW_STATE_DIR=/data,配置路径为 /data/openclaw.json。
注意: Discord 令牌可以来自:
- 环境变量:DISCORD_BOT_TOKEN(推荐用于密钥)
- 配置文件:channels.discord.token
如果使用环境变量,无需在配置中添加令牌。网关会自动读取 DISCORD_BOT_TOKEN。
重启以应用:
exit
fly machine restart <machine-id>
6) 访问网关
控制 UI
在浏览器中打开:
fly open
或访问 https://my-openclaw.fly.dev/
粘贴你的网关令牌(来自 OPENCLAW_GATEWAY_TOKEN)以进行身份验证。
日志
fly logs # 实时日志
fly logs --no-tail # 最近日志
SSH 控制台
fly ssh console
故障排除
"App is not listening on expected address"
网关绑定到 127.0.0.1 而不是 0.0.0.0。
修复: 在 fly.toml 的进程命令中添加 --bind lan。
健康检查失败 / 连接被拒绝
Fly 无法在配置的端口上访问网关。
修复: 确保 internal_port 匹配网关端口(设置 --port 3000 或 OPENCLAW_GATEWAY_PORT=3000)。
OOM / 内存问题
容器不断重启或被杀死。迹象:SIGABRT、v8::internal::Runtime_AllocateInYoungGeneration 或静默重启。
修复: 在 fly.toml 中增加内存:
[[vm]]
memory = "2048mb"
或更新现有机器:
fly machine update <machine-id> --vm-memory 2048 -y
注意: 512MB 太小。1GB 可能有效但在负载下或详细日志记录时可能 OOM。推荐 2GB。
网关锁定问题
网关拒绝启动并显示"已在运行"错误。
这发生在容器重启但 PID 锁定文件在卷上持久化时。
修复: 删除锁定文件:
fly ssh console --command "rm -f /data/gateway.*.lock"
fly machine restart <machine-id>
锁定文件位于 /data/gateway.*.lock(不在子目录中)。
配置未被读取
如果使用 --allow-unconfigured,网关会创建最小配置。你在 /data/openclaw.json 的自定义配置应在重启时被读取。
验证配置存在:
fly ssh console --command "cat /data/openclaw.json"
通过 SSH 写入配置
fly ssh console -C 命令不支持 shell 重定向。要写入配置文件:
# 使用 echo + tee(从本地管道到远程)
echo '{"your":"config"}' | fly ssh console -C "tee /data/openclaw.json"
# 或使用 sftp
fly sftp shell
> put /local/path/config.json /data/openclaw.json
注意: 如果文件已存在,fly sftp 可能失败。首先删除:
fly ssh console --command "rm /data/openclaw.json"
状态未持久化
如果重启后丢失凭证或会话,状态目录正在写入容器文件系统。
修复: 确保在 fly.toml 中设置了 OPENCLAW_STATE_DIR=/data 并重新部署。
更新
# 拉取最新更改
git pull
# 重新部署
fly deploy
# 检查健康状态
fly status
fly logs
更新机器命令
如果你需要在不完全重新部署的情况下更改启动命令:
# 获取机器 ID
fly machines list
# 更新命令
fly machine update <machine-id> --command "node dist/index.js gateway --port 3000 --bind lan" -y
# 或增加内存
fly machine update <machine-id> --vm-memory 2048 --command "node dist/index.js gateway --port 3000 --bind lan" -y
注意: 在 fly deploy 之后,机器命令可能会重置为 fly.toml 中的内容。如果你手动进行了更改,请在部署后重新应用。
私有部署(加固)
默认情况下,Fly 分配公共 IP,使你的网关可在 https://your-app.fly.dev 访问。这很方便,但意味着你的部署可被互联网扫描器(Shodan、Censys 等)发现。
对于无公共暴露的加固部署,使用私有模板。
何时使用私有部署
- 你只进行出站调用/消息(无入站 webhooks)
- 你使用 ngrok 或 Tailscale 隧道进行任何 webhook 回调
- 你通过 SSH、代理或 WireGuard 而不是浏览器访问网关
- 你希望部署对互联网扫描器隐藏
设置
使用 fly.private.toml 而不是标准配置:
# 使用私有配置部署
fly deploy -c fly.private.toml
或转换现有部署:
# 列出当前 IP
fly ips list -a my-openclaw
# 释放公共 IP
fly ips release <public-ipv4> -a my-openclaw
fly ips release <public-ipv6> -a my-openclaw
# 切换到私有配置,以便未来的部署不会重新分配公共 IP
#(移除 [http_service] 或使用私有模板部署)
fly deploy -c fly.private.toml
# 分配仅私有 IPv6
fly ips allocate-v6 --private -a my-openclaw
之后,fly ips list 应只显示 private 类型的 IP:
VERSION IP TYPE REGION
v6 fdaa:x:x:x:x::x private global
访问私有部署
由于没有公共 URL,使用以下方法之一:
选项 1:本地代理(最简单)
# 将本地端口 3000 转发到应用
fly proxy 3000:3000 -a my-openclaw
# 然后在浏览器中打开 http://localhost:3000
选项 2:WireGuard VPN
# 创建 WireGuard 配置(一次性)
fly wireguard create
# 导入到 WireGuard 客户端,然后通过内部 IPv6 访问
# 例如:http://[fdaa:x:x:x:x::x]:3000
选项 3:仅 SSH
fly ssh console -a my-openclaw
私有部署的 Webhooks
如果你需要 webhook 回调(Twilio、Telnyx 等)而不暴露公共访问:
- ngrok 隧道 - 在容器内或作为 sidecar 运行 ngrok
- Tailscale Funnel - 通过 Tailscale 暴露特定路径
- 仅出站 - 某些提供商(Twilio)在没有 webhooks 的情况下可以正常进行出站呼叫
使用 ngrok 的语音通话配置示例:
{
"plugins": {
"entries": {
"voice-call": {
"enabled": true,
"config": {
"provider": "twilio",
"tunnel": { "provider": "ngrok" }
}
}
}
}
}
ngrok 隧道在容器内运行,提供公共 webhook URL,而不暴露 Fly 应用本身。
安全优势
| 方面 | 公共 | 私有 |
|---|---|---|
| 互联网扫描器 | 可发现 | 隐藏 |
| 直接攻击 | 可能 | 被阻止 |
| 控制 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 定价。