Self-hosting n8n cho bạn toàn quyền kiểm soát: unlimited executions, data privacy, custom integrations. Bài viết hướng dẫn setup n8n server và tích hợp Alohub cho production.
- Stack: Docker, PostgreSQL, Nginx
- Security: SSL, authentication, API key management
- Alohub node: Cài đặt và cấu hình
- Monitoring: Healthchecks, logging
1. Tại sao self-host n8n?
| Tiêu chí | n8n Cloud | Self-hosted |
|---|---|---|
| Cost | $20/tháng (starter) | VPS $5-20/tháng |
| Executions | Limited (tùy plan) | Unlimited ✓ |
| Data location | EU servers | Your choice ✓ |
| Custom nodes | Limited | Full control ✓ |
| Maintenance | n8n handles | You handle |
Self-host phù hợp khi: Volume cao (>10,000 executions/tháng), data privacy requirements, cần custom nodes, muốn control infrastructure.
2. Server requirements
🔹 Minimum (Small Production)
|
✅ Recommended (Production)
|
3. Docker setup
docker-compose.yml:
version: '3.8'
services:
n8n:
image: n8nio/n8n:latest
restart: always
ports:
- "5678:5678"
environment:
- N8N_HOST=n8n.yourdomain.com
- N8N_PORT=5678
- N8N_PROTOCOL=https
- NODE_ENV=production
- WEBHOOK_URL=https://n8n.yourdomain.com/
- GENERIC_TIMEZONE=Asia/Ho_Chi_Minh
# Database
- DB_TYPE=postgresdb
- DB_POSTGRESDB_HOST=postgres
- DB_POSTGRESDB_PORT=5432
- DB_POSTGRESDB_DATABASE=n8n
- DB_POSTGRESDB_USER=n8n
- DB_POSTGRESDB_PASSWORD=${DB_PASSWORD}
# Security
- N8N_BASIC_AUTH_ACTIVE=true
- N8N_BASIC_AUTH_USER=${N8N_USER}
- N8N_BASIC_AUTH_PASSWORD=${N8N_PASSWORD}
- N8N_ENCRYPTION_KEY=${ENCRYPTION_KEY}
volumes:
- n8n_data:/home/node/.n8n
depends_on:
- postgres
postgres:
image: postgres:15
restart: always
environment:
- POSTGRES_USER=n8n
- POSTGRES_PASSWORD=${DB_PASSWORD}
- POSTGRES_DB=n8n
volumes:
- postgres_data:/var/lib/postgresql/data
volumes:
n8n_data:
postgres_data:
.env file:
DB_PASSWORD=your_secure_db_password N8N_USER=admin N8N_PASSWORD=your_secure_admin_password ENCRYPTION_KEY=your_32_char_hex_key
Generate encryption key:
openssl rand -hex 32
Deploy:
docker-compose up -d
4. Nginx reverse proxy + SSL
/etc/nginx/sites-available/n8n:
server {
listen 80;
server_name n8n.yourdomain.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl http2;
server_name n8n.yourdomain.com;
ssl_certificate /etc/letsencrypt/live/n8n.yourdomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/n8n.yourdomain.com/privkey.pem;
location / {
proxy_pass http://localhost:5678;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_read_timeout 300s;
}
}
Get SSL certificate:
sudo certbot --nginx -d n8n.yourdomain.com
5. Cài đặt Alohub node
Alohub – giải pháp AI Contact Center & nền tảng CPaaS tại Việt Nam – cung cấp community node cho n8n.
Option 1: Cài trong container
# SSH vào container docker exec -it n8n-n8n-1 /bin/sh # Install Alohub node npm install n8n-nodes-alohub # Exit và restart exit docker-compose restart n8n
Option 2: Thêm vào docker-compose.yml (recommended)
environment: - N8N_CUSTOM_EXTENSIONS=n8n-nodes-alohub
Cấu hình credentials:
- Trong n8n UI, vào Settings → Credentials
- Add new → Alohub API
- Nhập API Key từ Alohub dashboard
Chi tiết: Tích hợp Alohub node trên n8n.
6. Security best practices
🔒 Firewallsudo ufw allow 22/tcp sudo ufw allow 80/tcp sudo ufw allow 443/tcp sudo ufw enable |
🔑 API Keys
|
Backup script (crontab)
# Backup PostgreSQL daily (2:00 AM) 0 2 * * * docker exec n8n-postgres-1 pg_dump -U n8n n8n > /backups/n8n_$(date +\%Y\%m\%d).sql # Backup n8n data weekly (Sunday 3:00 AM) 0 3 * * 0 docker run --rm -v n8n_n8n_data:/data -v /backups:/backup alpine tar czf /backup/n8n_data_$(date +\%Y\%m\%d).tar.gz /data
7. Monitoring
Healthcheck endpoint:
curl https://n8n.yourdomain.com/healthz
# Returns: {"status":"ok"}
Uptime monitoring: Dùng UptimeRobot (free) hoặc Uptime Kuma (self-hosted) để monitor /healthz endpoint.
8. Troubleshooting
| Issue | Solution |
|---|---|
| Webhook không nhận được | Check WEBHOOK_URL env, firewall, Nginx config |
| Alohub node không hiện | Restart n8n sau khi install node |
| Workflow execution slow | Increase RAM, check database |
| SSL certificate expired | certbot renew --nginx |
| Out of disk | Clean old executions trong Settings |
FAQ
VPS ở đâu tốt nhất cho VN?
Singapore hoặc Hong Kong có latency tốt nhất đến VN (~20-40ms). DigitalOcean Singapore hoặc Vultr Singapore là lựa chọn phổ biến. Nếu cần data trong VN, có thể dùng VNG Cloud, CMC Cloud nhưng giá cao hơn.
Cần backup frequency như thế nào?
Database: Daily (critical). n8n data volume: Weekly. Retention: 30 ngày cho daily, 6 tháng cho weekly. Test restore định kỳ (quarterly) để đảm bảo backup hoạt động.
Có thể migrate từ n8n Cloud sang self-hosted không?
Có. Export workflows từ n8n Cloud (JSON), import vào self-hosted. Credentials cần tạo lại (security reasons). Webhook URLs sẽ thay đổi, cần update ở source systems.