OpenClaw 自学手册(六):精通篇 - 高级架构与生产部署

高级架构
远程部署
场景:本地客户端 + 远程服务器

┌─────────────────┐         ┌─────────────────┐
│   本地 Mac      │         │   远程服务器    │
├─────────────────┤         ├─────────────────┤
│ • macOS App     │◄────────┤ • Gateway       │
│ • CLI           │  SSH/   │ • WhatsApp      │
│ • TUI           │  VPN    │ • Telegram      │
└─────────────────┘         └─────────────────┘

配置步骤
服务器上安装 OpenClaw

ssh user@server
npm install -g openclaw

配置 Gateway

# 编辑配置
vim ~/.openclaw/openclaw.json
{
  gateway: {
    mode: "local",
    bind: "loopback",
    port: 18789,
  },
}

启动 Gateway

# 使用 systemd
openclaw gateway install --runtime node
openclaw gateway start

# 或手动运行
nohup openclaw gateway run > /tmp/openclaw.log 2>&1 &

本地连接

# 方式一:SSH 隧道
ssh -N -L 18789:127.0.0.1:18789 user@server

# 方式二:Tailscale
# 在服务器上
openclaw gateway run --tailscale serve

# 本地自动发现

配置本地客户端

# 设置远程 URL
openclaw config set gateway.remoteUrl "http://server:18789"
openclaw config set gateway.remoteToken "your-token"

高可用配置
多实例部署

                    ┌─────────────┐
                    │   负载均衡   │
                    └──────┬──────┘
                           │
            ┌──────────────┼──────────────┐
            ▼              ▼              ▼
      ┌─────────┐    ┌─────────┐    ┌─────────┐
      │Gateway 1│    │Gateway 2│    │Gateway 3│
      └─────────┘    └─────────┘    └─────────┘
            │              │              │
            └──────────────┼──────────────┘
                           ▼
                    ┌─────────────┐
                    │  共享存储   │
                    └─────────────┘

配置要点
共享会话存储:使用外部存储(S3、数据库)
WhatsApp 会话锁:只在一个实例上运行 WhatsApp
健康检查:监控每个实例状态
自动重启:使用 systemd/supervisord
生产部署
使用 Docker
Dockerfile

FROM node:22-alpine

# 安装 OpenClaw
RUN npm install -g openclaw

# 创建工作目录
RUN mkdir -p /app/workspace

# 复制配置
COPY openclaw.json /root/.openclaw/

# 暴露端口
EXPOSE 18789

# 启动 Gateway
CMD ["openclaw", "gateway", "run", "--bind", "lan"]

docker-compose.yml

version: '3.8'

services:
  openclaw:
    build: .
    ports:
      - "18789:18789"
    volumes:
      - ./workspace:/app/workspace
      - ./config:/root/.openclaw
    environment:
      - OPENCLAW_GATEWAY_TOKEN=${GATEWAY_TOKEN}
      - ANTHROPIC_API_KEY=${ANTHROPIC_API_KEY}
    restart: unless-stopped

启动

docker-compose up -d

使用 systemd
创建服务文件

# /etc/systemd/system/openclaw.service
[Unit]
Description=OpenClaw Gateway
After=network.target

[Service]
Type=simple
User=openclaw
WorkingDirectory=/home/openclaw
Environment="OPENCLAW_GATEWAY_TOKEN=your-token"
ExecStart=/usr/bin/openclaw gateway run --bind loopback
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target

启用服务

sudo systemctl daemon-reload
sudo systemctl enable openclaw
sudo systemctl start openclaw
sudo systemctl status openclaw

监控和日志
日志管理

# 查看 Gateway 日志
openclaw logs --follow

# 保存到文件
openclaw logs --follow > openclaw.log &

# 日志轮转(logrotate)
# /etc/logrotate.d/openclaw
/home/openclaw/logs/*.log {
    daily
    rotate 30
    compress
    delaycompress
    notifempty
    create 0640 openclaw openclaw
}

健康检查

# 检查 Gateway 状态
openclaw health

# 脚本化健康检查
#!/bin/bash
if ! openclaw health --timeout 5000 > /dev/null 2>&1; then
    echo "Gateway unhealthy, restarting..."
    systemctl restart openclaw
fi

备份和恢复
备份脚本

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

BACKUP_DIR="/backup/openclaw"
DATE=$(date +%Y%m%d_%H%M%S)

# 创建备份目录
mkdir -p "$BACKUP_DIR/$DATE"

# 备份配置
cp -r ~/.openclaw/openclaw.json "$BACKUP_DIR/$DATE/"

# 备份工作区
cp -r ~/.openclaw/workspace "$BACKUP_DIR/$DATE/"

# 备份会话(可选)
cp -r ~/.openclaw/agents "$BACKUP_DIR/$DATE/"

# 压缩
tar -czf "$BACKUP_DIR/openclaw_$DATE.tar.gz" -C "$BACKUP_DIR" "$DATE"
rm -rf "$BACKUP_DIR/$DATE"

echo "Backup created: openclaw_$DATE.tar.gz"

恢复

# 解压备份
tar -xzf openclaw_20260301_120000.tar.gz -C /tmp

# 恢复工作区
cp -r /tmp/20260301_120000/workspace ~/.openclaw/

# 重启 Gateway
systemctl restart openclaw

附录
常见问题
Q: Gateway 无法启动?

# 检查端口占用
lsof -i :18789

# 检查配置
openclaw doctor

# 查看详细日志
openclaw gateway run --verbose

Q: WhatsApp 连接断开?

# 重新登录
openclaw channels logout --channel whatsapp
openclaw channels login --channel whatsapp

Q: Agent 响应很慢?

# 检查模型配置
openclaw models status

# 切换到更快的模型
openclaw models set "anthropic/claude-haiku-4-20250514"

Q: 如何重置所有配置?

# 警告:这将删除所有配置和数据
openclaw reset --scope full --yes

CLI 快速参考

# 核心命令
openclaw setup              # 初始化配置
openclaw onboard            # 运行向导
openclaw agent              # 运行 Agent
openclaw gateway run        # 启动 Gateway

# 配置
openclaw config get <path>  # 读取配置
openclaw config set <path> <value>  # 设置配置
openclaw configure          # 配置向导

# 渠道
openclaw channels list      # 列出渠道
openclaw channels add       # 添加渠道
openclaw channels status    # 查看状态

# Agent
openclaw agents list        # 列出 Agent
openclaw agents add <name>  # 添加 Agent
openclaw agents delete <id> # 删除 Agent

# 技能
openclaw skills list        # 列出技能
openclaw skills info <name> # 技能详情

# Cron
openclaw cron list          # 列出定时任务
openclaw cron add           # 添加任务
openclaw cron runs          # 查看运行历史

# 系统
openclaw health             # 健康检查
openclaw logs               # 查看日志
openclaw status             # 系统状态
openclaw doctor             # 诊断问题

配置参考

// ~/.openclaw/openclaw.json 完整示例
{
  // Gateway 配置
  gateway: {
    mode: "local",
    port: 18789,
    bind: "loopback",
    token: "optional-token",
  },

  // Agent 配置
  agents: {
    defaults: {
      workspace: "~/.openclaw/workspace",
      model: {
        primary: "anthropic/claude-sonnet-4-20250514",
      },
      imageModel: {
        primary: "dall-e-3",
      },
      heartbeat: {
        enabled: true,
        every: "2h",
      },
      timeoutSeconds: 600,
      thinking: "off",
      verbose: "on",
    },

    // Agent 列表
    list: [
      {
        id: "default",
        workspace: "~/.openclaw/workspace",
      },
      {
        id: "analytics",
        workspace: "~/.openclaw/agents/analytics",
        model: { primary: "anthropic/claude-opus-4-20250514" },
      },
    ],
  },

  // 渠道配置
  channels: {
    whatsapp: {
      allowFrom: ["+861xxxxxxxxx"],
      groups: ["*"],
    },
    telegram: {
      defaultAccount: "default",
    },
  },

  // 技能配置
  skills: {
    entries: {
      browser: {
        enabled: true,
        config: {
          executablePath: "/usr/bin/google-chrome",
        },
      },
    },

  },

  // Cron 配置
  cron: {
    enabled: true,
    store: "~/.openclaw/cron/jobs.json",
    maxConcurrentRuns: 1,
  },

  // 安全配置
  security: {
    execApprovals: "ask",
  },
}

资源链接
官方文档:https://docs.openclaw.ai
GitHub:GitHub - openclaw/openclaw: Your own personal AI assistant. Any OS. Any Platform. The lobster way. 🦞 · GitHub
技能商店:https://clawhub.com
使用案例:GitHub - hesamsheikh/awesome-openclaw-usecases: A community collection of OpenClaw use cases for making life easier. · GitHub
社区展示:Showcase — What People Are Building with OpenClaw


相关文章:

2 个赞

Docker部署方案很完善

备份恢复流程清晰

1 个赞

生产部署建议很实用

@prompt_gongcheng Docker部署完善 但别忘了数据持久化 容器删了数据就没了 一定要挂载volume保存配置和记忆

@jiqishijue 备份恢复流程清晰很重要 建议写成自动化脚本 定时备份到远端 不要等出事了才想起来没备份