OpenClaw 部署与运维详细完整教程
本教程提供从安装到生产环境运维的完整 OpenClaw 部署指南,涵盖多种部署场景和最佳实践。

📋 目录
- OpenClaw 简介
- 系统要求与环境准备
- 安装部署
- 基础配置
- Gateway 配置详解
- 通道配置
- 安全与认证
- 远程访问配置
- 运维与监控
- 故障排查
- 备份与恢复
- 升级维护
一、OpenClaw 简介
1.1 什么是 OpenClaw?
OpenClaw 是一个运行在本地设备的个人 AI 助手,具有以下特点:
- 本地优先:数据和处理都在你的设备上
- 多通道支持:WhatsApp、Telegram、Slack、Discord、飞书等 20+ 通讯平台
- 统一控制平面:通过 Gateway WebSocket 管理所有会话和工具
- 可扩展:支持 Skills 插件系统
1.2 核心架构
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| ┌─────────────────────────────────────────────────────────────┐ │ 用户设备 │ │ ┌─────────────┐ ┌──────────────┐ ┌─────────────────┐ │ │ │ WhatsApp │ │ Telegram │ │ Discord │ │ │ └──────┬──────┘ └──────┬───────┘ └────────┬────────┘ │ │ │ │ │ │ │ └──────────────────┼─────────────────────┘ │ │ ▼ │ │ ┌─────────────────┐ │ │ │ Gateway │ │ │ │ (控制平面) │ │ │ │ ws://localhost │ │ │ └────────┬────────┘ │ │ │ │ │ ┌──────────────────┼──────────────────┐ │ │ ▼ ▼ ▼ │ │ ┌─────────────┐ ┌──────────────┐ ┌─────────────────┐ │ │ │ CLI 工具 │ │ WebChat │ │ 移动端 App │ │ │ └─────────────┘ └──────────────┘ └─────────────────┘ │ └─────────────────────────────────────────────────────────────┘
|
1.3 核心组件
| 组件 |
说明 |
用途 |
| Gateway |
WebSocket 控制平面 |
会话管理、工具调度、事件分发 |
| Agent |
AI 助手运行时 |
处理消息、调用工具 |
| Channels |
通讯通道 |
连接各种聊天平台 |
| Skills |
插件系统 |
扩展功能 |
| Nodes |
设备节点 |
移动端配对、设备控制 |
二、系统要求与环境准备
2.1 硬件要求
| 场景 |
CPU |
内存 |
存储 |
网络 |
| 个人使用 |
2+ 核 |
4GB+ |
10GB+ |
宽带 |
| 小团队 |
4+ 核 |
8GB+ |
20GB+ |
稳定连接 |
| 生产环境 |
8+ 核 |
16GB+ |
50GB+ |
公网 IP/域名 |
2.2 软件要求
- Node.js: 24 (推荐) 或 22.16+
- npm: 10+ 或 pnpm/bun
- 操作系统:
- macOS 12+
- Linux (Ubuntu 20.04+, Debian 11+, CentOS 8+)
- Windows 10/11 (WSL2 强烈推荐)
2.3 环境检查
1 2 3 4 5 6 7 8 9 10 11
| node --version
npm --version
uname -m
netstat -tlnp | grep 18789
|
2.4 网络要求
| 用途 |
端口 |
协议 |
说明 |
| Gateway |
18789 |
WebSocket |
核心控制平面 |
| WebChat |
18789 |
HTTP/WebSocket |
内置 Web 界面 |
| 通道 Webhook |
configurable |
HTTP |
接收平台消息 |
三、安装部署
3.1 标准安装(推荐)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| npm i -g openclaw
pnpm add -g openclaw
openclaw --version
curl -fsSL https://openclaw.ai/install.sh | bash
powershell -c "irm https://openclaw.ai/install.ps1 | iex"
|
3.2 交互式初始化
1 2
| openclaw onboard --install-daemon
|
这个命令会:
- 创建配置文件
~/.openclaw/openclaw.json
- 初始化工作区
~/.openclaw/workspace
- 安装 Gateway 守护进程(systemd/launchd)
- 引导配置模型和通道
3.3 开发环境安装
1 2 3 4 5 6 7 8 9 10
| git clone https://github.com/openclaw/openclaw.git cd openclaw
pnpm install pnpm ui:build pnpm build
pnpm openclaw onboard --install-daemon
|
3.4 Docker 部署
1 2 3 4 5 6 7 8 9 10 11 12 13
| docker pull openclaw/openclaw:latest
docker run -d \ --name openclaw \ -p 18789:18789 \ -v ~/.openclaw:/root/.openclaw \ -e OPENAI_API_KEY=your_key \ openclaw/openclaw:latest
docker logs -f openclaw
|
3.5 Nix 部署
1 2 3 4 5 6 7 8
| { inputs.openclaw.url = "github:openclaw/nix-openclaw"; outputs = { self, nixpkgs, openclaw }: { }; }
|
四、基础配置
4.1 配置文件结构
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| ~/.openclaw/ ├── openclaw.json # 主配置文件 ├── openclaw.json.bak # 自动备份 ├── workspace/ # 工作区 │ ├── AGENTS.md # 代理配置 │ ├── SOUL.md # 个性定义 │ ├── TOOLS.md # 工具配置 │ ├── USER.md # 用户信息 │ ├── BOOTSTRAP.md # 首次引导(可删除) │ ├── HEARTBEAT.md # 心跳任务 │ └── skills/ # 技能目录 ├── credentials/ # 凭据存储 ├── logs/ # 日志文件 ├── canvas/ # Canvas 数据 └── devices/ # 设备配对信息
|
4.2 最小配置示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| { "agent": { "model": "anthropic/claude-opus-4-6" }, "gateway": { "port": 18789, "mode": "local", "bind": "loopback", "auth": { "mode": "token", "token": "your-secure-token-here" } }, "channels": { "telegram": { "enabled": true, "botToken": "${TELEGRAM_BOT_TOKEN}" } } }
|
4.3 使用 CLI 配置
1 2 3 4 5 6 7 8 9 10 11 12
| openclaw configure
openclaw config get
openclaw config set gateway.port 19001 openclaw config set agent.model "openai/gpt-4o"
openclaw config validate
|
五、Gateway 配置详解
5.1 Gateway 核心配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| { "gateway": { "port": 18789, "mode": "local", "bind": "loopback", "auth": { "mode": "token", "token": "secure-token", "allowTailscale": true }, "controlUi": { "allowInsecureAuth": true }, "tailscale": { "mode": "off", "resetOnExit": false } } }
|
5.2 启动 Gateway
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| openclaw gateway run --verbose
openclaw gateway start
openclaw gateway status
openclaw gateway health
openclaw gateway stop
openclaw gateway restart
|
5.3 绑定模式详解
| 模式 |
说明 |
适用场景 |
loopback |
仅本地访问 (127.0.0.1) |
单机使用 |
lan |
局域网可访问 |
家庭/办公室网络 |
tailnet |
Tailscale 网络 |
远程安全访问 |
auto |
自动检测 |
动态环境 |
custom |
自定义绑定 |
高级配置 |
5.4 认证模式详解
| 模式 |
说明 |
安全级别 |
none |
无认证 |
⚠️ 仅开发 |
token |
共享令牌 |
✅ 推荐 |
password |
密码认证 |
✅ 高 |
trusted-proxy |
信任代理 |
⚠️ 需配合反向代理 |
六、通道配置
6.1 Telegram 配置
1 2 3 4 5 6
| export TELEGRAM_BOT_TOKEN="123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11"
openclaw config set channels.telegram.botToken "your-token" openclaw config set channels.telegram.enabled true
|
配置选项:
1 2 3 4 5 6 7 8 9 10 11 12
| { "channels": { "telegram": { "enabled": true, "botToken": "${TELEGRAM_BOT_TOKEN}", "dmPolicy": "pairing", "allowFrom": ["@username"], "webhookUrl": "", "webhookSecret": "" } } }
|
6.2 Discord 配置
1
| export DISCORD_BOT_TOKEN="your-bot-token"
|
1 2 3 4 5 6 7 8 9 10 11 12
| { "channels": { "discord": { "enabled": true, "token": "${DISCORD_BOT_TOKEN}", "dmPolicy": "pairing", "allowFrom": [], "guilds": [], "mediaMaxMb": 25 } } }
|
6.3 Slack 配置
1 2
| export SLACK_BOT_TOKEN="xoxb-your-token" export SLACK_APP_TOKEN="xapp-your-token"
|
6.4 飞书 (Feishu) 配置
1 2 3 4 5 6 7 8 9 10 11 12 13
| { "channels": { "feishu": { "enabled": true, "appId": "cli_xxx", "appSecret": "xxx", "domain": "feishu", "connectionMode": "websocket", "groupPolicy": "open", "dmPolicy": "pairing" } } }
|
6.5 WhatsApp 配置
1 2 3 4
| openclaw channels login --channel whatsapp
|
6.6 通道安全设置
1 2 3 4 5 6 7 8 9 10 11 12 13
| { "channels": { "telegram": { "dmPolicy": "pairing", "allowFrom": ["@admin"], "groups": { "*": { "requireMention": true } } } } }
|
七、安全与认证
7.1 DM 配对机制
默认情况下,未知发送者需要配对才能与助手交互:
1 2 3 4 5 6 7 8
| openclaw pairing list
openclaw pairing approve telegram ABC123
openclaw pairing deny telegram ABC123
|
7.2 Gateway Token 管理
1 2 3 4 5 6 7 8
| openssl rand -hex 32
openclaw config set gateway.auth.token "your-new-token"
openclaw gateway restart
|
7.3 沙箱配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| { "agents": { "defaults": { "sandbox": { "mode": "non-main", "allowlist": [ "bash", "process", "read", "write", "edit" ], "denylist": [ "browser", "canvas", "nodes", "cron" ] } } } }
|
7.4 安全检查
1 2 3 4 5
| openclaw doctor
openclaw security audit
|
八、远程访问配置
8.1 Tailscale 集成
1 2 3 4 5 6 7 8 9
| { "gateway": { "bind": "loopback", "tailscale": { "mode": "serve", "resetOnExit": false } } }
|
启动:
1 2 3 4 5
| openclaw gateway run --tailscale serve
openclaw gateway run --tailscale funnel --auth password
|
8.2 SSH 隧道
1 2 3 4 5 6 7 8
| ssh -L 18789:localhost:18789 user@remote-server
ssh -L 18888:127.0.0.1:18789 [email protected]
ssh -R 18789:localhost:18789 user@remote-server
|
8.3 反向代理配置
Nginx 配置:
1 2 3 4 5 6 7 8 9 10 11 12 13
| server { listen 443 ssl; server_name openclaw.yourdomain.com; location / { proxy_pass http://localhost:18789; 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; } }
|
Caddy 配置:
1 2 3
| openclaw.yourdomain.com { reverse_proxy localhost:18789 }
|
九、运维与监控
9.1 健康检查
1 2 3 4 5 6 7 8
| openclaw health
openclaw gateway probe
openclaw status
|
9.2 日志管理
1 2 3 4 5 6 7 8
| openclaw logs
openclaw logs --since 1h
openclaw gateway run --log-level debug
|
9.3 监控指标
1 2 3 4 5 6 7 8
| openclaw gateway usage-cost
openclaw sessions list
openclaw gateway call health
|
9.4 自动化任务
1 2 3 4 5 6 7 8 9 10
| openclaw cron add --name "daily-backup" \ --schedule "0 2 * * *" \ --command "openclaw backup create"
openclaw cron list
openclaw cron remove daily-backup
|
9.5 心跳检测
编辑 workspace/HEARTBEAT.md:
1 2 3 4 5
| # 心跳任务清单
- [ ] 检查未读邮件 - [ ] 检查日历事件 - [ ] 检查系统健康
|
十、故障排查
10.1 常见问题
Gateway 无法启动
1 2 3 4 5 6 7 8
| lsof -i :18789
openclaw gateway run --force
openclaw gateway run --verbose
|
通道连接失败
1 2 3 4 5 6 7 8
| openclaw channels status
openclaw channels login --channel telegram
openclaw logs --filter telegram
|
认证问题
1 2 3 4 5 6 7 8 9 10 11
| openclaw config get gateway.auth.token
openssl rand -hex 32
openclaw dashboard --no-open
echo $OPENCLAW_GATEWAY_TOKEN
|
10.2 诊断工具
1 2 3 4 5 6 7 8 9 10 11
| openclaw doctor
openclaw config validate
openclaw gateway discover
openclaw gateway call health
|
10.3 调试模式
1 2 3 4 5 6 7 8
| openclaw gateway run --verbose --ws-log full
openclaw gateway run --raw-stream --raw-stream-path ./stream.jsonl
openclaw --dev gateway run
|
十一、备份与恢复
11.1 创建备份
1 2 3 4 5 6 7 8 9 10 11 12
| openclaw backup create
openclaw backup create --output ~/backups/openclaw-$(date +%Y%m%d).tar.gz
BACKUP_DIR="$HOME/backups/openclaw" mkdir -p "$BACKUP_DIR" openclaw backup create --output "$BACKUP_DIR/backup-$(date +%Y%m%d-%H%M%S).tar.gz" find "$BACKUP_DIR" -name "*.tar.gz" -mtime +30 -delete
|
11.2 恢复备份
1 2 3 4 5
| openclaw backup restore --file ~/backups/openclaw-20240331.tar.gz
openclaw backup verify --file ~/backups/openclaw-20240331.tar.gz
|
11.3 备份内容
备份包含:
openclaw.json - 主配置
workspace/ - 工作区文件
credentials/ - 加密凭据
sessions/ - 会话历史
skills/ - 自定义技能
十二、升级维护
12.1 检查更新
1 2 3 4 5
| openclaw update status
openclaw --version
|
12.2 执行升级
1 2 3 4 5 6 7 8
| npm install -g openclaw@latest
npm install -g [email protected]
openclaw gateway restart
|
12.3 升级频道
1 2 3 4 5 6 7 8
| openclaw update --channel beta
openclaw update --channel dev
openclaw update --channel stable
|
12.4 升级前检查
1 2 3 4 5 6 7 8
| openclaw doctor
openclaw backup create --output ~/pre-upgrade-backup.tar.gz
openclaw update --dry-run
|
附录
A. 环境变量参考
| 变量 |
说明 |
示例 |
OPENCLAW_GATEWAY_TOKEN |
Gateway 认证令牌 |
abc123... |
OPENAI_API_KEY |
OpenAI API 密钥 |
sk-... |
ANTHROPIC_API_KEY |
Anthropic API 密钥 |
sk-ant-... |
TELEGRAM_BOT_TOKEN |
Telegram Bot Token |
123456:ABC... |
DISCORD_BOT_TOKEN |
Discord Bot Token |
... |
SLACK_BOT_TOKEN |
Slack Bot Token |
xoxb-... |
SLACK_APP_TOKEN |
Slack App Token |
xapp-... |
B. CLI 命令速查
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| openclaw --version openclaw onboard openclaw doctor
openclaw gateway run openclaw gateway start openclaw gateway stop openclaw gateway status openclaw gateway restart
openclaw config get openclaw config set <path> <value> openclaw config validate
openclaw channels login openclaw channels status
openclaw message send openclaw agent --message "..."
openclaw backup create openclaw backup restore
|
C. 配置文件模板
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| { "meta": { "lastTouchedVersion": "2026.3.28", "lastTouchedAt": "2026-03-31T00:00:00.000Z" }, "auth": { "profiles": { "openai:default": { "provider": "openai", "mode": "api_key" } } }, "models": { "mode": "merge", "providers": { "openai": { "baseUrl": "https://api.openai.com/v1", "api": "openai-completions" } } }, "agents": { "defaults": { "model": { "primary": "openai/gpt-4o" }, "workspace": "~/.openclaw/workspace" } }, "gateway": { "port": 18789, "mode": "local", "bind": "loopback", "auth": { "mode": "token", "token": "${OPENCLAW_GATEWAY_TOKEN}" } }, "channels": {}, "tools": { "profile": "coding", "web": { "search": { "enabled": true, "provider": "duckduckgo" } } } }
|
D. 参考链接
结语
OpenClaw 是一个功能强大且灵活的本地 AI 助手平台。通过本指南,你应该能够:
- 在各种环境中部署 OpenClaw
- 配置多个通讯通道
- 确保系统安全
- 进行日常运维和故障排查
- 保持系统更新