cd /news/ai-tools/self-host-n8n-on-a-vps-with-docker-a… · home › topics › ai-tools › article
[ARTICLE · art-173719] src=dev.to ↗ pub= topic=ai-tools verified=true sentiment=· neutral

Self-host n8n on a VPS with Docker and Caddy (and claim the owner account before anyone else does)

A developer published a step-by-step guide for self-hosting the n8n workflow automation tool on a single Linux VPS using two Docker containers (n8n and Caddy) with automatic HTTPS via Caddy's reverse proxy. The guide emphasizes starting only the n8n container first and creating the owner account through an SSH tunnel, since the first person to open a fresh n8n instance claims the owner account. It also covers DNS A/AAAA records, opening ports 80 and 443, installing Docker from its official repository, and pinning a specific stable image version instead of the latest tag.

recommendation

Bind n8n to 127.0.0.1 and bring up only the n8n container first, then claim the owner account through an SSH tunnel before exposing Caddy on ports 80 and 443.

more in playbook: Running AI in production
by read6 min views6 publishedNov 21, 2026

n8n is a workflow automation tool: you connect apps, APIs, AI models and scripts as boxes on a canvas, and the workflows run on their own. The cloud version is easy, but self-hosting gives you your own data, no per-execution pricing, and a server that keeps running while your laptop is asleep.

This guide sets it up on a single Linux VPS. It is short on purpose: two containers, one config file, automatic HTTPS.

n8n.example.com. A note on the licence: the self-hosted Community Edition is free for your own internal business or personal use under the n8n Sustainable Use License. Read it before you host n8n for clients.

At your DNS provider, add an A record for n8n with your server's IPv4 address. Check it from your own computer:

nslookup n8n.example.com

The answer should be your server's address. This usually takes a few minutes. If there is also an AAAA (IPv6) record for that name that does not point to this server, remove it, or the certificate step can fail.

Ports 80 and 443 must be reachable from the internet. If your provider has a firewall in its control panel, open them there too.

The Docker in Ubuntu's default repository is often old, so use Docker's repository, as their Ubuntu guide describes:

apt update && apt -y upgrade
apt install -y ca-certificates curl
install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
chmod a+r /etc/apt/keyrings/docker.asc
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo "$VERSION_CODENAME") stable" > /etc/apt/sources.list.d/docker.list
apt update
apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

The idea:

127.0.0.1:5678, so it is never reachable from the internet directly.

mkdir -p /opt/n8n && cd /opt/n8n
nano compose.yaml
name: n8n
services:
  n8n:
    image: docker.n8n.io/n8nio/n8n:VERSION   # replace VERSION, see below
    restart: unless-stopped
    ports:
      - "127.0.0.1:5678:5678"
    environment:
      - N8N_HOST=n8n.example.com
      - N8N_PROTOCOL=https
      - WEBHOOK_URL=https://n8n.example.com/
      - N8N_PROXY_HOPS=1
      - GENERIC_TIMEZONE=Europe/London
      - TZ=Europe/London
    volumes:
      - n8n_data:/home/node/.n8n
  caddy:
    image: caddy:2
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    command: caddy reverse-proxy --from n8n.example.com --to n8n:5678
    volumes:
      - caddy_data:/data
volumes:
  n8n_data:
    name: n8n_data
  caddy_data:
    name: caddy_data

Replace n8n.example.com (three places) and the time zone with your own, and replace VERSION with a complete stable version number from the n8n release notes (Compose will fail if you leave the word VERSION in).

Why these settings:

latest tag means docker compose pull never jumps to a new major version by surprise. You choose when to upgrade (step 7).name: n8n and the named volumesn8n_data, caddy_data), so the backup command in step 6 always finds the right data.WEBHOOK_URL makes n8n show the public HTTPS address in webhook nodes, not localhost. N8N_PROXY_HOPS=1 tells n8n there is one reverse proxy in front of it, so it reads the real client IP from Caddy's headers. This is the step most guides skip. The first person to open a fresh n8n creates the owner account. If you start Caddy first, anyone who finds your subdomain before you do can claim your instance.

So start only n8n:

docker compose up -d n8n

On your own computer, open an SSH tunnel and keep the window open:

ssh -L 5678:127.0.0.1:5678 root@YOUR.SERVER.IP

Open http://localhost:5678 in Chrome or Firefox and create the owner account with a strong password. (Safari refuses the n8n login over plain http://localhost, so use another browser for this one step.)

Now start everything and close the tunnel:

docker compose up -d

Open https://n8n.example.com. If it does not load straight away, give Caddy a minute to get the certificate and check:

docker compose logs caddy
ufw allow OpenSSH
ufw enable

(If SSH runs on another port, allow that port instead.)

One gotcha worth knowing: ports published by Docker bypass ufw. Here only Caddy publishes 80/443 and n8n is bound to 127.0.0.1, so that is fine. But if you later add a database or another container with ports: - "5432:5432", it will be open to the whole internet even though ufw "denies" it. Bind such ports to 127.0.0.1 or leave them out.

Everything that matters (workflows, credentials and the encryption key that unlocks those credentials) lives in the n8n_data volume. Lose the key and your saved credentials are useless, so back up the whole volume, not just an export of workflows.

First make sure the volume really exists, because docker run would otherwise quietly create an empty one and you would back up nothing. This should print n8n_data; if it prints nothing, stop here and check your compose file:

docker volume ls -q --filter name='^n8n_data$'

For a consistent copy, stop n8n for a few seconds:

cd /opt/n8n
docker compose stop n8n
docker run --rm -v n8n_data:/data:ro -v /opt/n8n:/backup alpine tar czf /backup/n8n-backup.tgz -C /data .
chmod 600 n8n-backup.tgz
docker compose start n8n
ls -lh n8n-backup.tgz

The file should be more than a few kilobytes. It contains your credentials and encryption key, so treat it like a password: copy it off the server (for example with scp) to somewhere private, and do not leave copies lying around. A backup that lives on the same server is not a backup.

To restore: use the same compose file with the same n8n version that made the backup (upgrade afterwards, not during), put the file in /opt/n8n and run the block below. It overwrites whatever is in the n8n_data volume, and each step only runs if the one before it worked, so n8n is not started on half-restored data:

cd /opt/n8n &&
docker compose stop n8n &&
docker volume create n8n_data &&
docker run --rm -v n8n_data:/data -v /opt/n8n:/backup:ro alpine \
  sh -c 'find /data -mindepth 1 -maxdepth 1 -exec rm -rf -- {} + && tar xzf /backup/n8n-backup.tgz -C /data' &&
docker compose up -d

If any step prints an error, stop and fix that before going on.

Pick the new version in the n8n release notes and read what changed (especially for a new major version). Take a backup as above, change the version number in compose.yaml, then:

cd /opt/n8n
docker compose pull && docker compose up -d

If something breaks, put the old version number back and run the same command; restore the backup if the new version already changed your data.

That is the whole setup: one file, two containers, HTTPS, a private first login, a firewall and a backup you can actually restore. From here you can add a PostgreSQL database or queue mode when you outgrow the defaults, but for most personal and small-team automation this is plenty.

I work on HostingBaba, a Linux and Windows VPS host, and we keep a longer version of this guide with plan sizing notes at hostingbaba.net/n8n-vps. The steps above work on any Ubuntu 24.04 server, wherever it runs.

n8n is a trademark of n8n GmbH. This post is not affiliated with or endorsed by n8n.

── more in #ai-tools 4 stories · sorted by recency
── more on @n8n 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
→ Live at https://your-agent.zahid.host ✓
Get free account → Pricing
from €0/mo · no card required
LIVE [news/self-host-n8n-on-a-v…] indexed:0 read:6min 2026-11-21 · —