Cài đặt OpenClaw trên Linux VPS — Hướng dẫn toàn tập

Dành cho: Ubuntu 22.04 / Debian 12 VPS
Kết quả: Bot Zalo AI chạy 24/7, truy cập từ xa qua domain riêng, tự khởi động khi server restart

Tổng quan kiến trúc

Người dùng Zalo
      ↓ nhắn tin
Zalo Bot API
      ↓ polling (bot tự lấy tin)
OpenClaw Gateway (VPS)
      ↓
Gemini / Claude / GPT (AI model)
      ↓ phản hồi
Zalo Bot API → người dùng

Dashboard (bạn quản lý)
      ↓ truy cập qua browser
Cloudflare Tunnel → yourdomain.com → OpenClaw

Yêu cầu:

  • Ubuntu 22.04 hoặc Debian 12
  • RAM tối thiểu 1GB (khuyến nghị 2GB+)
  • Tài khoản Cloudflare (miễn phí)
  • Tên miền đã trỏ về Cloudflare
  • Tài khoản Zalo Bot Platform

Phần 1 — Cài đặt môi trường

1.1 Cập nhật hệ thống

sudo apt update && sudo apt upgrade -y

1.2 Cài Node.js 20+

curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install -y nodejs
node --version  # phải là v20+

1.3 Cài pnpm

curl -fsSL https://get.pnpm.io/install.sh | sh -
source ~/.bashrc
pnpm --version

1.4 Cài Docker (cho Cloudflare Tunnel)

curl -fsSL https://get.docker.com | sh
sudo usermod -aG docker $USER
newgrp docker

Phần 2 — Cài đặt OpenClaw

2.1 Cài OpenClaw

pnpm add -g openclaw@latest
openclaw --version

2.2 Khởi tạo cấu hình

openclaw onboard

Wizard sẽ hỏi:

  • AI model: Chọn Google Gemini → nhập API key (lấy từ aistudio.google.com)
  • Workspace: Giữ mặc định ~/.openclaw/workspace
  • Gateway mode: Local
Lấy Google API key miễn phí: Vào aistudio.google.com → Get API Key → Copy

Phần 3 — Thiết lập Cloudflare Tunnel

3.1 Tạo tunnel trên Cloudflare Dashboard

  1. Vào one.dash.cloudflare.com
  2. Chọn Networks → Tunnels → Create a tunnel
  3. Đặt tên tunnel (VD: my-openclaw)
  4. Chọn Docker để lấy token

3.2 Chạy Cloudflare Tunnel bằng Docker

Tạo file ~/cloudflared/docker-compose.yml:

mkdir -p ~/cloudflared
cat > ~/cloudflared/docker-compose.yml << 'EOF'
version: "3"
services:
  cloudflared:
    image: cloudflare/cloudflared:latest
    restart: unless-stopped
    command: tunnel --no-autoupdate run --token <TUNNEL_TOKEN>
    network_mode: host
EOF
Thay <TUNNEL_TOKEN> bằng token lấy từ bước 3.1
cd ~/cloudflared
docker compose up -d
docker compose logs  # kiểm tra kết nối

3.3 Cấu hình route trên Cloudflare Dashboard

  1. Vào tunnel vừa tạo → Public Hostname
  2. Thêm route:
  • Subdomain: claw
  • Domain: yourdomain.com
  • Service: http://localhost:18789

Phần 4 — Cấu hình OpenClaw

4.1 Cấu hình gateway

# Cho phép kết nối từ tất cả interfaces (cần cho Cloudflare Tunnel trên Linux)
openclaw config set gateway.bind lan

# Trust Cloudflare proxy
openclaw config set gateway.trustedProxies '["127.0.0.1"]'

# Cho phép truy cập từ domain của bạn
openclaw config set gateway.controlUi.allowedOrigins '["https://claw.yourdomain.com"]'

# Đặt password truy cập dashboard
openclaw config set gateway.auth.password "mat-khau-cua-ban"

4.2 Lấy tokenized URL để truy cập lần đầu

openclaw dashboard --no-open

Copy URL dạng http://127.0.0.1:18789/#token=... → đổi thành https://claw.yourdomain.com/#token=...


Phần 5 — Thiết lập Zalo Bot

5.1 Tạo Bot trên Zalo Platform

  1. Vào bot.zaloplatforms.com
  2. Đăng nhập bằng tài khoản Zalo
  3. Tạo bot mới → đặt tên
  4. Copy Bot Token (dạng 123456789:abc-xyz-token)

5.2 Cấu hình Zalo channel trong OpenClaw

openclaw configure

Chọn:

  • Channels → Zalo (Bot API)
  • Nhập Bot Token
  • Use webhook mode: No (dùng polling — ổn định hơn)
  • DM Policy: Open (cho phép mọi người nhắn tin)

5.3 Test bot

Mở Zalo → tìm bot theo tên → nhắn "xin chào"

Bot sẽ phản hồi trong vòng 1-3 giây.


Phần 6 — Auto-start bằng systemd

Trên Linux không có openclaw gateway install như macOS, cần tạo systemd service thủ công.

6.1 Tìm đường dẫn openclaw

which openclaw
# VD: /root/.local/share/pnpm/openclaw

6.2 Tạo systemd service

sudo tee /etc/systemd/system/openclaw.service << EOF
[Unit]
Description=OpenClaw Gateway
After=network.target
Wants=network-online.target

[Service]
Type=simple
User=$USER
WorkingDirectory=$HOME
ExecStart=$(which openclaw) gateway
Restart=always
RestartSec=10
Environment=HOME=$HOME
Environment=PATH=/usr/local/bin:/usr/bin:/bin:$HOME/.local/share/pnpm

[Install]
WantedBy=multi-user.target
EOF

6.3 Enable và start service

sudo systemctl daemon-reload
sudo systemctl enable openclaw
sudo systemctl start openclaw
sudo systemctl status openclaw

6.4 Xem log

sudo journalctl -u openclaw -f

Các lệnh quản lý:

Lệnh Tác dụng
sudo systemctl start openclaw Start gateway
sudo systemctl stop openclaw Stop gateway
sudo systemctl restart openclaw Restart gateway
sudo systemctl status openclaw Xem trạng thái
sudo journalctl -u openclaw -f Xem log realtime

Phần 7 — Truy cập Dashboard từ xa

7.1 Truy cập lần đầu

Dùng tokenized URL:

https://claw.yourdomain.com/#token=<gateway-token>

Lấy token:

openclaw dashboard --no-open

7.2 Truy cập thường xuyên

Sau khi vào bằng tokenized URL lần đầu, từ lần sau chỉ cần vào https://claw.yourdomain.com và nhập password.

7.3 Chia sẻ với người khác

Share tokenized URL cho người tin tưởng — họ vào thẳng không cần password.

Để revoke quyền truy cập:

openclaw config set gateway.auth.token "$(openssl rand -hex 32)"
sudo systemctl restart openclaw

Phần 8 — Bảo mật cơ bản

8.1 Firewall — chỉ cho phép Cloudflare

sudo ufw enable
sudo ufw allow ssh
sudo ufw allow 18789/tcp  # chỉ cần nếu không dùng Cloudflare Tunnel

Nếu dùng Cloudflare Tunnel, không cần mở port 18789 ra ngoài — tunnel tự kết nối từ trong ra.

8.2 Đổi password định kỳ

openclaw config set gateway.auth.password "password-moi"
sudo systemctl restart openclaw

8.3 Không commit token vào git

File config nằm tại ~/.openclaw/openclaw.json — không bao giờ commit file này.


Phần 9 — Troubleshoot thường gặp

Service không start

sudo journalctl -u openclaw -n 50
openclaw doctor --fix

Zalo không nhận/gửi tin

openclaw status
openclaw channels list

Cloudflare 502 Bad Gateway

# Kiểm tra gateway đang chạy
sudo systemctl status openclaw

# Kiểm tra bind
openclaw config get gateway.bind
# Phải là: lan

# Kiểm tra Docker tunnel
docker ps | grep cloudflared
docker logs cloudflared

Dashboard yêu cầu pairing liên tục

openclaw config set gateway.trustedProxies '["127.0.0.1"]'
openclaw config set gateway.controlUi.allowedOrigins '["https://claw.yourdomain.com"]'
sudo systemctl restart openclaw

Sau đó dùng tokenized URL để vào lần đầu.


Tóm tắt các lệnh quan trọng

# Cài đặt
pnpm add -g openclaw@latest
openclaw onboard

# Cấu hình
openclaw config set gateway.bind lan
openclaw config set gateway.trustedProxies '["127.0.0.1"]'
openclaw config set gateway.controlUi.allowedOrigins '["https://claw.yourdomain.com"]'
openclaw config set gateway.auth.password "password"

# Zalo
openclaw configure  # chọn Channels → Zalo

# Auto-start (systemd)
sudo systemctl enable openclaw
sudo systemctl start openclaw

# Kiểm tra
openclaw status
sudo journalctl -u openclaw -f

Bài viết trước: [Cài đặt OpenClaw trên macOS]