Hướng dẫn tự Self-host n8n từ A-Z, full code

11 Tháng 5, 2025
Blog

Bài viết này sẽ hướng dẫn tự cài đặt n8n với docker + ubuntu từ A-Z, có câu hỏi nào có thể để lại trong bình luận, theo dõi video để làm theo từng bước để đạt hiệu quả tốt nhất.

Link mua VPS demo free 7 ngày: https://tino.vn/vps-gia-re

  • Mã giảm giá 1-2 năm giảm 20%:
    tinon8n20
  • Mã giảm giá 3-5 năm giảm 40%:
    tinon8n40

Code để cài đặt self-host n8n, cài đặt redis

1. Tạo thư mục và file cấu hình

mkdir n8n-selfhost && cd n8n-selfhost
touch docker-compose.yml
mkdir -p ~/.n8n

2. cài docker

# 1. Cài gói phụ trợ
sudo apt update
sudo apt install ca-certificates curl gnupg lsb-release -y
# 2. Thêm Docker GPG key
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
# 3. Thêm Docker repo
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] \
https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
# 4. Cài Docker Engine + Plugin Compose
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io docker-compose-plugin -y

3. Nội dung docker-compose.yml

*** lệnh cài nano nếu chưa có

sudo apt update && sudo apt install nano -y

Mở docker-compose

nano docker-compose.yml

Nội dung file

version: "3.8"
services:
n8n:
image: n8nio/n8n:latest
restart: always
ports:
- "5678:5678"
environment:
- GENERIC_TIMEZONE=Asia/Ho_Chi_Minh
- N8N_BASIC_AUTH_ACTIVE=true
- N8N_BASIC_AUTH_USER=admin
- N8N_BASIC_AUTH_PASSWORD=123456D
- N8N_HOST=dev102.com
- WEBHOOK_URL=https://dev102.com/
- N8N_PORT=5678
volumes:
- ~/.n8n:/home/node/.n8n

Khởi động

docker compose down
docker compose up -d

Gán lại quyền sở hữu để container

sudo chown -R 1000:1000 ~/.n8n

4. Cài NGINX

sudo apt update
sudo apt install nginx -y

Tạo file config:

sudo nano /etc/nginx/sites-available/dev102.com

code config:

server {
listen 80;
server_name dev102.com www.dev102.com;

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_cache_bypass $http_upgrade;
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;
}
}

Kích hoạt config:

sudo ln -s /etc/nginx/sites-available/dev102.com /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx

5. Cài SSL với Certbot

Cài Certbot:

sudo apt update
sudo apt install nginx certbot python3-certbot-nginx -y

Chạy cấp SSL:

sudo certbot --nginx -d dev102.com -d www.dev102.com

6. Cài Redis

Cài đặt

sudo apt update
sudo apt install redis-server -y

Khởi động

sudo systemctl start redis-server
sudo systemctl enable redis-server

Kiểm tra lại :

sudo systemctl status redis-server

Mở cổng để redis chạy ip ngoài

sudo nano /etc/redis/redis.conf

Tìm dòng:

bind 127.0.0.1 ::1

Sửa thành:

bind 0.0.0.0

Tìm dòng:

protected-mode yes

Sửa thành:

protected-mode no

Thêm pass để chạy rediss

requirepass 123456D

Restart Redis:

sudo systemctl restart redis-server

Test lại từ server:

redis-cli -a 123456D ping

Viết một bình luận