🏭 Production Deployment
Hướng dẫn triển khai OpenClaw chuẩn Enterprise với tiêu chí: High Availability (HA), Security Hardening, và Monitoring toàn diện.
🏗️ Tổng quan kiến trúc
Sơ đồ kiến trúc khuyến nghị cho hệ thống Production:
graph TD
User[User / Internet] -->|HTTPS/443| Nginx[Nginx Reverse Proxy]
Nginx -->|Rate Limited| Gateway[OpenClaw Gateway :18789]
Gateway -->|Cache| Redis[Redis]
Gateway -->|Persist| PG[PostgreSQL]
Gateway -->|Auto-Restart| PM2[PM2 Process Manager]
🚀 Lựa chọn phương án
💻 VPS + Docker (Khuyên dùng)
- Độ phức tạp: Trung bình
- Chi phí: Thấp ($5-10/tháng)
- Kiểm soát: 100%
Cân bằng tốt nhất giữa hiệu năng và dễ quản lý. Dùng Docker Compose để điều phối dịch vụ.
☸️ Kubernetes (K8s)
- Độ phức tạp: Cao
- Quy mô: Không giới hạn
Dành cho enterprise deployment với yêu cầu auto-scaling phức tạp.
Thiết lập VPS (Ubuntu 22.04)
1. Chọn nhà cung cấp VPS
- 🇻🇳 Local (VN)
- 🌍 Quốc tế
- VNG Cloud: Ổn định, support tốt.
- VNPT Cloud: Hạ tầng mạnh.
- Viettel IDC: Chuẩn Tier 3.
- Hetzner: ~€6/tháng (4GB RAM) - Best Value
- DigitalOcean: $6/tháng (1GB RAM)
- Vultr: $6/tháng (1GB RAM)
- AWS Lightsail: $5/tháng (1GB RAM)
2. Thiết lập máy chủ ban đầu
# SSH vào server
ssh root@your-server-ip
# Cập nhật hệ thống
apt-get update && apt-get upgrade -y
# Tạo user openclaw (bảo mật hơn root)
useradd -m -s /bin/bash openclaw
usermod -aG sudo openclaw
# Đặt mật khẩu
passwd openclaw
# Chuyển sang user openclaw
su - openclaw
3. Cài đặt các gói phụ thuộc
# Node.js 22 LTS
curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash -
sudo apt-get install -y nodejs
# Docker (Tùy chọn nhưng khuyên dùng)
curl -fsSL https://get.docker.com | sudo sh
sudo usermod -aG docker $USER
# PM2 (Process Manager)
sudo npm install -g pm2
# Nginx (Web Server)
sudo apt-get install -y nginx
# Kiểm tra version
node --version # v22.x.x
npm --version # 10.x.x
pm2 --version
nginx -v
4. Cài đặt OpenClaw
- NPM Global
- Từ Source Code
```bash npm install -g openclaw@latest ```
```bash git clone https://github.com/openclaw/openclaw.git cd openclaw npm install npm run build ```
5. Thiết lập ban đầu (Onboarding)
openclaw onboard --install-daemon
# Làm theo hướng dẫn:
# - Gateway mode: local
# - API keys
# - Key channels setup
Quản lý tiến trình với PM2
Tại sao dùng PM2?
- ✅ Tự khởi động lại khi crash
- ✅ Giám sát tài nguyên (CPU/RAM)
- ✅ Quản lý Log tập trung
- ✅ Chế độ Cluster (tận dụng đa nhân)
Cấu hình PM2
Tạo file ecosystem.config.js:
module.exports = {
apps: [{
name: 'openclaw-gateway',
script: 'openclaw',
args: 'gateway --port 18789',
instances: 1,
exec_mode: 'fork',
// Restart behavior
autorestart: true,
watch: false,
max_restarts: 10,
min_uptime: '10s',
restart_delay: 5000,
// Logs
error_file: '/var/log/openclaw/error.log',
out_file: '/var/log/openclaw/out.log',
log_date_format: 'YYYY-MM-DD HH:mm:ss Z',
merge_logs: true,
// Environment
env: {
NODE_ENV: 'production',
TZ: 'Asia/Ho_Chi_Minh'
},
// Resources
max_memory_restart: '2G',
// Health check
listen_timeout: 10000,
kill_timeout: 5000
}]
};
Khởi chạy với PM2
# Tạo thư mục log
sudo mkdir -p /var/log/openclaw
sudo chown openclaw:openclaw /var/log/openclaw
# Start
pm2 start ecosystem.config.js
# Kiểm tra trạng thái
pm2 status
pm2 logs openclaw-gateway
Tự khởi động cùng hệ thống
# Tạo startup script
pm2 startup
# Lưu danh sách process hiện tại
pm2 save
Nginx Reverse Proxy
Lợi ích của Nginx
- Bảo mật SSL/TLS termination
- Rate limiting (chống spam/DDoS đơn giản)
- Load balancing (nếu chạy nhiều instance)
- Phục vụ file tĩnh cực nhanh
Cấu hình
File: /etc/nginx/sites-available/openclaw
# Upstream (backend)
upstream openclaw_gateway {
server 127.0.0.1:18789 fail_timeout=30s max_fails=3;
keepalive 64;
}
# Rate limiting zones
limit_req_zone $binary_remote_addr zone=general:10m rate=10r/s;
limit_req_zone $binary_remote_addr zone=api:10m rate=100r/s;
# HTTP → HTTPS redirect
server {
listen 80;
listen [::]:80;
server_name openclaw.yourdomain.com;
return 301 https://$server_name$request_uri;
}
# HTTPS server
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name openclaw.yourdomain.com;
# SSL certificates (Let's Encrypt will managed these paths)
ssl_certificate /etc/letsencrypt/live/openclaw.yourdomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/openclaw.yourdomain.com/privkey.pem;
# Security headers
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
# WebSocket proxy (Quan trọng cho Real-time)
location / {
proxy_pass http://openclaw_gateway;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
}
}
Kích hoạt & Cài đặt SSL
# Link file cấu hình
sudo ln -s /etc/nginx/sites-available/openclaw /etc/nginx/sites-enabled/
# Kiểm tra config
sudo nginx -t
sudo systemctl reload nginx
# Cài đặt SSL (Certbot)
sudo apt-get install -y certbot python3-certbot-nginx
sudo certbot --nginx -d openclaw.yourdomain.com
Thiết lập Database (PostgreSQL)
Dành cho nhu cầu lưu trữ persistent lâu dài và ổn định hơn SQLite mặc định.
# Cài đặt PostgreSQL
sudo apt-get install -y postgresql postgresql-contrib
# Tạo DB & User
sudo -u postgres psql
CREATE DATABASE openclaw;
CREATE USER openclaw WITH PASSWORD 'your_secure_password';
GRANT ALL PRIVILEGES ON DATABASE openclaw TO openclaw;
\q
Cấu hình OpenClaw (config.json):
{
"database": {
"type": "postgresql",
"url": "postgresql://openclaw:password@localhost:5432/openclaw"
}
}
Sao lưu (Backup)
Script sao lưu tự động
File: /home/openclaw/backup.sh
#!/bin/bash
BACKUP_DIR="/backups/openclaw"
DATE=$(date +%Y%m%d_%H%M%S)
# Tạo thư mục
mkdir -p $BACKUP_DIR
# Backup
tar -czf $BACKUP_DIR/config-$DATE.tar.gz ~/.openclaw/
pg_dump -U openclaw openclaw | gzip > $BACKUP_DIR/db-$DATE.sql.gz
# Xóa backup cũ > 30 ngày
find $BACKUP_DIR -type f -mtime +30 -delete
Thêm vào Crontab (crontab -e) để chạy 2h sáng hàng ngày:
0 2 * * * /home/openclaw/backup.sh >> /var/log/openclaw/backup.log 2>&1
Tăng cường bảo mật
Tường lửa (UFW)
sudo ufw enable
sudo ufw allow 22/tcp # SSH
sudo ufw allow 80/tcp # HTTP
sudo ufw allow 443/tcp # HTTPS
# KHÔNG mở port 18789 ra public
Fail2Ban
Cài đặt để chống brute-force attack:
sudo apt-get install -y fail2ban
Checklist chuẩn bị Production
- VPS đã sẵn sàng & SSH Key configured
- Domain trỏ về IP server
- SSL certificate (HTTPS) active
- Database (PostgreSQL) configured
- OpenClaw chạy dưới PM2 (Auto-restart)
- Nginx làm Reverse Proxy
- Firewall (UFW) bật
- Backup script hoạt động