全部教程OpenClaw VPS 部署指南(无 Docker,Ubuntu 22.04)

OpenClaw VPS 部署指南(无 Docker,Ubuntu 22.04)

我是 Quentin,OpenClaw 作者。这篇是把 OpenClaw 装到一台真正的 VPS 上、跑 systemd、上 SSL、能 24/7 在线的完整教程。

不用 Docker —— 因为 2 核 4G 的入门 VPS 套 Docker 很容易吃完内存。直接装更省,崩了好查。

适用环境:Ubuntu 22.04 / 2 核 4GB / 入门型 VPS(腾讯云、阿里云、Vultr、DigitalOcean 都行)

想跳过手工配置直接接拼车 token:

curl -fsSL https://cp.bizq.net/setup.sh | bash -s -- claude-max-20x

目录

  1. 初始化服务器
  2. 安装 OpenClaw
  3. 安全加固
  4. Nginx 反向代理 + SSL
  5. 配置消息频道
  6. 开机自启(systemd)
  7. 维护与更新
  8. 常见问题排查

1. 初始化服务器

1.1 SSH 登录

ssh root@<你的VPS公网IP>

1.2 更新系统

apt update && apt dist-upgrade -y

1.3 加 Swap(4GB 内存机器必做

4GB 跑 OpenClaw 刚好够,遇到大上下文容易 OOM,加 4G swap 给自己留个安全垫:

sudo fallocate -l 4G /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

1.4 创建专用用户(别用 root 跑 OpenClaw

adduser openclaw
usermod -aG sudo openclaw
su - openclaw

后续操作都在 openclaw 用户下进行。

1.5 安装基础依赖

sudo apt install -y curl wget git build-essential

1.6 安装 Node.js 22

OpenClaw 基于 Node.js 22 LTS,用 nvm 装最干净:

# 安装 nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash
 
# 加载
export NVM_DIR="$HOME/.nvm"
source "$NVM_DIR/nvm.sh"
 
# 安装 Node 22
nvm install 22
nvm use 22
nvm alias default 22
 
# 验证
node --version   # 应当是 v22.x.x
npm --version

2. 安装 OpenClaw

2.1 跑官方安装脚本

curl -fsSL https://openclaw.ai/install.sh | bash

向导会依次问你:

步骤选择
部署模式QuickStart
AI 提供商你的 API 类型(Anthropic / OpenAI / Gemini)
消息频道要接的平台(Telegram / WhatsApp / Discord 等)
包管理器npm

2.2 配置 API 密钥

最快路径:直接用 OpenClaw 拼车,一行搞定 + 自动写到 ~/.openclaw/openclaw.json 的 auth profile:

curl -fsSL https://cp.bizq.net/setup.sh | bash -s -- claude-max-20x

手动路径

openclaw configure

或者直接写环境变量到 ~/.bashrc 末尾:

cat >> ~/.bashrc << 'EOF'
 
# OpenClaw API Keys
export ANTHROPIC_API_KEY="sk-ant-你的密钥"
# export OPENAI_API_KEY="sk-你的密钥"
# export GEMINI_API_KEY="你的密钥"
EOF
 
source ~/.bashrc

完整 CLI 速查见 OpenClaw CLI 命令完整参考

2.3 把 Gateway 绑到 loopback(重要安全步骤

Gateway 不能暴露公网,必须只听 127.0.0.1:

openclaw configure
# 选 "Local (this machine)"

或者直接:

openclaw config set gateway.mode local
openclaw config set gateway.bind loopback

2.4 启动并验证

# 启动
openclaw start
 
# 健康检查
curl -fsS http://127.0.0.1:18789/healthz

返回 ok 就装好了。


3. 安全加固

3.1 云厂安全组只开 22 / 80 / 443

端口协议用途
22TCPSSH
80TCPHTTP(Nginx)
443TCPHTTPS(Nginx + SSL)

绝对不要在安全组里开 18789。 那是 Gateway 内部端口,外网能访问 = 你的 agent 可以被任何人调用。

3.2 UFW 防火墙

sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow 22/tcp
sudo ufw limit 22/tcp
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable
 
# 看一眼规则
sudo ufw status verbose

3.3 Fail2Ban(防暴力 SSH 破解)

sudo apt install -y fail2ban
 
sudo tee /etc/fail2ban/jail.local << 'EOF'
[sshd]
enabled = true
port = 22
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
bantime = 3600
findtime = 600
EOF
 
sudo systemctl enable fail2ban
sudo systemctl start fail2ban

3.4 关 SSH 密码登录(先确认密钥能登录再做

# 千万先在另一个终端测好密钥登录!
sudo sed -i 's/#PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config
sudo sed -i 's/PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config
sudo systemctl restart sshd

3.5 OpenClaw 自带的安全审计

openclaw security audit --deep

会把所有暴露面、过期密钥、危险权限都列出来,配合 --fix 可以自动修。


4. Nginx 反向代理 + SSL

4.1 装 Nginx

sudo apt install -y nginx

4.2 站点配置

your-domain.com 换成你的域名:

sudo tee /etc/nginx/sites-available/openclaw << 'EOF'
server {
    listen 80;
    server_name your-domain.com;
 
    location / {
        proxy_pass http://127.0.0.1:18789;
        proxy_http_version 1.1;
 
        # WebSocket(OpenClaw 必需)
        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;
        proxy_set_header X-Forwarded-Proto $scheme;
 
        # 长连接超时
        proxy_read_timeout 86400;
        proxy_send_timeout 86400;
    }
}
EOF
 
# 启用站点
sudo ln -s /etc/nginx/sites-available/openclaw /etc/nginx/sites-enabled/
sudo rm -f /etc/nginx/sites-enabled/default
 
# 测试 + reload
sudo nginx -t && sudo systemctl reload nginx

4.3 域名解析

云厂 DNS 控制台加 A 记录:

  • 主机记录:@ 或子域名(如 claw
  • 记录值:你的 VPS 公网 IP

4.4 装 SSL 证书(Let’s Encrypt)

sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d your-domain.com
 
# 自动续期 dry-run
sudo certbot renew --dry-run

没域名也能跑(直接 http://你的IP),但生产环境强烈不推荐


5. 配置消息频道

Telegram

# 1. 找 @BotFather 创建 Bot 拿 token
# 2. 添加频道
openclaw channels add --channel telegram --token "<你的Bot Token>"

WhatsApp

openclaw channels login
# 终端会显示 QR,手机 WhatsApp → 设置 → 关联设备 → 扫码

Discord

# 1. Discord Developer Portal 创建 Bot 拿 token
# 2. 添加频道
openclaw channels add --channel discord --token "<你的Bot Token>"

6. 开机自启(systemd)

让 OpenClaw 在重启后自动起来:

sudo tee /etc/systemd/system/openclaw.service << EOF
[Unit]
Description=OpenClaw AI Assistant
After=network.target
 
[Service]
Type=simple
User=openclaw
WorkingDirectory=/home/openclaw
Environment=NODE_ENV=production
Environment=PATH=/home/openclaw/.nvm/versions/node/v22.0.0/bin:/usr/bin:/bin
ExecStart=/home/openclaw/.nvm/versions/node/v22.0.0/bin/openclaw start
Restart=on-failure
RestartSec=10
 
[Install]
WantedBy=multi-user.target
EOF

⚠️ v22.0.0 必须替换为你实际安装的版本。node --version 看一眼。

# 启用 + 启动
sudo systemctl daemon-reload
sudo systemctl enable openclaw
sudo systemctl start openclaw
 
# 状态
sudo systemctl status openclaw

或者用 OpenClaw 内置的 systemd 集成(更省事):

openclaw gateway install
openclaw gateway start

systemd 常用命令

sudo systemctl start openclaw     # 启动
sudo systemctl stop openclaw      # 停止
sudo systemctl restart openclaw   # 重启
sudo systemctl status openclaw    # 状态
journalctl -u openclaw -f         # 实时日志
journalctl -u openclaw --since "1 hour ago"

7. 维护与更新

更新 OpenClaw

sudo systemctl stop openclaw
 
# 用 CLI 自带的 update(推荐)
openclaw update
 
# 或者重跑安装脚本
curl -fsSL https://openclaw.ai/install.sh | bash
 
sudo systemctl start openclaw
curl -fsS http://127.0.0.1:18789/healthz

备份

tar -czf openclaw-backup-$(date +%Y%m%d).tar.gz \
  ~/.openclaw \
  ~/.bashrc
 
# 推荐定期上传到对象存储

或者用 OpenClaw 自带的备份:

openclaw backup create
openclaw backup verify

监控磁盘 / 内存

df -h               # 磁盘
du -sh ~/.openclaw  # OpenClaw 数据
free -h             # 内存

8. 常见问题排查

服务起不来

journalctl -u openclaw -n 50 --no-pager
sudo ss -tlnp | grep 18789
openclaw start         # 前台跑看 stderr

内存不足 / OOM

free -h
swapon --show          # 确认 swap 在
top -o %MEM            # 看谁吃内存

Gateway 不通

curl -fsS http://127.0.0.1:18789/healthz
sudo nginx -t
sudo ufw status
openclaw doctor        # 一站式诊断

WhatsApp 掉线

sudo systemctl restart openclaw
openclaw channels login

API 密钥不对

env | grep API_KEY
grep API_KEY ~/.bashrc
openclaw models status --probe   # 看模型是否能 ping 通

快速参考

# === 服务 ===
sudo systemctl start openclaw
sudo systemctl stop openclaw
sudo systemctl restart openclaw
sudo systemctl status openclaw
 
# === 日志 ===
journalctl -u openclaw -f
 
# === 健康 ===
curl -fsS http://127.0.0.1:18789/healthz
openclaw doctor
 
# === 配置 ===
openclaw configure
 
# === 频道 ===
openclaw channels login
openclaw channels add --channel telegram --token "TOKEN"
 
# === 备份 ===
openclaw backup create

推荐配置总结

推荐备注
CPU2 核满足下限
内存4 GB + 4 GB Swap必加 swap
带宽30 Mbps文本交互绰绰有余
系统Ubuntu 22.04也支持 Debian 12 / Ubuntu 24.04
安装方式直接安装(非 Docker入门 VPS 套 Docker 内存吃紧
适合场景纯文本 + 单/双频道 + 云端 LLM不要在这种机器上跑本地 LLM

立即开始

curl -fsSL https://cp.bizq.net/setup.sh | bash -s -- claude-max-20x

完整文档:docs.openclaw.ai | GitHub:github.com/openclaw/openclaw


相关文章