Self-hosting n8n Guide from A-Z, full code

This article will guide you through self-hosting n8n with docker + ubuntu from A-Z. If you have any questions, feel free to leave them in the comments. Follow the video to proceed step by step for the best results.

Link to purchase demo VPS with 7-day free trial: https://tino.vn/vps-gia-re

  • Discount code 20% (buy 1-2 year):
    tinon8n20
  • Discount code 40% (buy 3-5 year):
    tinon8n40

Code to install self-hosted n8n and Redis

1. Create directory and configuration file

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

2. Install Docker

# 1. Install prerequisite packages
sudo apt update
sudo apt install ca-certificates curl gnupg lsb-release -y
# 2. Add 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. Add Docker repository
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. Install Docker Engine + Compose Plugin
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io docker-compose-plugin -y

3. Docker-compose.yml content

*** Command to install nano if you don’t have it

sudo apt update && sudo apt install nano -y

Open docker-compose

nano docker-compose.yml

File content

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

Start up

docker compose down
docker compose up -d

Reassign ownership for the container

sudo chown -R 1000:1000 ~/.n8n

4. Install NGINX

sudo apt update
sudo apt install nginx -y

Create config file:

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

Config code:

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;
    }
}

Activate config:

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

5. Install SSL with Certbot

Install Certbot:

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

Run SSL certification:

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

6. Install Redis

Installation

sudo apt update
sudo apt install redis-server -y

Start service

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

Check status:

sudo systemctl status redis-server

Open port to allow Redis to run on external IP

sudo nano /etc/redis/redis.conf

Find the line:

bind 127.0.0.1 ::1

Change to:

bind 0.0.0.0

Find the line:

protected-mode yes

Change to:

protected-mode no

Add password for Redis

requirepass 123456D

Restart Redis:

sudo systemctl restart redis-server

Test from server:

redis-cli -a 123456D ping

Leave a Comment