Skip to content

生产部署

概述

本指南面向需要 OpenClaw 7×24 小时稳定运行的用户,涵盖服务器选型、服务管理、安全加固和监控维护。

前置要求

  • 一台 Linux 服务器(VPS 或物理机)
  • 基本的 Linux 命令行使用经验
  • 已完成 基础安装

服务管理

Systemd 守护进程(推荐)

使用内置命令一键创建系统服务:

bash
openclaw onboard --install-daemon

这会自动:

  • 创建 /etc/systemd/system/openclaw.service 服务文件
  • 配置开机自启动
  • 设置自动重启策略

手动配置 Systemd

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

[Service]
Type=simple
User=openclaw
WorkingDirectory=/home/openclaw
ExecStart=/usr/local/bin/openclaw gateway start
Restart=always
RestartSec=10
Environment=NODE_ENV=production

# 安全加固
NoNewPrivileges=true
ProtectSystem=strict
ReadWritePaths=/home/openclaw/.openclaw

[Install]
WantedBy=multi-user.target
bash
sudo systemctl daemon-reload
sudo systemctl enable openclaw
sudo systemctl start openclaw

服务管理命令

bash
# 启动/停止/重启
sudo systemctl start openclaw
sudo systemctl stop openclaw
sudo systemctl restart openclaw

# 查看状态
sudo systemctl status openclaw

# 查看日志
sudo journalctl -u openclaw -f
sudo journalctl -u openclaw --since "1 hour ago"

安全加固

1. 创建专用用户

bash
sudo useradd -m -s /bin/bash openclaw
sudo -u openclaw bash

2. 环境变量存储密钥

不要在配置文件中明文存储 API Key:

bash
# 正确做法:使用环境变量
echo 'DEEPSEEK_API_KEY=sk-xxxxx' >> ~/.openclaw/.env

# 限制文件权限
chmod 600 ~/.openclaw/.env

3. 防火墙配置

bash
# UFW 基本配置
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow ssh
sudo ufw enable

# 不要直接暴露 OpenClaw 端口!
# 通过 Tailscale 或反向代理访问

4. 绑定 localhost

bash
openclaw config set gateway.host "127.0.0.1"
openclaw config set gateway.auth.enabled true

5. 定期轮换密钥

建议每 90 天更换一次 API Key,尤其在怀疑泄露时立即更换。

Docker 生产部署

Docker Compose 配置

yaml
version: '3.8'
services:
  openclaw:
    image: openclaw/openclaw:latest
    restart: unless-stopped
    ports:
      - "127.0.0.1:18789:18789"
    volumes:
      - openclaw-data:/root/.openclaw
    environment:
      - TZ=Asia/Shanghai
      - NODE_ENV=production
    deploy:
      resources:
        limits:
          memory: 2G
          cpus: '2'

volumes:
  openclaw-data:

沙箱模式

Docker 部署支持沙箱隔离,防止 Agent 意外操作宿主系统:

bash
openclaw config set agents.defaults.sandbox.mode non-main

健康检查与监控

内置健康检查

bash
# 基础状态
openclaw status

# 深度检查
openclaw status --deep

# 自动诊断
openclaw doctor

健康检查端点

OpenClaw 提供 HTTP 健康检查端点:

  • 存活检查GET /healthz — 进程是否运行
  • 就绪检查GET /readyz — 是否准备好处理请求

可以配合监控系统(如 UptimeRobot、Prometheus)使用。

日志管理

bash
# 实时查看
openclaw logs --follow

# 最近 100 条
openclaw logs --limit 100

# JSON 格式(便于日志分析)
openclaw logs --json

配置日志级别:

json
{
  "gateway": {
    "logging": {
      "level": "info"
    }
  }
}

备份与恢复

自动备份

bash
# 创建备份脚本
cat > /home/openclaw/backup.sh << 'EOF'
#!/bin/bash
BACKUP_DIR="/home/openclaw/backups"
mkdir -p "$BACKUP_DIR"
tar -czf "$BACKUP_DIR/openclaw-$(date +%Y%m%d).tar.gz" ~/.openclaw/
# 保留最近 30 天
find "$BACKUP_DIR" -name "*.tar.gz" -mtime +30 -delete
EOF

chmod +x /home/openclaw/backup.sh

# 添加定时任务(每天凌晨 3 点)
(crontab -l 2>/dev/null; echo "0 3 * * * /home/openclaw/backup.sh") | crontab -

恢复

bash
# 停止服务
sudo systemctl stop openclaw

# 恢复备份
tar -xzf backups/openclaw-20260310.tar.gz -C ~/

# 重启
sudo systemctl start openclaw

磁盘管理

会话历史和记忆文件会逐渐增长,建议定期清理:

bash
# 清理 30 天前的每日记忆
find ~/.openclaw/workspace/memory/ -name "*.md" -mtime +30 -delete

# 查看磁盘占用
du -sh ~/.openclaw/
du -sh ~/.openclaw/workspace/memory/

升级

bash
# 检查当前版本
openclaw --version

# 升级到最新版
npm update -g openclaw

# 重启服务
sudo systemctl restart openclaw

升级前备份

每次升级前建议执行一次备份,以防升级导致兼容性问题。

性能优化

优化项建议
减少活跃 Skill保持 5-10 个常用即可
使用更快的模型简单任务用小模型
启用缓存减少重复 API 调用
增大心跳间隔减少后台 Token 消耗
交换分区小内存 VPS 开启 2GB swap

基于 MIT 协议发布