1.准备腾讯云 CVM 实例,选择适合的服务器,并配置安全组(防火墙)

openclaw openclaw官方 2
  1. 搭建基础运行环境:在 CVM 上安装必要的系统依赖和运行环境。
  2. 部署 OpenClaw 应用:获取代码,安装应用依赖,进行配置。
  3. 配置网络与域名:将域名解析到 CVM 的公网 IP,并通过 Nginx/Apache 反向代理(推荐)。
  4. 设置进程守护与服务化:确保应用稳定运行,开机自启。

详细部署步骤

第一步:准备腾讯云 CVM

  1. 购买/启动 CVM 实例

    1.准备腾讯云 CVM 实例,选择适合的服务器,并配置安全组(防火墙)-第1张图片-OpenClaw开源下载|官方OpenClaw下载

    • 地域与可用区:选择离你或你的用户最近的地域。
    • 镜像:选择 Ubuntu Server 22.04 LTSCentOS 8 Stream(本文以 Ubuntu 22.04 为例,更通用),OpenClaw 对 Python 版本有要求,新系统更易满足。
    • 实例规格:根据预期流量选择,个人或小型项目,2核4GB2核2GB 通常足够,计算密集型任务需更高配置。
    • 公网 IP:务必勾选 “免费分配公网 IPv4 地址”
    • 安全组这是关键一步! 创建一个新的安全组或使用默认组,但必须放行以下端口:
      • 22:SSH 管理端口(来源建议设为 0.0.0/0 或你的固定 IP)。
      • 80:HTTP 端口(来源 0.0.0/0)。
      • 443:HTTPS 端口(来源 0.0/0),如果后续要启用 SSL。
      • OpenClaw 服务端口808080007860(具体看 OpenClaw 的默认配置,来源可以先设为 0.0/0,部署完成后可收紧为仅本机 0.0.1)。
    • 系统盘:至少 50GB。
  2. 登录服务器

    • 在腾讯云控制台获取 CVM 的公网 IP。
    • 使用 SSH 客户端登录:
      ssh ubuntu@<你的公网IP>
      # 或对于 CentOS
      ssh root@<你的公网IP>
    • 登录后,首先更新系统:
      sudo apt update && sudo apt upgrade -y
      # 或对于 CentOS
      sudo dnf update -y

第二步:搭建基础运行环境

  1. 安装必备工具

    sudo apt install -y git curl wget vim python3-pip python3-venv nginx
    # 或对于 CentOS
    sudo dnf install -y git curl wget vim python3-pip python3-virtualenv nginx
  2. 安装 Python(如果版本不符)

    • OpenClaw 通常需要 Python 3.8+,Ubuntu 22.04 自带 Python 3.10,通常满足要求。
    • 验证:python3 --version

第三步:部署 OpenClaw 应用

  1. 获取源代码

    • 假设 OpenClaw 的代码仓库在 GitHub 上。
      cd /opt
      sudo git clone https://github.com/<OpenClaw组织或用户名>/openclaw.git
      # 如果没有 sudo,可以先 clone 到 home 目录,再移动或更改权限
      sudo chown -R $USER:$USER /opt/openclaw
      cd openclaw
  2. 创建 Python 虚拟环境(强烈推荐):

    python3 -m venv venv
    source venv/bin/activate
    # 你的命令行提示符前会出现 (venv)
  3. 安装 Python 依赖

    pip install --upgrade pip
    # 根据项目要求安装依赖,通常有 requirements.txt 文件
    pip install -r requirements.txt
    # 如果没有 requirements.txt,可能需要查看项目文档或 setup.py
  4. 应用配置

    • 根据 OpenClaw 的文档,复制或创建配置文件(config.py, .env)。
    • 常见需要配置的项:数据库连接(如改为使用云数据库 TencentDB)、API Keys、服务监听地址和端口(建议设置为 0.0.0:<端口号> 以接收外部请求)等。
    • 示例:编辑配置文件,设置 HOST = ‘0.0.0.0’PORT = 8080

第四步:配置网络与域名(Nginx 反向代理)

  1. 测试应用直接运行(临时):

    cd /opt/openclaw
    source venv/bin/activate
    # 根据项目启动命令启动,
    # python app.py
    # 或
    # uvicorn main:app --host 0.0.0.0 --port 8080
    # 或
    # python cli.py server
    • 在浏览器访问 http://<你的公网IP>:8080,确认服务能通过 IP 访问。
    • Ctrl+C 停止服务。
  2. 配置 Nginx 反向代理

    • 创建 Nginx 站点配置文件:

      sudo vim /etc/nginx/sites-available/openclaw
    • 写入以下配置(将 your_domain.com 替换为你的域名,8080 替换为 OpenClaw 的实际端口):

      server {
          listen 80;
          server_name your_domain.com www.your_domain.com; # 如果没有域名,这里可以填 _ 或空,用 IP 访问
          location / {
              proxy_pass http://127.0.0.1:8080; # 指向 OpenClaw 服务
              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;
              # OpenClaw 是 WebSocket 应用,可能需要以下 headers
              # proxy_http_version 1.1;
              # proxy_set_header Upgrade $http_upgrade;
              # proxy_set_header Connection "upgrade";
          }
          # 可选:静态文件处理
          # location /static {
          #     alias /opt/openclaw/static;
          # }
      }
    • 启用站点并测试配置:

      sudo ln -s /etc/nginx/sites-available/openclaw /etc/nginx/sites-enabled/
      sudo nginx -t # 测试配置语法
      sudo systemctl reload nginx # 重载配置
  3. 配置域名解析

    • 在你的域名注册商处,添加一条 A 记录,将域名(如 your_domain.com)指向你的 CVM 公网 IP
    • 等待 DNS 生效(几分钟到几小时)。

第五步:设置进程守护与服务化

使用 Systemd 来管理 OpenClaw 服务,确保其稳定运行和开机自启。

  1. 创建 Systemd 服务文件

    sudo vim /etc/systemd/system/openclaw.service
  2. 写入配置(根据你的路径和启动命令调整):

    [Unit]
    Description=OpenClaw Service
    After=network.target nginx.target
    [Service]
    Type=simple
    User=ubuntu # 改为你的用户名
    Group=ubuntu # 改为你的用户组
    WorkingDirectory=/opt/openclaw
    Environment="PATH=/opt/openclaw/venv/bin"
    ExecStart=/opt/openclaw/venv/bin/python app.py
    # 或 ExecStart=/opt/openclaw/venv/bin/uvicorn main:app --host 0.0.0.0 --port 8080
    # 或 ExecStart=/opt/openclaw/venv/bin/gunicorn -w 4 -b 0.0.0.0:8080 main:app
    Restart=always
    RestartSec=3
    [Install]
    WantedBy=multi-user.target
  3. 启用并启动服务

    sudo systemctl daemon-reload
    sudo systemctl enable openclaw # 开机自启
    sudo systemctl start openclaw
    sudo systemctl status openclaw # 检查状态

第六步:后续优化与安全

  1. 配置 SSL(HTTPS)

    • 使用 Let‘s Encryptcertbot 免费证书。
      sudo apt install certbot python3-certbot-nginx
      sudo certbot --nginx -d your_domain.com -d www.your_domain.com
    • 证书会自动续期。
  2. 收紧安全组规则

    • 将 OpenClaw 应用端口(如 8080)的源 IP 限制改为 0.0.1 或特定 IP,因为现在已通过 Nginx 的 80/443 端口访问。
  3. 数据持久化

    • OpenClaw 使用数据库,建议使用腾讯云 TencentDB(云数据库)而非本地数据库,以提高可靠性和便于管理。
    • 重要文件(如上传目录)可以考虑挂载 云硬盘(CBS) 并做定期快照备份。
  4. 监控与日志

    • 查看服务日志:sudo journalctl -u openclaw -f
    • 查看 Nginx 日志:sudo tail -f /var/log/nginx/access.logerror.log
    • 在腾讯云控制台可以查看 CVM 的基础监控(CPU、内存、网络)。

腾讯云部署 OpenClaw 的关键在于:

  1. 安全组正确放行端口
  2. 使用 Nginx 反向代理 处理外部请求。
  3. 使用 Systemd 将应用作为服务运行。
  4. 考虑使用 云数据库和对象存储 等云产品提升生产环境可靠性。

请务必根据 OpenClaw 项目的具体文档(尤其是 README.mdDEPLOYMENT.md)调整上述通用步骤中的命令和配置。

标签: 安全组配置

抱歉,评论功能暂时关闭!