环境:阿里云 ECS · Ubuntu 22.04 · 2 vCPU · 1.6GB RAM · 40GB 磁盘

目录

  1. 架构总览
  2. 环境准备
  3. 安装 OpenClaw
  4. 配置 AI 模型
  5. 接入微信 ClawBot
  6. 内存优化
  7. 多用户隔离
  8. 常用运维命令

1. 架构总览

你的微信  ──→  腾讯 ClawBot  ──→  阿里云 ECS (OpenClaw)  ──→  DeepSeek API
老婆微信  ──→       ↓              ├─ agent:main (你的会话/记忆)
                    ↓              └─ agent:wife (她的会话/记忆)
              微信消息总线
组件说明
OpenClaw开源个人 AI 助手(原 Clawdbot/Moltbot),自托管
ClawBot腾讯微信官方插件,将微信消息桥接到 OpenClaw
DeepSeek国内直连 AI 模型服务商,性价比高

2. 环境准备

2.1 检查环境

# 系统版本
cat /etc/os-release          # Ubuntu 22.04+

# Node.js 版本(需要 ≥ 22)
node --version               # v24.16.0

# 内存
free -h                      # 建议 ≥ 2GB,不够需配 swap

2.2 配置 Swap(1.6GB 小内存机器必需)

sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
free -h   # 验证

2.3 切换 npm 镜像(国内加速)

npm config set registry https://registry.npmmirror.com/

3. 安装 OpenClaw

npm install -g openclaw@latest
openclaw --version   # 验证,需 ≥ 2026.3.22(微信插件要求)

4. 配置 AI 模型

以 DeepSeek 为例(需先在 platform.deepseek.com 获取 API Key):

openclaw onboard --non-interactive --accept-risk \
  --auth-choice deepseek-api-key \
  --deepseek-api-key "sk-你的Key" \
  --install-daemon \
  --skip-channels \
  --skip-ui

其他可选国内模型服务商:

服务商获取地址
DeepSeek ⭐platform.deepseek.com
通义千问(阿里云)dashscope.aliyun.com
Kimi(月之暗面)platform.moonshot.cn
智谱 ChatGLMopen.bigmodel.cn

5. 接入微信 ClawBot

5.1 安装微信插件

openclaw plugins install "@tencent-weixin/openclaw-weixin"
openclaw config set plugins.entries.openclaw-weixin.enabled true
openclaw gateway restart

5.2 扫码绑定

openclaw channels login --channel openclaw-weixin

终端会显示二维码,用手机微信扫描即可。

5.3 开始使用

微信 → 搜索 「微信ClawBot」 → 直接发消息

⚠️ 前置条件:微信版本 ≥ 8.0.70,ClawBot 目前处于灰度测试阶段

6. 内存优化

实践验证:以下优化将 RSS 从 ~500MB(双进程)降至 ~261MB(单进程),1.6G 机器稳定运行。

6.1 禁止双进程 fork(内存减半)

# 写入 shell 环境
echo 'export OPENCLAW_NO_RESPAWN=1' >> ~/.bashrc
source ~/.bashrc

副作用:OpenClaw 自身看门狗被禁用。由 systemd 的 Restart=always 代替(效果相同)。

6.2 限制 Node 堆内存

echo 'export NODE_OPTIONS="--max-old-space-size=768"' >> ~/.bashrc
source ~/.bashrc

6.3 降低并发数

openclaw config set agents.defaults.maxConcurrent 2
openclaw config set agents.defaults.subagents.maxConcurrent 4

6.4 降低 swappiness(减少无谓 swap)

echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf
sudo sysctl -p

默认 swappiness=60,内核会积极 swap。改为 10 后只在物理内存真正不足时才用 swap。

6.5 注入 systemd 服务环境变量

编辑 ~/.config/systemd/user/openclaw-gateway.service,在 [Service] 段追加:

Environment=OPENCLAW_NO_RESPAWN=1
Environment=NODE_OPTIONS=--max-old-space-size=768

然后重载:

systemctl --user daemon-reload
systemctl --user restart openclaw-gateway

6.6 启用 lingering(防止 SSH 断开后服务停止)

sudo loginctl enable-linger $USER

6.7 验证优化效果

free -h                          # 内存使用
ps aux | grep openclaw           # 进程数(应为 1 个)
cat /proc/$(pgrep openclaw)/environ | tr '\0' '\n' | grep -E "NO_RESPAWN|NODE_OPTIONS"

7. 多用户隔离

通过 OpenClaw 的 Agent 机制实现多用户会话隔离。

7.1 创建新 Agent

openclaw agents add wife \
  --model deepseek/deepseek-v4-flash \
  --workspace ~/.openclaw/workspace-wife

7.2 让新用户扫码绑定

openclaw channels login --channel openclaw-weixin

用新用户的微信扫描二维码。

7.3 查看账号并绑定

# 查看所有已连接的微信账号
openclaw channels status --probe

# 绑定(示例 ID,实际以 status 输出为准)
openclaw agents bind --agent main --bind "openclaw-weixin:c8cb5f6d1234-im-bot"
openclaw agents bind --agent wife --bind "openclaw-weixin:ecb625dd1234-im-bot"

# 重启生效
openclaw gateway restart

7.4 验证绑定

openclaw agents bindings

输出示例:

Routing bindings:
- main <- openclaw-weixin accountId=c8cb5f6d1234-im-bot
- wife <- openclaw-weixin accountId=ecb625dd1234-im-bot

7.5 效果

你的微信  →  agent:main  →  ~/.openclaw/workspace       你的会话/记忆
老婆微信  →  agent:wife  →  ~/.openclaw/workspace-wife  她的会话/记忆(互不串台)

内存开销:额外 Agent 空闲时仅增加 ~5-10MB。


8. 常用运维命令

# 服务管理
openclaw start           # 启动
openclaw stop            # 停止
openclaw gateway restart # 重启
openclaw status          # 查看状态

# 日志
openclaw logs --tail 50
openclaw logs --follow

# 配置
openclaw configure --section model    # 改模型
openclaw onboard                      # 重新运行向导

# Agent 管理
openclaw agents list                  # 列出所有 Agent
openclaw agents bindings              # 查看路由绑定
openclaw agents add <name>            # 新建 Agent
openclaw agents delete <name>         # 删除 Agent

# 渠道管理
openclaw channels list                # 列出渠道
openclaw channels status --probe      # 渠道状态详情
openclaw channels login --channel openclaw-weixin  # 绑定新微信

# 系统
systemctl --user status openclaw-gateway   # systemd 状态
systemctl --user restart openclaw-gateway  # systemd 重启

附录:关键文件路径

文件/目录用途
~/.openclaw/openclaw.json主配置文件
~/.openclaw/workspace/默认 Agent 工作区
~/.openclaw/agents/main/main Agent 状态
~/.openclaw/agents/wife/wife Agent 状态
~/.openclaw/agents/main/sessions/会话记录
~/.config/systemd/user/openclaw-gateway.servicesystemd 服务定义

标签: openclaw

添加新评论