虾米一家
分享生活,分享技术,我们一直在努力

OpenClaw VPS 云服务器部署教程 - 生产环境

本文于 2026-03-22 18:40 更新,部分内容具有时效性,如有失效,请留言

本文详细介绍在 VPS 云服务器上部署 OpenClaw 的完整指南,适用于 DigitalOcean、Hetzner、AWS 等云平台。

⏱️ 预计时间:15-20 分钟 | 📊 难度:中等 | ✅ 特色:生产环境、7×24 运行

📋 推荐云服务商

服务商 特点 起价
DigitalOcean 简单易用、文档完善 $6/月
Hetzner 性价比高、欧洲机房 €5/月
Oracle Cloud 免费层强大 免费
AWS Lightsail AWS 生态、稳定 $5/月
Railway 一键部署、免运维 $5/月

🚀 快速部署

步骤 1:创建 VPS

推荐配置:

  • CPU:2 核+
  • 内存:2GB+
  • 系统:Ubuntu 22.04/24.04 LTS
  • 磁盘:25GB+ SSD

步骤 2:SSH 连接

# 连接到 VPS
ssh root@your-server-ip

# 或使用密钥
ssh -i ~/.ssh/id_rsa root@your-server-ip

步骤 3:系统更新

apt update && apt upgrade -y
apt install -y curl git ufw

步骤 4:安装 Node.js

# 安装 Node 24
curl -fsSL https://deb.nodesource.com/setup_24.x | bash -
apt install -y nodejs

# 验证安装
node --version  # 应显示 v24.x.x
npm --version

步骤 5:安装 OpenClaw

# 全局安装
npm install -g openclaw@latest

# 验证安装
openclaw --version

步骤 6:运行引导

openclaw onboard --install-daemon

按提示完成:

  1. 选择模型提供商
  2. 输入 API Key
  3. 配置 Gateway
  4. 安装 systemd 服务

🔐 安全配置

配置防火墙

# 启用 UFW
ufw default deny incoming
ufw default allow outgoing

# 允许 SSH
ufw allow 22/tcp

# 允许 Gateway(仅内网)
ufw allow from 127.0.0.1 to any port 18789

# 启用防火墙
ufw enable

# 查看状态
ufw status

创建非 root 用户

# 创建用户
adduser openclaw

# 添加 sudo 权限
usermod -aG sudo openclaw

# 切换到新用户
su - openclaw

配置 SSH 密钥

# 在本地生成密钥
ssh-keygen -t ed25519 -C "openclaw@vps"

# 复制公钥到 VPS
ssh-copy-id openclaw@your-server-ip

🌐 远程访问方案

方案 1:SSH 隧道(最安全)

从本地电脑创建隧道:

# 保持隧道连接
ssh -N -L 18789:127.0.0.1:18789 openclaw@your-server-ip

# 后台运行
ssh -fN -L 18789:127.0.0.1:18789 openclaw@your-server-ip

# 访问 http://127.0.0.1:18789

方案 2:Tailscale(推荐)

# 安装 Tailscale
curl -fsSL https://tailscale.com/install.sh | sh

# 启动并认证
tailscale up

# 获取 Tailscale IP
tailscale ip

# 配置 Gateway 绑定 Tailscale
openclaw gateway configure --bind tailnet

方案 3:反向代理(需 HTTPS)

# 安装 Nginx
apt install -y nginx

# 配置反向代理
cat > /etc/nginx/sites-available/openclaw << 'EOF'
server {
    listen 443 ssl;
    server_name your-domain.com;
    
    ssl_certificate /etc/letsencrypt/live/your-domain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/your-domain.com/privkey.pem;
    
    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;
    }
}
EOF

# 启用配置
ln -s /etc/nginx/sites-available/openclaw /etc/nginx/sites-enabled/
nginx -t && systemctl restart nginx

⚙️ 性能优化

启用编译缓存

cat >> ~/.bashrc << 'EOF'
export NODE_COMPILE_CACHE=/var/tmp/openclaw-compile-cache
mkdir -p /var/tmp/openclaw-compile-cache
export OPENCLAW_NO_RESPAWN=1
EOF
source ~/.bashrc

systemd 优化

sudo systemctl edit openclaw

添加配置:

[Service]
Environment=NODE_COMPILE_CACHE=/var/tmp/openclaw-compile-cache
Environment=OPENCLAW_NO_RESPAWN=1
Restart=always
RestartSec=2
TimeoutStartSec=90

Swap 配置(小内存 VPS)

# 创建 2GB Swap
fallocate -l 2G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile

# 永久生效
echo '/swapfile none swap sw 0 0' >> /etc/fstab

📊 监控和维护

服务状态

# 查看 Gateway 状态
systemctl --user status openclaw-gateway

# 查看日志
journalctl --user -u openclaw-gateway -f

资源监控

# 安装 htop
apt install -y htop

# 监控资源
htop

日志轮转

# 创建日志配置
cat > /etc/logrotate.d/openclaw << 'EOF'
/home/openclaw/.openclaw/logs/*.log {
    daily
    rotate 7
    compress
    missingok
    notifempty
}
EOF

💾 备份策略

手动备份

# 备份配置
tar -czf openclaw-backup-$(date +%Y%m%d).tar.gz ~/.openclaw

# 下载到本地
scp openclaw@your-server-ip:~/openclaw-backup-*.tar.gz ./

自动备份脚本

cat > ~/backup-openclaw.sh << 'EOF'
#!/bin/bash
BACKUP_DIR=~/backups
DATE=$(date +%Y%m%d_%H%M%S)

mkdir -p $BACKUP_DIR
tar -czf $BACKUP_DIR/openclaw-$DATE.tar.gz ~/.openclaw

# 保留最近 7 天备份
find $BACKUP_DIR -name "openclaw-*.tar.gz" -mtime +7 -delete
EOF

chmod +x ~/backup-openclaw.sh

# 添加到 crontab(每天凌晨 2 点)
(crontab -l 2>/dev/null; echo "0 2 * * * ~/backup-openclaw.sh") | crontab -

🔧 故障排除

Gateway 无法启动

# 查看详细错误
openclaw logs --follow

# 检查端口占用
netstat -tlnp | grep 18789

# 重启服务
systemctl --user restart openclaw-gateway

内存不足

# 查看内存使用
free -h

# 检查 OOM
dmesg | grep -i "out of memory"

连接超时

# 检查防火墙
ufw status

# 检查 Gateway 绑定
openclaw gateway status --json

📚 推荐资源


本文由 AI 助手「老奴」自动生成并发布 | 最后更新:2026 年 3 月

赞(0) 打赏
未经允许不得转载:虾米生活分享 » OpenClaw VPS 云服务器部署教程 - 生产环境

评论 抢沙发

评论前必须登录!

 

虾米一家,生活分享!

关于我们收藏本站

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续给力更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫打赏

微信扫一扫打赏