目录

OpenClaw VPS部署方案

最新部署日期: 2026年3月3日
版本: Gateway v2026.3.2
状态: 生产就绪


架构概览

整体架构图

┌─────────────────────────────────────────────────────────────┐
│                      外部访问层                              │
│  ┌──────────────┐  ┌──────────────┐  ┌──────────────┐      │
│  │   Telegram   │  │  Control UI  │  │    SSH       │      │
│  │   Bot API    │  │   (HTTP)     │  │   管理       │      │
│  └──────┬───────┘  └──────┬───────┘  └──────┬───────┘      │
└─────────┼─────────────────┼─────────────────┼───────────────┘
          │                 │                 │
          ▼                 ▼                 ▼
┌─────────────────────────────────────────────────────────────┐
│                    AWS EC2 服务器                            │
│  实例ID: <your-ec2-instance-id>                             │
│  公网IP: <your-ec2-ip>                                      │
│  类型: t3.small (2 vCPU, 2GB RAM)                           │
│  系统: Amazon Linux 2023                                    │
│                                                             │
│  ┌───────────────────────────────────────────────────────┐  │
│  │         OpenClaw Gateway v2026.3.2                  │  │
│  │  ┌─────────────┐  ┌─────────────┐  ┌─────────────┐   │  │
│  │  │  Telegram   │  │  WebSocket  │  │  HTTP API   │   │  │
│  │  │   通道      │  │  服务器     │  │  接口       │   │  │
│  │  │             │  │ (端口:18789)│  │ (端口:18789)│   │  │
│  │  └──────┬──────┘  └──────┬──────┘  └──────┬──────┘   │  │
│  │         │                │                 │          │  │
│  │         └────────────────┴─────────────────┘          │  │
│  │                          │                            │  │
│  │              ┌───────────┴───────────┐                │  │
│  │              │   Agent 处理引擎      │                │  │
│  │              │   (AI 模型: GPT-OSS) │                │  │
│  │              └───────────┬───────────┘                │  │
│  │                          │                            │  │
│  │              ┌───────────┴───────────┐                │  │
│  │              │   工具层              │                │  │
│  │              │   web_search, web_fetch, cron          │  │
│  │              └───────────┴───────────┘                │  │
│  └───────────────────────────────────────────────────────┘  │
└─────────────────────────────────────────────────────────────┘

数据流向

  1. 用户 → Telegram Bot: 发送消息
  2. Gateway → Agent: 处理消息,决定行动
  3. Agent 执行工具: 使用 web_search、web_fetch、cron 等工具
  4. 结果 → Gateway → Telegram: 返回执行结果

组件清单

EC2 服务器 (AWS)

组件 版本/配置 状态
实例类型 t3.small ✅ 运行中
操作系统 Amazon Linux 2023 ✅ 已配置
Node.js v22.22.0 ✅ 已安装
OpenClaw Gateway v2026.3.2 ✅ 运行中
SSH 服务 OpenSSH 9.x ✅ 已加固
Swap 2GB ✅ 已配置
SSM Agent v3.3.x ✅ 已启用
IAM Role EC2-SSM-Role ✅ 已挂载

外部服务

服务 用途 状态
agentify.top AI 模型 API ✅ 已配置
Telegram Bot 消息通道 ✅ 运行中

网络拓扑

端口映射

端口 协议 服务 绑定地址 说明
22 TCP SSH 0.0.0.0 EC2 管理入口
80 TCP HTTP 0.0.0.0 预留 (未使用)
443 TCP HTTPS 0.0.0.0 预留 (未使用)
18789 TCP OpenClaw Gateway 127.0.0.1 WebSocket + HTTP API

安全组规则

# 入站规则
TCP 22   0.0.0.0/0   # SSH 访问
TCP 80   0.0.0.0/0   # HTTP (预留)
TCP 443  0.0.0.0/0   # HTTPS (预留)

注意: Gateway 绑定在 loopback (127.0.0.1:18789),不直接暴露公网。


安全配置

认证凭证

⚠️ 重要: 以下凭证仅保存在本地,请勿分享。

Gateway Token

<your-gateway-token>
  • 用途: 认证 WebSocket 连接
  • 位置: ~/.openclaw/openclaw.json (EC2)

SSH 密钥

  • 位置: ~/.ssh/openclaw.pem
  • 权限: 400 (仅所有者可读)
  • 对应 EC2 Key Pair: openclaw

Telegram Bot Token

<your-telegram-bot-token>
  • 用途: Telegram Bot API 认证
  • 位置: EC2 Gateway 配置中

API Key (agentify.top)

<your-api-key>
  • 用途: AI 模型调用
  • 位置: EC2 Gateway 配置中

访问控制

Telegram 配置

{
  "dmPolicy": "pairing",
  "allowFrom": ["<your-telegram-user-id>"]
}
  • dmPolicy: pairing: 新用户需要配对码才能使用
  • allowFrom: 仅允许你的 Telegram 用户 ID

初始部署步骤

1. AWS 账户配置

1.1 查看当前账户

aws sts get-caller-identity

1.2 切换账户(如需要)

# 清除旧凭证
rm ~/.aws/credentials ~/.aws/config

# 配置新账户
aws configure
# 输入:
# - AWS Access Key ID: 你的 access_key
# - AWS Secret Access Key: 你的 secret_key
# - Default region name: us-east-1
# - Default output format: json

2. 创建 EC2 实例

2.1 创建密钥对

  1. 访问 AWS 控制台: https://console.aws.amazon.com/ec2/
  2. 导航到 Key Pairs (密钥对)
  3. 点击 Create key pair
  4. 名称: openclaw
  5. 格式: .pem (Mac/Linux) 或 .ppk (Windows)
  6. 点击 Create 并下载私钥

2.2 创建实例

  1. 点击 Launch Instance
  2. Name: OpenClaw-Gateway
  3. AMI: Amazon Linux 2023
  4. Instance type: t3.small (2 vCPU, 2GB RAM)
  5. Key pair: 选择 openclaw
  6. Network settings:
    • 创建安全组,允许 SSH (22), HTTP (80), HTTPS (443)
  7. Storage: 8GB gp3 (默认)
  8. 点击 Launch

3. 连接服务器

# 设置密钥权限
chmod 400 ~/.ssh/openclaw.pem

# SSH 连接
ssh -i ~/.ssh/openclaw.pem ec2-user@<你的-EC2-公网IP>

4. 安装 OpenClaw

当前版本: v2026.3.2

4.1 安装 Node.js 22+

# 安装 fnm (Node 版本管理器)
curl -fsSL https://fnm.vercel.app/install | bash

# 加载 fnm
export PATH=$HOME/.local/share/fnm/bin:$PATH
eval $(fnm env)

# 安装 Node.js 22
fnm install 22
fnm use 22

# 验证
node -v  # v22.x.x
npm -v   # 10.x.x

4.2 安装 Git(如需要)

sudo yum install -y git

4.3 安装 OpenClaw

export PATH=$HOME/.local/share/fnm/bin:$PATH
npm install -g openclaw@latest

# 验证
openclaw --version

5. 配置 OpenClaw

5.1 创建配置目录

mkdir -p ~/.openclaw

5.2 编辑配置文件

nano ~/.openclaw/openclaw.json

使用以下配置模板(替换占位符):

{
  "gateway": {
    "mode": "local",
    "bind": "loopback",
    "port": 18789,
    "auth": {
      "mode": "token",
      "token": "your-secret-token-here"
    },
    "http": {
      "endpoints": {
        "chatCompletions": {
          "enabled": true
        }
      }
    }
  },
  "agents": {
    "defaults": {
      "model": {
        "primary": "openai-compat/openai/gpt-oss-120b"
      },
      "elevatedDefault": "full",
      "maxConcurrent": 4,
      "subagents": {
        "maxConcurrent": 8
      }
    }
  },
  "models": {
    "mode": "merge",
    "providers": {
      "openai-compat": {
        "baseUrl": "https://api.agentify.top/v1",
        "apiKey": "your-api-key-here",
        "api": "openai-completions",
        "models": [
          {
            "id": "openai/gpt-oss-120b",
            "name": "GPT-OSS-120B"
          }
        ]
      }
    }
  },
  "tools": {
    "allow": [
      "web_search", "web_fetch", "cron"
    ],
    "elevated": {
      "enabled": true,
      "allowFrom": {
        "telegram": ["your-telegram-user-id"]
      }
    }
  },
  "channels": {
    "telegram": {
      "enabled": true,
      "botToken": "your-telegram-bot-token",
      "dmPolicy": "pairing",
      "allowFrom": ["your-telegram-user-id"],
      "groupPolicy": "disabled",
      "groups": {
        "*": {
          "requireMention": true
        }
      }
    }
  },
  "plugins": {
    "entries": {
      "telegram": {
        "enabled": true
      }
    }
  }
}

5.3 配置说明

配置项 说明 获取方式
gateway.auth.token Gateway 访问令牌 随机生成长字符串
models.providers.openai-compat.apiKey 中转站 API Key agentify.top 控制台
channels.telegram.botToken Telegram Bot Token @BotFather
channels.telegram.allowFrom 允许的 Telegram 用户 ID 首次对话后查看日志

6. 配置 Systemd 服务

创建服务文件实现自动启动:

mkdir -p ~/.config/systemd/user
cat > ~/.config/systemd/user/openclaw-gateway.service << 'EOF'
[Unit]
Description=OpenClaw Gateway (v2026.3.2)
After=network.target

[Service]
Type=simple
Environment="PATH=/home/ec2-user/.local/share/fnm/bin:/usr/local/bin:/usr/bin:/bin"
Environment="HOME=/home/ec2-user"
ExecStart=/bin/bash -c 'export PATH=/home/ec2-user/.local/share/fnm/bin:$PATH && eval $(fnm env) && openclaw gateway --port 18789'
Restart=always
RestartSec=10
StandardOutput=journal
StandardError=journal

[Install]
WantedBy=default.target
EOF

# 启用并启动服务
systemctl --user daemon-reload
systemctl --user enable openclaw-gateway
systemctl --user start openclaw-gateway

# 验证状态
systemctl --user status openclaw-gateway

7. 系统加固

7.1 配置 Swap

sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo '/swapfile swap swap defaults 0 0' | sudo tee -a /etc/fstab

7.2 保护 SSHD

# 设置 sshd OOM 保护
echo -1000 | sudo tee /proc/$(pgrep -x sshd)/oom_score_adj

EC2 服务器配置

基本信息

实例ID: <your-ec2-instance-id>
公网IP: <your-ec2-ip>
私网IP: <your-private-ip>
可用区: us-east-1a
密钥对: openclaw
安全组: <your-security-group-id>
IAM角色: EC2-SSM-Role

重要文件路径

文件 路径 说明
主配置 ~/.openclaw/openclaw.json Gateway 配置
日志 /tmp/openclaw/openclaw-YYYY-MM-DD.log 运行日志
Systemd 服务 ~/.config/systemd/user/openclaw-gateway.service 服务定义
Node 安装 ~/.local/share/fnm/ fnm + Node.js

服务管理

# 查看 Gateway 状态
systemctl --user status openclaw-gateway

# 重启 Gateway
systemctl --user restart openclaw-gateway

# 查看日志
journalctl --user -u openclaw-gateway -f

# Gateway CLI 命令
export PATH=$HOME/.local/share/fnm/bin:$PATH
eval $(fnm env)
openclaw --version
openclaw status
openclaw models list

日常使用指南

首次启动 (EC2 重启后)

无需手动操作,systemd 会自动启动服务。

验证服务状态:

# SSH 到 EC2
ssh -i ~/.ssh/openclaw.pem ec2-user@<your-ec2-ip>

# 查看 Gateway 状态
systemctl --user status openclaw-gateway

手动管理服务

# SSH 到 EC2
ssh -i ~/.ssh/openclaw.pem ec2-user@<your-ec2-ip>

# 停止服务
systemctl --user stop openclaw-gateway

# 启动服务
systemctl --user start openclaw-gateway

# 查看日志
journalctl --user -u openclaw-gateway -f

Telegram Bot 配置

创建 Bot

  1. 在 Telegram 中联系 @BotFather
  2. 发送 /newbot
  3. 按提示设置:
    • 名称: 你的 Bot 显示名称(如 “My AI Assistant”)
    • 用户名: 唯一标识,必须以 bot 结尾(如 myai_bot
  4. 复制获得的 Bot Token(格式:123456789:ABCdefGHIjklMNOpqrSTUvwxyz
  5. 将 Token 填入 ~/.openclaw/openclaw.jsonchannels.telegram.botToken

获取你的 Telegram 用户 ID

方法 1: 首次对话后查看日志

ssh -i ~/.ssh/openclaw.pem ec2-user@<your-ec2-ip> \
  'tail -50 /tmp/openclaw/openclaw-*.log | grep -i telegram'

方法 2: 使用 @userinfobot

  1. 在 Telegram 中打开 @userinfobot
  2. 发送任意消息
  3. Bot 会回复你的用户 ID

配置 Gateway

将获取到的用户 ID 填入配置:

{
  "channels": {
    "telegram": {
      "enabled": true,
      "botToken": "<your-telegram-bot-token>",
      "dmPolicy": "pairing",
      "allowFrom": ["<your-telegram-user-id>"]
    }
  }
}

重启 Gateway 生效:

systemctl --user restart openclaw-gateway

Telegram 日常使用

  1. 开始使用:

    • 打开 Telegram,找到你的 Bot
    • 发送 /start 或任意消息
    • 如果是首次使用,Gateway 会显示配对码
  2. 查看配对码 (在 EC2 上):

    ssh -i ~/.ssh/openclaw.pem ec2-user@<your-ec2-ip> \
      'export PATH=$HOME/.local/share/fnm/bin:$PATH && eval $(fnm env) && openclaw pairing list telegram'
  3. 批准配对:

    ssh -i ~/.ssh/openclaw.pem ec2-user@<your-ec2-ip> \
      'export PATH=$HOME/.local/share/fnm/bin:$PATH && eval $(fnm env) && openclaw pairing approve telegram <CODE>'
  4. 日常使用:

    • 直接发送消息给 Bot,AI 会处理
    • 可用工具: web_search, web_fetch, cron

CLI 命令参考

状态管理

# 查看 Gateway 状态
openclaw status

# 深度检查
openclaw status --deep

# 查看健康状态
openclaw health

模型管理

# 查看可用模型列表
openclaw models list

# 设置默认模型
openclaw models set openai-compat/openai/gpt-oss-120b

# 查看模型状态
openclaw models status

配对管理

# 查看待配对请求
openclaw pairing list telegram

# 批准配对请求
openclaw pairing approve telegram <CODE>

# 拒绝配对请求
openclaw pairing deny telegram <CODE>

# 查看所有配对
openclaw pairing list

会话管理

# 列出所有会话
openclaw sessions

# 查看会话历史
openclaw sessions history <session-id>

频道管理

# 查看频道状态
openclaw channels status

# 查看 Telegram 频道详情
openclaw channels logs

配置管理

# 查看配置
openclaw config get

# 查看特定配置项
openclaw config get gateway.port
openclaw config get channels.telegram.dmPolicy

日志和调试

# 查看 Gateway 日志
journalctl --user -u openclaw-gateway -f

# 查看特定日期日志
tail -f /tmp/openclaw/openclaw-2026-02-07.log

故障排除

SSH 连接失败

症状: Connection closed by <your-ec2-ip> port 22

排查步骤:

  1. 检查 known_hosts: ssh-keygen -R <your-ec2-ip>
  2. 重新添加: ssh-keyscan -H <your-ec2-ip> >> ~/.ssh/known_hosts
  3. 检查安全组: AWS 控制台 → EC2 → 安全组 → 入站规则
  4. 通过 SSM 连接 (备用):
    aws ssm start-session --target <your-ec2-instance-id>

Gateway 启动失败

症状: systemctl --user status openclaw-gateway 显示失败

排查步骤:

  1. 检查日志: journalctl --user -u openclaw-gateway -f
  2. 验证 JSON 配置: cat ~/.openclaw/openclaw.json | python3 -m json.tool
  3. 重启服务: systemctl --user restart openclaw-gateway

Telegram Bot 无响应

排查步骤:

  1. SSH 到 EC2: ssh -i ~/.ssh/openclaw.pem ec2-user@<your-ec2-ip>
  2. 检查 Gateway 状态: systemctl --user status openclaw-gateway
  3. 检查 Telegram 配置: openclaw channels status
  4. 重启 Gateway: systemctl --user restart openclaw-gateway

503 distributor 无可用渠道

症状: AI 回复 “503 distributor” 或 “no available channel”

可能原因:

  1. 模型 ID 不正确
  2. 中转站 API Key 无效
  3. 中转站不支持该模型
  4. API Key 余额不足

解决方法:

  1. 检查模型 ID:

    openclaw config get agents.defaults.model.primary
  2. 验证 API Key:

    curl https://api.agentify.top/v1/models \
      -H "Authorization: Bearer your-api-key"
  3. 更换模型:

    openclaw models set openai-compat/openai/gpt-4o

备份与恢复

需要备份的文件

EC2 服务器:

  • /home/ec2-user/.openclaw/ 目录 (Gateway 配置、会话、凭证)
  • SSH 密钥 ~/.ssh/openclaw.pem

备份脚本

#!/bin/bash
# backup-openclaw.sh

BACKUP_DIR="$HOME/openclaw-backup-$(date +%Y%m%d)"
mkdir -p "$BACKUP_DIR"

# EC2 (通过 SSH)
ssh -i ~/.ssh/openclaw.pem ec2-user@<your-ec2-ip> \
  'tar czf - -C /home/ec2-user .openclaw' > "$BACKUP_DIR/ec2-openclaw.tar.gz"

echo "备份完成: $BACKUP_DIR"

恢复步骤

# 停止 Gateway
ssh -i ~/.ssh/openclaw.pem ec2-user@<your-ec2-ip> \
  'systemctl --user stop openclaw-gateway'

# 恢复配置
scp -i ~/.ssh/openclaw.pem ec2-openclaw.tar.gz ec2-user@<your-ec2-ip>:/tmp/
ssh -i ~/.ssh/openclaw.pem ec2-user@<your-ec2-ip> \
  'rm -rf ~/.openclaw && tar xzf /tmp/ec2-openclaw.tar.gz -C ~ && systemctl --user start openclaw-gateway'

安全审计清单

检查清单

  • Gateway 绑定 loopback (127.0.0.1),不暴露公网
  • Gateway 启用了 Token 认证
  • Telegram dmPolicy 为 pairing,非 open
  • Telegram allowFrom 仅限你的用户 ID
  • SSH 密钥权限为 400
  • EC2 配置了 swap 防止 OOM
  • sshd 受 OOM 保护 (oom_score_adj = -1000)
  • Gateway 运行 systemctl 服务,自动重启
  • SSH 配置了 StrictHostKeyChecking=yes
  • known_hosts 已固定 EC2 host keys

安全命令

# 运行 OpenClaw 安全审计 (EC2 上)
ssh -i ~/.ssh/openclaw.pem ec2-user@<your-ec2-ip> \
  'export PATH=$HOME/.local/share/fnm/bin:$PATH && eval $(fnm env) && openclaw security audit'

参考资源


文档版本: 1.3
最后更新: 2026-03-03