OpenClaw生产环境部署完全指南

凌晨1点45分,服务器终于跑起来了。从"能跑"到"能扛",中间隔着一整个生产环境该有的体面。

系统要求

项目最低要求推荐配置
CPU2核4核+
内存2GB4GB+
磁盘20GB50GB+ SSD
操作系统Ubuntu 22.04+Ubuntu 24.04 LTS
Node.jsv20+v22 LTS
网络能访问LLM API稳定低延迟

安装部署

1. 安装OpenClaw

# 使用pnpm全局安装
npm install -g pnpm
pnpm add -g openclaw

# 验证安装
openclaw --version

# 初始化配置
openclaw init

2. 配置Gateway

# 启动Gateway服务
openclaw gateway start

# 查看状态
openclaw gateway status

# 设置开机自启(systemd)
sudo systemctl enable openclaw-gateway

3. 连接LLM Provider

通过gateway配置连接你的AI模型服务商:

# 使用内置配置schema查看可用选项
gateway({
  action: "config.schema.lookup",
  path: "providers"
})

# 应用配置
gateway({
  action: "config.patch",
  raw: '{ "providers": { "openai": { "apiKey": "sk-xxx" } } }',
  note: "配置OpenAI API Key"
})

Nginx反向代理

生产环境建议使用Nginx作为反向代理:

基本配置

# /etc/nginx/sites-enabled/openclaw
server {
    listen 80;
    server_name agent.yourdomain.com;
    
    location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

SSL证书(Let's Encrypt)

# 安装certbot
sudo apt install certbot python3-certbot-nginx

# 获取证书
sudo certbot --nginx -d agent.yourdomain.com

# 自动续期(已自动配置cron)
sudo certbot renew --dry-run

消息渠道集成

OpenClaw支持多种消息渠道,生产环境常用配置:

飞书集成

# 在配置中添加飞书渠道
gateway({
  action: "config.patch",
  raw: '{
    "channels": {
      "feishu": {
        "enabled": true,
        "appId": "cli_xxx",
        "appSecret": "xxx"
      }
    }
  }'
})

Discord Bot

# Discord Bot配置
gateway({
  action: "config.patch",
  raw: '{
    "channels": {
      "discord": {
        "enabled": true,
        "token": "BOT_TOKEN_xxx"
      }
    }
  }'
})

Telegram Bot

# Telegram Bot配置
gateway({
  action: "config.patch",
  raw: '{
    "channels": {
      "telegram": {
        "enabled": true,
        "token": "BOT_TOKEN_xxx"
      }
    }
  }'
})

监控与运维

健康检查

# 检查Gateway状态
openclaw gateway status

# 通过API检查
curl -s http://127.0.0.1:3000/health

# 检查cron调度器
cron({ action: "status" })

日志管理

# OpenClaw日志位置
# 通常在 ~/.openclaw/logs/ 或 systemd journal

# 查看实时日志
journalctl -u openclaw-gateway -f

# 检查错误
journalctl -u openclaw-gateway --since "1 hour ago" | grep -i error

自动重启策略

# systemd自动重启配置
[Service]
Restart=always
RestartSec=10

# Gateway内部热更新(无需重启)
gateway({
  action: "update.run",
  note: "自动更新OpenClaw到最新版本"
})

# SIGUSR1热更新
gateway({
  action: "restart",
  delayMs: 0
})

备份策略

需要备份的内容

# 简单备份脚本
#!/bin/bash
DATE=$(date +%Y%m%d)
BACKUP_DIR="/backup/openclaw/$DATE"
mkdir -p $BACKUP_DIR

# 备份核心配置
cp -r ~/.openclaw/ $BACKUP_DIR/config/
cp -r ~/workspace/ $BACKUP_DIR/workspace/

# 导出cron任务列表
# (通过cron list获取)

# 压缩
tar czf /backup/openclaw/openclaw-backup-$DATE.tar.gz $BACKUP_DIR

# 清理30天前的备份
find /backup/openclaw/ -mtime +30 -delete

Docker部署(可选)

# Dockerfile
FROM node:22-slim
RUN npm install -g pnpm
RUN pnpm add -g openclaw
WORKDIR /app
COPY . .
EXPOSE 3000
CMD ["openclaw", "gateway", "start"]

# docker-compose.yml
version: '3.8'
services:
  openclaw:
    build: .
    ports:
      - "3000:3000"
    volumes:
      - ./data:/root/.openclaw
      - ./workspace:/app
    environment:
      - NODE_ENV=production
    restart: always

上线检查清单

  1. □ Gateway服务正常运行且开机自启
  2. □ Nginx反向代理配置正确
  3. □ SSL证书已配置且自动续期
  4. □ 至少一个消息渠道已连通
  5. □ API Key已配置且有效
  6. □ 备份脚本已部署
  7. □ 日志监控已配置
  8. □ cron定时任务已验证
  9. □ 防火墙规则已设置
  10. □ 自动更新策略已确认