Self-host n8n on a VPS: Docker + HTTPS
Run n8n on a VPS with Docker Compose, Postgres, and HTTPS behind a reverse proxy: the WEBHOOK_URL and encryption key traps, plus every error string.
What you are building
n8n is a workflow automation tool: a visual editor where a trigger, a webhook, a schedule, a form submission, fires a chain of nodes that call APIs, reshape data and write to other systems. It has become the default glue for AI-agent workflows because it speaks to every model provider and database without you writing a service. One docker run gets a working editor in two minutes. This guide is about the other ninety percent: making it durable with Postgres instead of the default SQLite file, reachable over HTTPS, and, the part almost everyone gets wrong, making webhooks hand out a URL the outside world can actually reach.
The finished stack is two containers on one Docker network: n8n itself, and a Postgres database holding its workflows and credentials. A reverse proxy on the host terminates TLS and forwards to n8n on localhost, so nothing faces the internet except through that proxy. It sits alongside the other services on the 2026 self-hosting shortlist.
Prerequisites, and the honest limits
Size the VPS memory for the workflows and their data, because executions plus the Node.js runtime eat memory and the out-of-memory killer taking the container mid-run is a miserable way to learn that. If this box will also run something heavier, size it for that service first: a photo library is the usual culprit, and the real RAM floors for PhotoPrism and Immich dwarf anything n8n asks for. The same goes for a media box: a Jellyfin server plus a browsable front end for it, like Halcyon, which rebuilds the library as a 90s rental shop, will claim the RAM and transcoding headroom long before n8n notices.
You need a domain or subdomain, say n8n.example.com, with an A record pointing at the VPS public IP that resolves before you request a certificate. Ports 80 and 443 must be open to the proxy; n8n's own port 5678 must not face the internet. You need Docker Engine and the Compose plugin; if docker compose version errors with docker: 'compose' is not a docker command, Docker cannot find a usable Compose plugin, which does not establish that the old standalone binary is installed. On Ubuntu or Debian with Docker's package repository configured, install the plugin with sudo apt install docker-compose-plugin.
SQLite is fine for a test, Postgres for anything you rely on
n8n's default database is a SQLite file at /home/node/.n8n/database.sqlite. For kicking the tyres it is fine, mount no volume and you lose it on the first container recreate, which is its own lesson. The reason to move to Postgres is not raw speed; SQLite allows only one writer at a time, and n8n does not support distributed queue-mode deployments on SQLite. Postgres avoids that single-writer ceiling and backs up cleanly with pg_dump. Switching later means exporting and importing data with n8n's CLI, so if this box matters, start on Postgres.
DNS and the firewall
Point the record and open the ports first, so the certificate step later does not fail on a name that does not resolve.
dig +short n8n.example.com
curl -s ifconfig.me
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw allow OpenSSH
sudo ufw enableDo not open 5678. The compose file binds n8n to 127.0.0.1:5678 so the published port is accessible only from the Docker host; a ufw allow 5678 does not change that localhost binding. Use Docker Engine 28.0.0 or later: older releases allowed hosts on the same network segment to reach ports published to localhost.
The Compose file
Create a working directory and a docker-compose.yml. This is the whole stack, two services, one private network, two named volumes.
services:
postgres:
image: postgres:16-alpine
restart: unless-stopped
environment:
POSTGRES_USER: n8n
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: n8n
volumes:
- postgres_data:/var/lib/postgresql/data
networks:
- n8n_net
healthcheck:
test: ["CMD-SHELL", "pg_isready -U n8n -d n8n"]
interval: 10s
timeout: 5s
retries: 5
n8n:
image: docker.n8n.io/n8nio/n8n:2.29.10
restart: unless-stopped
ports:
- "127.0.0.1:5678:5678"
environment:
- N8N_HOST=n8n.example.com
- N8N_PORT=5678
- N8N_PROTOCOL=https
- WEBHOOK_URL=https://n8n.example.com/
- N8N_ENCRYPTION_KEY=${N8N_ENCRYPTION_KEY}
- N8N_PROXY_HOPS=1
- GENERIC_TIMEZONE=Europe/London
- DB_TYPE=postgresdb
- DB_POSTGRESDB_HOST=postgres
- DB_POSTGRESDB_PORT=5432
- DB_POSTGRESDB_DATABASE=n8n
- DB_POSTGRESDB_USER=n8n
- DB_POSTGRESDB_PASSWORD=${POSTGRES_PASSWORD}
volumes:
- n8n_data:/home/node/.n8n
networks:
- n8n_net
depends_on:
postgres:
condition: service_healthy
volumes:
postgres_data:
n8n_data:
networks:
n8n_net:A few decisions worth stating outright. DB_POSTGRESDB_HOST=postgres is the service name, which Docker resolves on the shared network, not localhost, which inside the n8n container means n8n itself. The depends_on with condition: service_healthy stops n8n racing Postgres on boot; without it n8n can start before Postgres is ready and fail to connect. The named volume n8n_data at /home/node/.n8n holds the encryption key and, on SQLite, the database, the one directory you must not lose. Pin the image to an exact version, never latest; the reasons are in the upgrade section below.
The secrets file
Never put passwords in the compose file. Put them in a .env file next to it that Compose reads automatically, and generate them so they are actually random.
printf 'POSTGRES_PASSWORD=%s\n' "$(openssl rand -hex 24)" > .env
printf 'N8N_ENCRYPTION_KEY=%s\n' "$(openssl rand -hex 32)" >> .env
chmod 600 .envThe N8N_ENCRYPTION_KEY is the single most important string here, it is the key every stored credential is encrypted with. Set it explicitly rather than letting n8n generate one, because a value you generated is one you can write down and restore. Once n8n has encrypted its first credential with this key, changing it makes every credential undecryptable, so set it once, now, and never touch that line again.
The env vars that decide whether webhooks work
Four variables control how n8n describes itself to the outside world, and getting them wrong is the number-one n8n support question.
N8N_HOSTis the public hostname,n8n.example.com. Leave it at the defaultlocalhostand generated URLs that fall back to this hostname point atlocalhost.N8N_PROTOCOL=httpstells n8n to buildhttps://URLs. The session cookie'sSecureflag is controlled independently byN8N_SECURE_COOKIE, which defaults totrue.N8N_PORT=5678is the port n8n listens on inside the container. It is not the public port; the proxy owns 443.WEBHOOK_URL=https://n8n.example.com/is the one that bites. n8n prints the webhook addresses you paste into Stripe, GitHub or any external caller by building them from these values. If it is empty or unset, n8n falls back to the base URL built fromN8N_PROTOCOL,N8N_HOSTandN8N_PORTand hands youhttps://n8n.example.com:5678/webhook/...or, worse,http://localhost:5678/webhook/..., printed with no error, plausible-looking, and unreachable from the internet, so the caller's requests silently never arrive. A nonempty incorrectWEBHOOK_URLis still used, so a wrong value does not trigger that fallback. Set it to the exact public base URL with the trailing slash, then confirm the webhook node shows a URL with no port. The variable has since been renamed:N8N_WEBHOOK_URLis available from n8n 2.30.0, and the docs tell reverse-proxy setups to set that one.WEBHOOK_URLremains an alias, deprecated from 2.35.0, when n8n starts logging a deprecation warning. The 2.29.10 image this guide pins predates the rename and reads onlyWEBHOOK_URL, so keep the line as written on that tag and rename it when you upgrade to 2.30.0 or later.
N8N_PROXY_HOPS=1 tells n8n's Express server to trust one proxy in front of it, so rate-limiting and any feature that reads the client IP see the real address rather than the proxy's. One variable you deliberately do not set here is N8N_RUNNERS_ENABLED. Task runners, n8n running Code-node logic in a separate process isolated from the main one, were opt-in throughout the 1.x line, where the variable defaulted to false and you had to set it to true. n8n 2.0 turned them on by default, so on the 2.x line this guide pins the variable is deprecated and n8n's docs say you no longer need to set it.
First start
docker compose up -d
docker compose ps
docker compose logs -f n8nA healthy first boot ends with an Editor is now accessible via: line, with an n8n ready on ..., port 5678 line above it. docker compose ps should show both containers Up, with postgres marked (healthy). If n8n sits in a Restarting loop, read the logs, it is almost always the database connection or the volume permissions covered below.
TLS with a reverse proxy
n8n itself speaks plain HTTP on 5678; something in front terminates HTTPS. Two clean choices.
If you already run several containers, put n8n behind a Traefik reverse proxy that issues TLS certificates automatically with a couple of labels, Traefik requests and renews the certificate for you.
If this is the only app on the box, an nginx virtual host with a Let's Encrypt certificate is simpler. Use the Certbot and nginx TLS setup for Ubuntu 24.04 to obtain the certificate, then this server block:
server {
listen 443 ssl;
server_name n8n.example.com;
ssl_certificate /etc/letsencrypt/live/n8n.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/n8n.example.com/privkey.pem;
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-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_read_timeout 3600;
client_max_body_size 16m;
}
}The Upgrade and Connection "upgrade" headers are not optional. n8n pushes live execution updates to the editor over a WebSocket, and without those two lines the login page loads and then hangs with a lost-connection banner. proxy_read_timeout 3600 raises nginx's timeout between successive reads from the upstream server from 60 seconds to 3600 seconds; it is not a limit on the total execution time. The X-Forwarded-Proto $scheme header is the companion to N8N_PROXY_HOPS=1: it tells n8n the original request was HTTPS even though the proxy reaches it over plain HTTP. n8n sets the session cookie's Secure flag from N8N_SECURE_COOKIE, independently of that header, and the browser enforces the flag.
Your first workflow, to make it real
Open https://n8n.example.com/, create the owner account (next section), and build the smallest workflow that proves the path works: a webhook in, an HTTP call, a response out.
- Add a Webhook node. Set the method to
POSTand a path likehello. It shows two URLs, a Test URL and a Production URL, the source of half the "my webhook doesn't work" reports. The Test URL answers one call, and only while you have clicked Listen for test event; it then expires. The Production URL is registered when you publish the workflow and answers from then on. - Add an HTTP Request node after it, pointed at any public JSON API, a GET to
https://api.github.com/zenreturns a one-line string, which is enough. - Add a Respond to Webhook node, and set the Webhook node's Respond option to "Using Respond to Webhook node" so the caller gets the HTTP node's output back.
- Click Publish (top right) and call it:
curl -X POST https://n8n.example.com/webhook/hello. You should get the zen line back, POST in, API call, response out, the shape of most real automations. Older guides tell you to flip an Active toggle here; n8n 2.0 replaced that toggle with Publish and Unpublish.
A scheduled variant swaps the Webhook node for a Schedule Trigger and calls a model endpoint instead, a self-hosted one from Ollama running on the same VPS is a tidy way to build a nightly summariser.
User management, not basic auth
Older n8n guides tell you to set N8N_BASIC_AUTH_ACTIVE=true. Those variables were removed in n8n 1.0 and do nothing now. Authentication today is the owner account: the first time you load the editor, n8n makes you create an email-and-password owner, and that gate is mandatory, there is no anonymous mode. Create it immediately after first boot, before you hand anyone the URL: between docker compose up and that first form submission the instance is claimable by whoever reaches it first. A reverse-proxy basic-auth layer on top is a reasonable extra lock, but it is an additional password gate, not a separate authentication factor. The owner account and everything else in this guide runs on the free community edition. Projects are not part of it: n8n's edition comparison lists projects, alongside SSO, environments and external secrets, as paid features of the Business and Enterprise plans, and free registration of the Community edition adds only folders, debug in editor, and custom execution data. n8n's RBAC documentation explicitly says team projects and project roles are not available on the Community and Registered Community editions. Custom roles require Enterprise and SAML SSO requires Business or Enterprise. Read which n8n features need a paid licence before you plan around them.
Backups: the encryption key first, then the database
The encryption key and database need backing up, along with the n8n data volume for a complete restore.
The N8N_ENCRYPTION_KEY. Every credential you store in n8n, API tokens, database passwords, OAuth secrets, is encrypted at rest with this key. The workflows in Postgres are useless without it: restore the database onto a new box with a different key and n8n cannot decrypt a single credential, with no recovery and no reset. Your .env file holds the key; copy it somewhere off the server, a password-manager entry is ideal, the day you create it. This is the backup that actually matters.
The Postgres database, for the workflows, execution history and the encrypted credentials themselves:
docker compose exec -T postgres pg_dump -U n8n -d n8n \
| gzip > n8n-db-$(date +%F).sql.gzRun that on a schedule and copy the dump off the box. Also back up the n8n_data volume: it holds the settings file and filesystem binary data, which the database dump does not include. To restore on a fresh VPS: bring the stack up once so the database exists, stop n8n, load the dump back with psql, restore the data volume, put the same N8N_ENCRYPTION_KEY into .env, and start n8n. The key must match the restored settings file; a new key leaves the restored credentials unreadable.
Upgrades: pin the tag
The compose file pins n8nio/n8n:2.29.10 rather than latest on purpose. n8n ships a new minor most weeks and occasionally changes database schema or node behaviour between them, so latest means an unattended pull can hand you a build that migrates your database the moment it starts. Pin a version, read the release notes before bumping, n8n calls out breaking changes there, and upgrade deliberately:
docker compose exec -T postgres pg_dump -U n8n -d n8n | gzip > pre-upgrade.sql.gz
# edit the image tag in docker-compose.yml, then:
docker compose pull n8n
docker compose up -d n8n
docker compose logs -f n8nMajor-version jumps are where this matters most. The 2.0 line, for instance, flipped N8N_BLOCK_ENV_ACCESS_IN_NODE to true by default, so any Code node that read process.env silently loses access until you set it back to false; the same release began enforcing strict permissions on the settings file, enabled task runners by default, and replaced the workflow Activate toggle with Publish. Read the 2.0 breaking-changes page before crossing a major boundary. Minor bumps can carry renames too: N8N_WEBHOOK_URL is available from 2.30.0, and from 2.35.0 WEBHOOK_URL is a deprecated alias, so rename that line when upgrading to 2.30.0 or later to avoid the warning from 2.35.0. As of 2026-11-25, n8n's installation docs list 2.49.6 as stable and 2.50.1 as beta. n8n 3.0.0 is currently an early access release. Before moving to 3.x, also read the 3.0 breaking-changes page and run the migration report. n8n runs any needed database migrations automatically on start, that is exactly why the pre-upgrade pg_dump is not optional. Because credentials live encrypted with a key in .env and the data lives in Postgres and the named volume, the containers are disposable: you upgrade by replacing them, and roll back by pinning the previous tag and restoring the database dump and data-volume backup.
Failure modes, with the strings you will see
The requested webhook "POST hello" is not registered. A 404 from calling a webhook whose workflow is not published, or from calling the test path when nobody is listening. Test paths (/webhook-test/...) answer only while you have clicked "Listen for test event"; production paths (/webhook/...) answer only once the workflow is published. The sibling This webhook is not registered for GET requests. Did you mean to make a POST request? means the method is wrong, the node expects POST and you sent GET.
The webhook URL shows a :5678 or localhost. The node displays https://n8n.example.com:5678/webhook/... or http://localhost:5678/.... An empty or unset WEBHOOK_URL (N8N_WEBHOOK_URL from 2.30.0, with the old name deprecated from 2.35.0) makes n8n build the address from N8N_PROTOCOL, N8N_HOST and N8N_PORT; a nonempty wrong value is used as supplied. Set it to https://n8n.example.com/, recreate the container with docker compose up -d, and the port disappears.
There was a problem loading init data in the browser. The editor loaded but cannot reach its own backend API. Check the failed request in the browser's network panel and the proxy and n8n logs to find which request is failing.
password authentication failed for user "n8n" in the logs, with the container restarting. The password n8n sends does not match what the database was initialised with. The trap: Postgres reads POSTGRES_PASSWORD only when it initialises an empty data directory. Start the stack once, then change POSTGRES_PASSWORD in .env, and the existing postgres_data volume still holds the old password. Set it back to the original, or, if you have no data to keep, docker compose down and docker volume rm the postgres volume, then bring it up fresh.
EACCES: permission denied, open '/home/node/.n8n/config' on start. n8n runs as the node user (UID 1000) and cannot write its config directory. This bites people who bind-mount a host folder (./n8n_data:/home/node/.n8n) owned by root. Use the named volume shown above, or if you insist on a bind mount, sudo chown -R 1000:1000 ./n8n_data first.
Permissions 0644 for n8n settings file /home/node/.n8n/config are too wide. Changing permissions to 0600.. From the 2.x line n8n enforces 0600 on that settings file by default and attempts to fix it on boot, this log line announces the attempt, commonly after a bind mount or after a restore copied the file back with loose permissions. Check for a following Could not enforce settings file permissions warning before assuming it succeeded; set N8N_ENFORCE_SETTINGS_FILE_PERMISSIONS=false only if your filesystem genuinely cannot support permissions.
Mismatching encryption keys, the fuller line says the encryption key in the settings file /home/node/.n8n/config does not match the N8N_ENCRYPTION_KEY in your environment. The key in your environment differs from the one n8n wrote into its data volume on a previous run, most often because n8n generated a random key on an earlier boot when the variable was unset, and you then set a different one. Put the original key back in .env, or, only if you truly have no stored credentials worth keeping, delete the config file inside the n8n_data volume and let n8n regenerate it, accepting that existing credentials become unreadable.
A login banner about secure cookies: Your n8n server is configured to use a secure cookie, however you are either visiting this via an insecure URL, or using Safari. N8N_SECURE_COOKIE defaults to true, independently of N8N_PROTOCOL; reaching n8n over plain HTTP instead of the HTTPS proxy conflicts with that secure-cookie setting. Reach it via https://n8n.example.com/. Only if you genuinely cannot use HTTPS should you set N8N_SECURE_COOKIE=false, and never on an internet-facing box.
To put a language model inside those workflows, see building AI workflows with Claude and n8n.
FAQ
Should I use SQLite or Postgres for n8n?
SQLite (the default) is fine for trying n8n out and for a personal instance running one workflow at a time. Move to Postgres for anything you depend on: SQLite allows only one writer at a time, and n8n does not support distributed queue-mode deployments on SQLite. Postgres backs up cleanly with pg_dump. Migrating later requires CLI export and import, so if the box matters, start on Postgres.
Why do my n8n webhooks never fire?
Almost always WEBHOOK_URL, named N8N_WEBHOOK_URL from n8n 2.30.0, with the old name deprecated from 2.35.0. Empty or unset, n8n prints webhook addresses built from N8N_PROTOCOL, N8N_HOST and N8N_PORT, often with a :5678 or localhost in them, that look valid but are unreachable from the internet, so the caller's requests never arrive. A nonempty wrong value is used as supplied, so check that too. Set it to https://n8n.example.com/ and confirm the node shows a URL with no port. The second cause is calling the production URL of a workflow you have not published, which returns The requested webhook ... is not registered.
What must I back up in n8n?
The N8N_ENCRYPTION_KEY from your .env file, because every stored credential is encrypted with it and losing it makes them permanently undecryptable, copy it off the server the day you create it. And a pg_dump of the Postgres database for the workflows, history and credentials. Include the n8n_data volume for its settings and filesystem binary data. A complete restore needs the same key, the dump and the data volume.
How do I put n8n behind HTTPS?
n8n serves plain HTTP on port 5678; a reverse proxy in front terminates TLS. Bind n8n to 127.0.0.1:5678 so the published port is accessible only from the Docker host, then use Traefik with automatic certificates or nginx with a Let's Encrypt certificate. Set N8N_PROTOCOL=https and WEBHOOK_URL=https://your-host/ (N8N_WEBHOOK_URL from 2.30.0, with the old name deprecated from 2.35.0), and make sure the proxy forwards the WebSocket Upgrade headers or the editor hangs.
How do I upgrade n8n safely?
Pin a specific image tag instead of latest, take a pg_dump and back up the data volume first because n8n runs migrations automatically on start, read the release notes for breaking changes, then bump the tag and run docker compose pull n8n && docker compose up -d n8n. The container is disposable, so roll back by pinning the previous tag and restoring the pre-upgrade dump and data-volume backup.