Hetzner 部署

在 Hetzner Cloud 上部署 OpenClaw

概述

Hetzner 是一家德国云服务提供商,以高性价比著称。 其 VPS 价格远低于 AWS、GCP 等大厂,非常适合个人和小团队部署 OpenClaw。

高性价比

€3.79/月起,性价比极高

欧洲机房

德国、芬兰机房可选

高性能

NVMe SSD,高速网络

推荐配置

用途型号配置价格
个人使用CX111 vCPU, 2GB RAM€3.79/月
推荐CX212 vCPU, 4GB RAM€5.83/月
多用户CX312 vCPU, 8GB RAM€10.59/月

创建服务器

步骤 1: 注册账户

  1. 访问 Hetzner Cloud
  2. 注册账户并完成验证
  3. 添加支付方式

步骤 2: 创建项目

  1. 登录 Hetzner Cloud Console
  2. 创建新项目(如 "OpenClaw")

步骤 3: 创建服务器

  1. 点击 "Add Server"
  2. 选择位置(推荐 Falkenstein 或 Helsinki)
  3. 选择镜像:Ubuntu 22.04
  4. 选择类型:CX21(推荐)
  5. 添加 SSH Key(强烈推荐)
  6. 点击 "Create & Buy now"

使用 CLI 创建

# 安装 hcloud CLI
brew install hcloud

# 配置 API Token
hcloud context create openclaw

# 创建服务器
hcloud server create \
  --name openclaw \
  --type cx21 \
  --image ubuntu-22.04 \
  --location fsn1 \
  --ssh-key your-key-name

安装配置

连接服务器

ssh root@your-server-ip

系统更新

apt update && apt upgrade -y

安装 Node.js

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

# 验证
node --version

安装 OpenClaw

# 安装 OpenClaw
npm install -g openclaw

# 初始化配置
openclaw init

# 配置 API Key
openclaw config set anthropic.api_key "sk-..."

# 配置频道(如 Telegram)
openclaw telegram setup

测试运行

# 前台运行测试
openclaw gateway

# 测试成功后 Ctrl+C 停止

系统服务

使用 systemd 让 OpenClaw 作为系统服务运行:

创建服务文件

cat > /etc/systemd/system/openclaw.service << 'EOF'
[Unit]
Description=OpenClaw Gateway
After=network.target

[Service]
Type=simple
User=root
WorkingDirectory=/root
ExecStart=/usr/bin/openclaw gateway
Restart=always
RestartSec=10
Environment=NODE_ENV=production

[Install]
WantedBy=multi-user.target
EOF

启动服务

# 重载 systemd
systemctl daemon-reload

# 启动服务
systemctl start openclaw

# 开机自启
systemctl enable openclaw

# 查看状态
systemctl status openclaw

查看日志

# 实时日志
journalctl -u openclaw -f

# 最近日志
journalctl -u openclaw --since "1 hour ago"

安全设置

配置防火墙

# 安装 ufw
apt install -y ufw

# 允许 SSH
ufw allow ssh

# 如果需要外部访问 Gateway API
ufw allow 8080/tcp

# 启用防火墙
ufw enable

创建非 root 用户

# 创建用户
adduser openclaw

# 添加 sudo 权限
usermod -aG sudo openclaw

# 切换用户
su - openclaw

配置 SSH

# 编辑 SSH 配置
nano /etc/ssh/sshd_config

# 推荐设置:
# PermitRootLogin no
# PasswordAuthentication no

# 重启 SSH
systemctl restart sshd

监控管理

Hetzner Cloud Metrics

Hetzner Console 提供基本的 CPU、内存、网络监控。

安装监控工具

# 安装 htop
apt install -y htop

# 查看资源使用
htop

配置自动更新

# 安装 unattended-upgrades
apt install -y unattended-upgrades

# 启用自动安全更新
dpkg-reconfigure -plow unattended-upgrades

设置备份

在 Hetzner Console 中启用自动快照:

  1. 进入服务器详情
  2. 点击 "Snapshots"
  3. 启用自动快照