Tutorial: How to Self Host n8n

n8n tutorial logo

Automation is key to saving time and increasing productivity. n8n is a powerful, open-source workflow automation tool that allows you to connect apps, services, and APIs without relying on third-party platforms. By self-hosting n8n, you maintain full control over your data, workflows, and security—ideal for individuals, teams, and businesses that value privacy and customization.

This tutorial will guide you step-by-step through the process of self-hosting n8n on your own server using Docker and Docker Compose. You’ll learn how to set up, configure, and secure your n8n instance, with optional steps for enabling HTTPS using Let’s Encrypt. Whether you’re a developer, IT professional, or tech enthusiast, this guide will help you deploy a robust automation platform tailored to your needs.

Prerequisites

  1. Docker and Docker Compose installed
  2. A domain name (optional, but recommended for HTTPS)
  3. A reverse proxy (like Nginx or Traefik, optional but recommended)
  4. A server with at least 1GB RAM

1. Install Docker & Docker Compose

If you haven’t already, install Docker and Docker Compose:

# Install Docker
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io

# Install Docker Compose
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

2. Create a Directory for n8n

mkdir ~/n8n
cd ~/n8n

3. Create a docker-compose.yml File

Create a file called docker-compose.yml with the following content:

version: '3.8'

services:
  n8n:
    image: n8nio/n8n
    restart: always
    ports:
      - "5678:5678" # n8n default port
    environment:
      - N8N_HOST=${N8N_HOST:-yourdomain.com}
      - N8N_PROTOCOL=https # Change to http if not using SSL
      - N8N_PORT=5678
      - NODE_ENV=production
      - N8N_BASIC_AUTH_ACTIVE=true
      - N8N_BASIC_AUTH_USER=${N8N_USER:-n8n}
      - N8N_BASIC_AUTH_PASSWORD=${N8N_PASSWORD:-yourpassword}
      - DB_TYPE=sqlite # Or postgresdb for production
      - N8N_ENCRYPTION_KEY=${N8N_ENCRYPTION_KEY:-yourrandomkey}
    volumes:
      - ~/.n8n:/home/node/.n8n

Replace placeholders like ${N8N_HOST}, ${N8N_USER}, ${N8N_PASSWORD}, and ${N8N_ENCRYPTION_KEY} with your own values.

4. (Optional) Set Environment Variables

Create a .env file in the same directory:

N8N_HOST=yourdomain.com
N8N_USER=admin
N8N_PASSWORD=supersecretpassword
N8N_ENCRYPTION_KEY=averylongrandomstring

5. Start n8n

docker-compose up -d

This will pull the n8n image and start it in detached mode.

6. Access n8n

Open your browser and go to:

http://localhost:5678

Or, if you set up a domain and reverse proxy:

https://yourdomain.com

You’ll be prompted to log in with the credentials you set in .env.

7. (Optional) Set Up Reverse Proxy with Nginx

If you want to use a domain and HTTPS, set up Nginx as a reverse proxy.

Example Nginx Configuration

server {
    listen 80;
    server_name yourdomain.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;
    }
}

For HTTPS, use Certbot to obtain a free SSL certificate.

8. (Optional) Use PostgreSQL Instead of SQLite

For production, you should use PostgreSQL. Update your docker-compose.yml:

services:
  db:
    image: postgres:14
    restart: always
    environment:
      - POSTGRES_USER=n8n
      - POSTGRES_PASSWORD=strongpassword
      - POSTGRES_DB=n8n
    volumes:
      - postgres_data:/var/lib/postgresql/data

  n8n:
    # ... same as before ...
    environment:
      - DB_TYPE=postgresdb
      - DB_POSTGRESDB_HOST=db
      - DB_POSTGRESDB_PORT=5432
      - DB_POSTGRESDB_USER=n8n
      - DB_POSTGRESDB_PASSWORD=strongpassword
      - DB_POSTGRESDB_DATABASE=n8n
    depends_on:
      - db

volumes:
  postgres_data:

9. (Optional) Backups

Back up your ~/.n8n directory and your database regularly.

10. (Optional) Update n8n

To update n8n, pull the latest image and restart:

docker-compose pull
docker-compose up -d

References

Leave a Reply

Your email address will not be published. Required fields are marked *