How to install n8n on a VPS (Docker, Nginx, SSL)

A production-shaped n8n install on Ubuntu: Docker with a real Postgres database, your own domain, an Nginx reverse proxy and a free HTTPS certificate. No SQLite, no port 5678 exposed to the world — the setup you would actually trust with live automations.

Disclosure: contains an affiliate link to Inferno VPS.

What you get

By the end you will have n8n running at https://n8n.example.com, backed by Postgres, restarting on reboot, with webhook URLs that actually resolve from the public internet. This is the base every other guide in the n8n silo builds on.

Prerequisites

This guide was tested on Ubuntu 22.04 with n8n 1.6x and Postgres 16 on a 2 GB Inferno VPS in Frankfurt.

Step 1 — Install Docker and Compose

Use Docker's official convenience script, then add your user to the docker group so you can run it without sudo:

curl -fsSL https://get.docker.com | sudo sh
sudo usermod -aG docker $USER
newgrp docker
docker --version
docker compose version

If you want the fuller walkthrough with the apt repository method, see install Docker on an Ubuntu VPS.

Step 2 — Point your domain at the VPS

In your DNS provider, create an A record for n8n.example.com pointing at the server's public IPv4. Give it a few minutes and confirm:

dig +short n8n.example.com

It should print your VPS IP before you continue — Let's Encrypt will fail otherwise.

Step 3 — Create the project and Compose file

mkdir -p ~/n8n && cd ~/n8n
nano docker-compose.yml
services:
  db:
    image: postgres:16
    restart: unless-stopped
    environment:
      POSTGRES_USER: n8n
      POSTGRES_PASSWORD: ${DB_PASSWORD}
      POSTGRES_DB: n8n
    volumes:
      - ./pgdata:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U n8n"]
      interval: 10s
      timeout: 5s
      retries: 5

  n8n:
    image: docker.n8n.io/n8nio/n8n:latest
    restart: unless-stopped
    depends_on:
      db:
        condition: service_healthy
    ports:
      - "127.0.0.1:5678:5678"
    environment:
      - DB_TYPE=postgresdb
      - DB_POSTGRESDB_HOST=db
      - DB_POSTGRESDB_DATABASE=n8n
      - DB_POSTGRESDB_USER=n8n
      - DB_POSTGRESDB_PASSWORD=${DB_PASSWORD}
      - N8N_HOST=n8n.example.com
      - N8N_PROTOCOL=https
      - WEBHOOK_URL=https://n8n.example.com/
      - N8N_ENCRYPTION_KEY=${N8N_ENCRYPTION_KEY}
      - GENERIC_TIMEZONE=Europe/Berlin
      - N8N_PROXY_HOPS=1
    volumes:
      - ./n8n-data:/home/node/.n8n

Two things matter here. Binding the port to 127.0.0.1:5678 means n8n is only reachable through Nginx, never directly. And N8N_ENCRYPTION_KEY is the master key for every stored credential — set it now and never lose it. The full list is in the environment variables reference.

Step 4 — Create the .env file

cat > .env <<EOF
DB_PASSWORD=$(openssl rand -hex 24)
N8N_ENCRYPTION_KEY=$(openssl rand -hex 24)
EOF
cat .env   # copy the encryption key somewhere safe

Back up that encryption key immediately. If you lose it, a restored database is useless — see backup and restore.

Step 5 — Start the stack

docker compose up -d
docker compose ps
curl -I http://127.0.0.1:5678

The curl should return an HTTP response (a redirect or 200). n8n is now running locally; the last job is to expose it over HTTPS.

Step 6 — Nginx reverse proxy and SSL

sudo apt update
sudo apt install -y nginx certbot python3-certbot-nginx
sudo nano /etc/nginx/sites-available/n8n
server {
    listen 80;
    server_name n8n.example.com;

    location / {
        proxy_pass http://127.0.0.1:5678;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        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;
        proxy_read_timeout 3600s;
    }
}

The Upgrade / Connection headers are not optional — n8n's editor uses WebSockets and the UI feels broken without them. Enable the site and issue the certificate:

sudo ln -s /etc/nginx/sites-available/n8n /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx
sudo certbot --nginx -d n8n.example.com --redirect

certbot rewrites the config to listen on 443 and auto-renews via a systemd timer. The full reverse-proxy deep dive, including webhook edge cases, is in Nginx reverse proxy + SSL for n8n.

Step 7 — First login and owner account

Open https://n8n.example.com. n8n prompts you to create the owner account on first run. Do it immediately — until you do, anyone who finds the URL can claim it. Build a one-node workflow with a Webhook trigger and confirm the generated URL uses https://n8n.example.com/, not localhost. If it shows localhost, your WEBHOOK_URL env var did not apply — recheck Step 3.

Need a VPS for n8n?

n8n runs comfortably from 2 GB RAM for personal automations and wants 4 GB+ once you add Postgres, heavy nodes and concurrent executions. Inferno's NVMe plans start at $3.49/mo with eight EU + Asia locations and DDoS protection included.

View Inferno plans →

Common install problems

"This site can't provide a secure connection" after certbot

DNS had not propagated when certbot ran, so it issued for the wrong target or failed silently. Confirm dig +short n8n.example.com shows your IP, then re-run the certbot command.

Webhooks return a localhost URL

Set both N8N_HOST and WEBHOOK_URL and recreate the container with docker compose up -d. Environment changes need a container recreate, not just a restart.

Editor loads but nodes hang or the canvas is laggy

Missing WebSocket headers in Nginx. Confirm the Upgrade and Connection "upgrade" lines are present and reload Nginx.

Database connection refused on first boot

n8n started before Postgres was ready. The depends_on: condition: service_healthy block handles this; if you removed it, add it back or simply docker compose restart n8n.

What to do next


Author: Marek Vainionpää — SRE, Helsinki. Last tested: 2026-05-20 on n8n 1.62, Postgres 16, Ubuntu 22.04, Inferno 2 GB plan (Frankfurt).