一、VPS 选购(2026 推荐)

国外(推荐,直连 OpenAI)

  • Hetzner CX22:2 核 4G/40G SSD,€4.5 / 月,性能强、稳定docs.openclaw.ai
  • Vultr/DO:2 核 4G,$20–24 / 月,全球机房
  • 系统Ubuntu 22.04 LTS(必选)

国内(腾讯云 / 阿里云)

  • 2 核 4G/50G SSD,¥50–80 / 月
  • 优点:国内延迟低;缺点:必须备案、OpenAI 需代理

最低配置

  • CPU:2 核 | 内存:4GB | 磁盘:40G SSD | 系统:Ubuntu 22.04

二、系统初始化(安全加固)

1. 登录 & 创建普通用户(禁止 root 运行)

bash

运行

ssh root@你的VPS_IP

# 创建用户
adduser openclaw
usermod -aG sudo openclaw
su - openclaw

2. 系统更新 & 基础工具

bash

运行

sudo apt update && sudo apt upgrade -y
sudo apt install -y curl wget git build-essential htop unzip

3. 防火墙(只开 22/80/443)

bash

运行

sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow ssh
sudo ufw allow 80
sudo ufw allow 443
sudo ufw enable
sudo ufw status

4. 禁用 root SSH 登录(安全)

bash

运行

sudo nano /etc/ssh/sshd_config
# 修改:
PermitRootLogin no
# 重启
sudo systemctl restart sshd

5. 安装 fail2ban(防暴力破解)

bash

运行

sudo apt install -y fail2ban
sudo systemctl enable fail2ban
sudo systemctl start fail2ban

三、安装 Node.js 24(官方推荐)

bash

运行

curl -fsSL https://deb.nodesource.com/setup_24.x | sudo -E bash -
sudo apt install -y nodejs

node -v   # v24.x
npm -v

国内加速(国内 VPS)

bash

运行

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

四、安装 OpenClaw(正式生产版)

bash

运行

# 官方脚本
curl -fsSL https://get.openclaw.ai | bash

# 或npm安装
npm install -g openclaw@latest

openclaw --version

初始化向导(配置模型 API Key)

bash

运行

openclaw onboard

依次配置:

  1. 模型提供商(OpenAI/Anthropic/DeepSeek)
  2. 填入 API Key
  3. 完成初始化

核心目录

plaintext

~/.openclaw/
├── openclaw.json        # 主配置
├── agents/default/      # 智能体人设
├── gateway/              # 网关数据
└── skills/               # 技能

五、PM2 进程守护(7×24 运行、崩溃自启)

bash

运行

npm install -g pm2

# 启动网关
openclaw gateway run

# PM2托管
pm2 start ~/.openclaw/gateway/index.js --name openclaw-gateway

# 开机自启
pm2 startup
pm2 save

# 查看状态
pm2 status
pm2 monit

日志轮转(防止日志爆盘)

bash

运行

pm2 install pm2-logrotate
pm2 set pm2-logrotate:max_size 10M
pm2 set pm2-logrotate:retain 7

六、Nginx 反向代理 + SSL(域名 + HTTPS)

1. 安装 Nginx

bash

运行

sudo apt install -y nginx
sudo systemctl enable nginx
sudo systemctl start nginx

2. 配置 Nginx(支持 WebSocket)

bash

运行

sudo nano /etc/nginx/sites-available/openclaw

写入(替换域名):

nginx

server {
    listen 80;
    server_name openclaw.yourdomain.com;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    server_name openclaw.yourdomain.com;

    ssl_certificate /etc/letsencrypt/live/openclaw.yourdomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/openclaw.yourdomain.com/privkey.pem;
    ssl_protocols TLSv1.2 TLSv1.3;

    location / {
        proxy_pass http://127.0.0.1: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;
        proxy_read_timeout 300s;
    }
}

3. 启用配置

bash

运行

sudo ln -s /etc/nginx/sites-available/openclaw /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx

4. 申请免费 SSL(Let’s Encrypt)

bash

运行

sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d openclaw.yourdomain.com

自动续期:certbot renew --dry-run


七、OpenClaw 安全配置(仪表板认证)

bash

运行

nano ~/.openclaw/openclaw.json

添加:

json

"gateway": {
  "host": "127.0.0.1",
  "port": 18789,
  "auth": {
    "type": "password",
    "username": "admin",
    "password": "强密码"
  },
  "controlUi": {
    "allowedOrigins": ["https://openclaw.yourdomain.com"]
  }
}

重启:

bash

运行

openclaw gateway restart

八、远程访问方案(推荐)

1. Tailscale(最安全,不用备案)

bash

运行

curl -fsSL https://tailscale.com/install.sh | sh
sudo tailscale up
tailscale ip -4

访问:http://100.x.x.x:18789

2. 公网域名(国外 VPS 直接用,国内需备案)

  • 访问:https://openclaw.yourdomain.com

九、健康检查 & 自动重启

bash

运行

nano ~/health-check.sh

bash

运行

#!/bin/bash
STATUS=$(curl -s -o /dev/null -w '%{http_code}' http://127.0.0.1:18789/)
if [ "$STATUS" != "200" ]; then
  echo "$(date): Gateway down, restarting" >> ~/health.log
  pm2 restart openclaw-gateway
fi

添加定时:

bash

运行

chmod +x ~/health-check.sh
crontab -e
# 添加:
*/5 * * * * /home/openclaw/health-check.sh

十、备份策略(每日自动备份记忆 / 配置)

bash

运行

nano ~/backup.sh

bash

运行

#!/bin/bash
DATE=$(date +%Y-%m-%d)
BACKUP_DIR=~/backups
mkdir -p $BACKUP_DIR
tar -zcf $BACKUP_DIR/openclaw-$DATE.tar.gz ~/.openclaw/

定时:

bash

运行

chmod +x ~/backup.sh
crontab -e
# 添加:
0 1 * * * /home/openclaw/backup.sh

十一、国内 VPS 备案说明

  • 国内 VPS + 域名 80/443 = 必须备案
  • 备案期:7–20 天
  • 备案前可用:IP 直接访问 / 非 80 端口 / Tailscale

十二、最终检查清单

✅ 普通用户运行

✅ 防火墙只开 22/80/443

✅ fail2ban 防暴力破解

✅ Node.js 24

✅ OpenClaw 安装 + API 配置

✅ PM2 守护 + 开机自启

✅ Nginx 反向代理 + SSL

✅ 仪表板密码认证

✅ 健康检查 + 自动重启

✅ 每日备份


要不要我把以上所有命令整理成一份一键部署脚本(install.sh),你直接复制运行即可全自动部署?