在 VPS 上部署 OpenClaw 并远程访问

    在 VPS 上部署 OpenClaw 并远程访问

    如果你想让 OpenClaw 一直在线,随时随地都能访问,可以把它部署在 VPS 上。本文教你如何一步步部署。

    准备工作

    • 一台 VPS(推荐 2C4G 以上配置)
    • 操作系统:Ubuntu 22.04+/Debian 12+
    • 域名(可选,用于 HTTPS 访问)
    • Node.js 20+

    步骤 1: 安装 Node.js

    # 使用 nvm 安装 Node.js 20
    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
    source ~/.bashrc
    nvm install 20
    nvm use 20
    nvm alias default 20
    
    # 验证
    node --version
    npm --version

    步骤 2: 安装 OpenClaw

    npm install -g openclaw
    
    # 验证
    openclaw --version

    步骤 3: 初始化配置

    openclaw init

    按照提示配置:

    1. 输入你的 API Key
    2. 选择默认模型
    3. 配置端口(默认 18789)

    步骤 4: 使用 systemd 托管(推荐)

    创建 systemd 服务文件 /etc/systemd/system/openclaw.service:

    [Unit]
    Description=OpenClaw AI Assistant
    After=network.target
    
    [Service]
    Type=simple
    User=your-username
    ExecStart=/home/your-username/.nvm/versions/node/v20/bin/openclaw
    WorkingDirectory=/home/your-username/.openclaw
    Restart=always
    RestartSec=10
    Environment=PATH=/home/your-username/.nvm/versions/node/v20/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
    
    [Install]
    WantedBy=multi-user.target

    启用并启动服务:

    sudo systemctl daemon-reload
    sudo systemctl enable openclaw
    sudo systemctl start openclaw
    
    # 查看状态
    sudo systemctl status openclaw

    步骤 5: Nginx 反向代理(可选,推荐)

    如果你有域名,可以配置 Nginx 反向代理并启用 HTTPS:

    server {
        listen 80;
        server_name your-domain.com;
        return 301 https://$server_name$request_uri;
    }
    
    server {
        listen 443 ssl;
        server_name your-domain.com;
    
        ssl_certificate /path/to/fullchain.pem;
        ssl_certificate_key /path/to/privkey.pem;
    
        location / {
            proxy_pass http://localhost:18789;
            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;
        }
    }

    使用 Let’s Encrypt 申请证书:

    sudo certbot --nginx -d your-domain.com

    步骤 6: 访问测试

    • 无域名:直接访问 http://your-vps-ip:18789
    • 有域名:访问 https://your-domain.com

    常用维护命令

    # 查看日志
    journalctl -u openclaw -f
    
    # 重启服务
    sudo systemctl restart openclaw
    
    # 停止服务
    sudo systemctl stop openclaw
    
    # 更新 OpenClaw
    npm update -g openclaw && sudo systemctl restart openclaw

    安全建议

    1. 不要直接暴露 OpenClaw 到公网不做任何认证
    2. 使用 HTTPS 加密传输
    3. 如果不需要公网访问,可以用 SSH 隧道本地访问
    4. 定期更新 OpenClaw 和系统

    SSH 隧道本地访问(更安全)

    如果你不想公开端口,可以只监听 localhost,然后用 SSH 隧道:

    # 在本地机器上
    ssh -N -L 18789:localhost:18789 your-vps

    然后本地浏览器访问 http://localhost:18789 即可。

    总结

    这样你就拥有了一个 24 小时在线的 OpenClaw 实例,可以随时随地访问。使用 systemd 托管保证服务稳定运行,配合 Nginx 和 HTTPS 更加安全可靠。

    发表回复

    您的邮箱地址不会被公开。 必填项已用 * 标注