On this page
n8n is one of the most powerful open-source workflow automation tools, but its real power only shows when you host it yourself on a server that runs without interruption. The cloud version (n8n Cloud) is convenient for trying things out, but it charges you per execution and keeps your data on the company's servers. Self-hosting on a VPS, on the other hand, gives you full control, complete privacy for your credentials, and a fixed cost that doesn't balloon no matter how many runs you have. The only problem is that the setup steps confuse beginners: multiple containers, an SSL certificate, webhooks that need a correct public URL, and an encryption key you must never lose. This guide is written to clear up that confusion completely. If you're entirely new to n8n and want to understand the concepts first, start with the guide Automation with n8n from scratch, then come back here for the hands-on setup on your server.
Why self-host n8n on a VPS instead of n8n Cloud?
The first question to settle before running any command: do you host n8n yourself, or subscribe to n8n Cloud? The decision depends on three factors: control, privacy and long-term cost.
Control: when you self-host, you own everything: the version you run, the size of the resources, custom integrations, and even the extra npm packages you install inside the container. No one forces a surprise upgrade on you or locks a feature behind a more expensive plan.
Privacy: for many people, this is the most important reason. With self-hosting, all your data and credentials (API keys, OAuth tokens, passwords) stay encrypted in your own database and never pass through a third party. For companies bound by data protection policies or working in sensitive sectors, that's a decisive difference.
Cost: n8n Cloud charges you by the number of executions or active workflows, and the bill grows as your automation grows. Self-hosting has a fixed cost: the monthly price of a small VPS, with practically unlimited runs (limited only by your server's resources).
| Criterion | Self-hosting on a VPS | n8n Cloud |
|---|---|---|
| Cost | Fixed (the VPS price) | Based on executions/workflows |
| Executions | Practically unlimited | Limited by plan |
| Privacy | Your data on your server | On n8n's servers |
| Version control | Full (you choose when to upgrade) | Managed automatically |
| Maintenance | Your responsibility (updates/backups) | Fully managed |
| Technical level required | Medium | Very low |
| Custom packages/nodes | Fully flexible | Restricted |
| Time to get started | Initial setup (this guide) | Minutes |
The practical takeaway: if you want maximum control and privacy and low long-term cost, and you don't mind half an hour of initial setup, self-hosting is the choice. If you're just experimenting or don't want any maintenance, start with Cloud and move later. The rest of this guide assumes you've chosen self-hosting.
What do you need before you start?
Before you type the first command, make sure the following items are ready. Missing any one of them will stop you halfway:
| Requirement | Details | How to get it |
|---|---|---|
| VPS server | A clean Linux distribution (Ubuntu 22.04/24.04 recommended) | From your hosting provider's panel |
| Public IP address | A static IPv4 for the server | Comes with the server |
| Domain or subdomain | Such as n8n.example.com | From a domain registrar |
| DNS record of type A | Points from the domain to the server's IP | From the registrar's DNS panel |
| User with sudo privileges | Non-root, for daily work | You create it when preparing the server |
| Two open ports | 80 and 443 (HTTP/HTTPS) | Through the firewall |
Server specs: n8n is relatively lightweight, but its consumption jumps with the number of concurrent workflows and the amount of data it processes. The following table suggests sizes based on the expected load:
| Load | vCPU | RAM | Storage | Notes |
|---|---|---|---|---|
| Testing / personal project | 1 | 1 GB | 20 GB SSD | Enough for a few simple workflows |
| Light, regular use | 1–2 | 2 GB | 25–40 GB SSD | The most common for a serious start |
| Medium production | 2 | 4 GB | 40–80 GB SSD | Dozens of workflows + PostgreSQL |
| Heavy load / team | 4+ | 8 GB+ | 80 GB+ SSD | Consider queue mode (later) |
A practical rule: start with two cores and 2 GB of RAM as a comfortable minimum if you'll run n8n with PostgreSQL in production. Less is possible for testing, but heavy processing (transforming large JSON, generating images, AI calls) can exhaust memory quickly.
DNS setup: before anything else, create an A record in your domain panel that links the subdomain to the server's IP address. Example: n8n → 203.0.113.42. Wait for the record to propagate (minutes to an hour), and verify with dig n8n.example.com +short that it returns the correct IP. This step is a prerequisite for issuing an SSL certificate automatically later, so don't skip it. If you want a deeper understanding of DNS record types and how to configure them, the guide DNS records explained covers them in detail.
Preparing the server itself (updating the system, creating a non-root user, SSH keys, a basic firewall) is a prerequisite for this guide, and we won't repeat it in full here. If you haven't prepared your server yet, first follow the guide Setting up your first VPS server from scratch step by step, then come back here with a clean, secured server.
How do you install Docker and Docker Compose?
We'll run n8n with Docker because it isolates the application and its dependencies in containers, so you can install, update and move the system without polluting the server with conflicting packages. The whole setup comes down to a single file, and upgrading is a single command. We'll install the latest Docker Engine from the official repository (not the old version in the distribution's repositories).
Run the following commands on an Ubuntu server as a user with sudo privileges:
# 1) Remove any old versions that may be installed
for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do
sudo apt-get remove -y $pkg
done
# 2) Install prerequisites and add Docker's official GPG key
sudo apt-get update
sudo apt-get install -y ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg \
-o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
# 3) Add the official Docker repository
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" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
# 4) Install Docker Engine + the Compose plugin (built-in v2)
sudo apt-get update
sudo apt-get install -y docker-ce docker-ce-cli containerd.io \
docker-buildx-plugin docker-compose-pluginAfter installation, add your user to the docker group so you can run commands without sudo every time, then verify the installation succeeded:
# Run docker without sudo (requires logging in again or newgrp)
sudo usermod -aG docker $USER
newgrp docker
# Check the versions
docker --version
docker compose version
# Quick test
docker run --rm hello-worldNote: in modern Docker, Compose is a built-in plugin invoked with docker compose (with a space), not docker-compose (with a hyphen). Every command in this guide uses the modern docker compose syntax.
| Command | What it does |
|---|---|
docker ps | Lists running containers |
docker ps -a | Lists all containers (including stopped ones) |
docker compose up -d | Starts services in the background |
docker compose down | Stops and removes containers (keeping the volumes) |
docker compose logs -f n8n | Follows the n8n container logs live |
docker compose pull | Pulls the latest images |
docker compose restart n8n | Restarts a specific service |
docker volume ls | Lists volumes (where data is stored) |
docker exec -it n8n sh | Opens a shell inside the container |
What container architecture will we build?
Before writing the file, understand the architecture. We won't run n8n on its own, but three cooperating containers on a single internal Docker network, each with a specific role:
- n8n container: the application itself, with the interface, the engine and its internal port 5678. We don't expose this port directly to the internet; we put it behind a proxy.
- PostgreSQL container: the database that stores workflows, credentials and execution history. By default n8n uses an SQLite database (a single file), but it doesn't hold up in production: it breaks down under concurrent writes and grows without limit. PostgreSQL is the right production choice.
- Reverse proxy container: the only entry point from the internet (ports 80 and 443). It terminates SSL (HTTPS) and forwards requests internally to n8n on 5678. We'll use Caddy because it issues and renews a Let's Encrypt certificate automatically with a few lines, making it the simplest option for beginners.
Each container keeps its data in a persistent volume, so data isn't lost when a container is rebuilt or upgraded. This is a key point: containers are ephemeral, but volumes persist.
This separation of layers is what makes the system production-ready: the database is isolated, the proxy protects n8n from direct exposure, and SSL is managed automatically. Now let's turn this architecture into a single docker-compose.yml file.
The complete docker-compose.yml file, explained
Create a project folder and go into it. Inside it, we'll create two files: docker-compose.yml and .env.
mkdir -p ~/n8n && cd ~/n8nFirst, the .env file for all the sensitive and variable values. Don't write these values directly in the compose file. Keep them isolated here, and never push this file to Git:
# ===== Domain and protocol =====
DOMAIN_NAME=n8n.example.com
GENERIC_TIMEZONE=Asia/Riyadh
SSL_EMAIL=admin@example.com
# ===== Core n8n variables =====
N8N_HOST=n8n.example.com
N8N_PROTOCOL=https
N8N_PORT=5678
WEBHOOK_URL=https://n8n.example.com/
# Encryption key: generate it once and keep it forever (don't change it later)
N8N_ENCRYPTION_KEY=put_a_long_random_key_here
# ===== PostgreSQL database =====
DB_TYPE=postgresdb
DB_POSTGRESDB_DATABASE=n8n
DB_POSTGRESDB_HOST=postgres
DB_POSTGRESDB_PORT=5432
DB_POSTGRESDB_USER=n8n
DB_POSTGRESDB_PASSWORD=a_strong_database_password
# Initialization data for the postgres container (used on first run only)
POSTGRES_USER=n8n
POSTGRES_PASSWORD=a_strong_database_password
POSTGRES_DB=n8nTo generate a strong N8N_ENCRYPTION_KEY and database password, use:
openssl rand -hex 32Run it twice, and put the output in the corresponding fields. Now for the docker-compose.yml file itself:
services:
postgres:
image: postgres:16
restart: unless-stopped
environment:
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
- POSTGRES_DB=${POSTGRES_DB}
volumes:
- postgres_data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}"]
interval: 10s
timeout: 5s
retries: 5
networks:
- n8n_net
n8n:
image: docker.n8n.io/n8nio/n8n:latest
restart: unless-stopped
environment:
- N8N_HOST=${N8N_HOST}
- N8N_PROTOCOL=${N8N_PROTOCOL}
- N8N_PORT=${N8N_PORT}
- WEBHOOK_URL=${WEBHOOK_URL}
- N8N_ENCRYPTION_KEY=${N8N_ENCRYPTION_KEY}
- GENERIC_TIMEZONE=${GENERIC_TIMEZONE}
- DB_TYPE=${DB_TYPE}
- DB_POSTGRESDB_DATABASE=${DB_POSTGRESDB_DATABASE}
- DB_POSTGRESDB_HOST=${DB_POSTGRESDB_HOST}
- DB_POSTGRESDB_PORT=${DB_POSTGRESDB_PORT}
- DB_POSTGRESDB_USER=${DB_POSTGRESDB_USER}
- DB_POSTGRESDB_PASSWORD=${DB_POSTGRESDB_PASSWORD}
volumes:
- n8n_data:/home/node/.n8n
depends_on:
postgres:
condition: service_healthy
networks:
- n8n_net
caddy:
image: caddy:2
restart: unless-stopped
ports:
- "80:80"
- "443:443"
volumes:
- ./Caddyfile:/etc/caddy/Caddyfile:ro
- caddy_data:/data
- caddy_config:/config
depends_on:
- n8n
networks:
- n8n_net
volumes:
postgres_data:
n8n_data:
caddy_data:
caddy_config:
networks:
n8n_net:An explanation of the key sections:
restart: unless-stoppedrestarts the container automatically after a server reboot or any crash, which is essential in production.depends_onwithcondition: service_healthyensures n8n doesn't start before PostgreSQL is actually ready (not just "started"), which prevents connection errors at boot.- The volumes (
n8n_data,postgres_data,caddy_data) are where data is stored persistently. Deleting a container doesn't delete them. - n8n doesn't expose its port (
5678) to the host: there's noportsentry under it. It's reached internally, over then8n_netnetwork, from Caddy only.
Finally, create a Caddyfile in the same folder. This is all Caddy needs to issue SSL and route traffic:
n8n.example.com {
reverse_proxy n8n:5678
}Just two lines: Caddy sees the domain, requests a Let's Encrypt certificate automatically, and forwards all requests to the n8n container on its port 5678. That's why we chose Caddy for beginners.
Environment variables, WEBHOOK_URL and N8N_ENCRYPTION_KEY
The two settings that cause beginners the most trouble are WEBHOOK_URL and N8N_ENCRYPTION_KEY. Take them seriously.
WEBHOOK_URL is the public address you give n8n so it can build external webhook URLs. When you create a workflow that starts with a Webhook trigger (for example, to receive a notification from an external service), n8n generates a URL that the external service must be able to reach. If you don't set WEBHOOK_URL to your public domain with the correct protocol, n8n will generate URLs with localhost or an internal IP that external services can't reach, and no webhook will work. Always set it to https://yourdomain/, with a trailing slash.
N8N_ENCRYPTION_KEY is the key n8n uses to encrypt all the credentials stored in the database. The danger is that if it changes or gets lost, n8n won't be able to decrypt your old credentials, and you'll have to re-enter all of them manually. So generate it once, keep it somewhere safe (a password manager), and never change it after the first run. If you don't set it, n8n generates one automatically and stores it inside the settings volume, but setting it manually is safer for backups and migration.
The following table summarizes the most important environment variables:
| Variable | Ideal value | Purpose |
|---|---|---|
N8N_HOST | n8n.example.com | The hostname n8n runs on |
N8N_PROTOCOL | https | The protocol in generated URLs |
N8N_PORT | 5678 | The application's internal port |
WEBHOOK_URL | https://n8n.example.com/ | The public address for webhook URLs |
N8N_ENCRYPTION_KEY | A long random string | Encrypts credentials (don't change it) |
GENERIC_TIMEZONE | Asia/Riyadh | Time zone for scheduling (Cron) |
DB_TYPE | postgresdb | Database type |
DB_POSTGRESDB_HOST | postgres | The database service name on the internal network |
DB_POSTGRESDB_USER | n8n | Database user |
DB_POSTGRESDB_PASSWORD | A strong password | Database password |
A note on GENERIC_TIMEZONE: if you have workflows scheduled with a Schedule/Cron node, the time zone determines when they actually run. Set it to your region (for example, Asia/Riyadh); otherwise things will be scheduled in UTC.
Because n8n is the interface that receives and processes your sensitive data, getting these variables right is a prerequisite for safe, reliable operation, and this is where the value of hosting it on a VPS you fully control becomes clear.
Connecting the domain and issuing SSL: first run and owner registration
Once you've prepared the three files (docker-compose.yml, .env, Caddyfile) and confirmed that a DNS record of type A points to the server's IP, start the stack:
cd ~/n8n
docker compose up -dDocker will pull the images (n8n, postgres, caddy) and start the containers in the background. The first run may take a minute or two. Follow the logs to make sure there are no errors:
docker compose logs -fLook for a line saying that n8n is running and listening on 5678, and that Caddy successfully issued the domain's certificate. To stop following the logs, press Ctrl+C (this only stops following; it doesn't stop the containers).
Now open your browser at https://n8n.example.com. If SSL was issued successfully, you'll see the n8n interface with a secure padlock. The first thing it asks you to do is create the owner account: email, name and password. This first account is the top-level administrator of the instance, so keep its details safe. After registering, you enter the n8n dashboard and can start building workflows.
| Port | Service | Exposed to the internet? | Purpose |
|---|---|---|---|
| 80 | Caddy | Yes | HTTP → HTTPS redirect + Let's Encrypt challenge |
| 443 | Caddy | Yes | Main HTTPS traffic |
| 5678 | n8n | No (internal only) | The application behind the proxy |
| 5432 | PostgreSQL | No (internal only) | Database |
Note the golden rule: the only exposed ports are 80 and 443. Everything else is internal, within the Docker network. This is the foundation of the security hardening we'll go deeper into later.
If you want to build real automations after registering, the guide Practical workflow examples offers ready-made scenarios you can apply directly to your new instance.
How do you build your first workflow after installation?
After registering the owner, you'll see an empty canvas. A workflow in n8n is a chain of nodes that starts with a trigger and then passes through processing steps and actions. Let's look at the flow:
- Trigger: the starting point. It can be a schedule (Schedule), a webhook (Webhook) that receives an external request, or an event from an app (an incoming email, for example).
- Processing nodes: transform data, filter it, make conditional decisions (IF), or call APIs.
- Action nodes: write the result to a destination: sending a message, updating a row in a database, or making an HTTP call.
A practical tip for your first try: create a simple workflow with a Schedule Trigger node (every hour) → an HTTP Request node (fetching data from a public API) → a node that writes the output into a message. Run it manually with the Execute button to watch the data flow between the nodes, then activate it (Active) so it runs automatically on schedule. That's how you confirm your instance works end to end.
How do you upgrade n8n safely?
The advantage of Docker is that upgrading is almost a single command, but the order matters to avoid losing data. The rule: take a backup first, then pull the new image, then recreate the containers.
cd ~/n8n
# 1) Back up the database before any upgrade (important)
docker compose exec postgres pg_dump -U n8n n8n > backup-$(date +%F).sql
# 2) Pull the latest images
docker compose pull
# 3) Recreate the containers with the new images (keeps the volumes)
docker compose up -d
# 4) Make sure everything works
docker compose ps
docker compose logs -f n8nBecause the data lives in volumes separate from the containers, recreating them (up -d) replaces the container images and keeps the data intact. Even so, always take a backup before upgrading: a major upgrade sometimes makes changes to the database schema (migrations), and rolling back requires the backup.
Pinning a specific version instead of latest: in production, many people prefer to pin an explicit version number instead of :latest (for example, docker.n8n.io/n8nio/n8n:1.70.0) so they fully control when upgrades happen and avoid the surprise of pulling an untested release. Change the image tag in the compose file, then run docker compose up -d.
| Step | Command | Why |
|---|---|---|
| Backup | pg_dump ... > backup.sql | Protection before any change |
| Pull the images | docker compose pull | Fetch the new version |
| Recreate | docker compose up -d | Apply the upgrade while keeping the data |
| Verify | docker compose logs -f n8n | Catch migration errors early |
How do you take a reliable backup?
A backup has two components: the database (workflows, credentials and executions) and the n8n settings volume (the encryption key and configuration files). Back them up together.
1) Database backup with pg_dump:
cd ~/n8n
docker compose exec -T postgres pg_dump -U n8n -d n8n > n8n-db-$(date +%F).sql2) Backup of the n8n settings volume (it contains the configuration data and the encryption key, if it was generated automatically):
docker run --rm \
-v n8n_n8n_data:/data \
-v $(pwd):/backup \
alpine tar czf /backup/n8n-data-$(date +%F).tar.gz -C /data .The volume name may be prefixed with the folder name (such as
n8n_n8n_data). Check withdocker volume ls.
Restoring the database from a backup:
cat n8n-db-2026-06-13.sql | docker compose exec -T postgres psql -U n8n -d n8nAutomating backups: add both commands to a daily cron job on the server, and move the backups to external storage (object storage such as R2 or S3) so you don't lose them if the server itself fails. For a deeper look at complete backup strategies, see the website backup guide.
| Item | Backup tool | Recommended frequency |
|---|---|---|
| Database | pg_dump | Daily |
| n8n settings volume | tar for the volume | Weekly or after major changes |
The N8N_ENCRYPTION_KEY key | Save in a password manager | Once (it doesn't change) |
The .env and docker-compose.yml files | Copy to a safe place off the server | After any change |
How do you secure n8n on the server?
n8n stores API keys and sensitive tokens, so securing it isn't a luxury. Follow these layers:
1) Firewall: expose only 80 and 443. This is the most important rule. Close every port except SSH (22) and HTTP/HTTPS. Never expose 5678 (n8n) or 5432 (PostgreSQL) to the internet; the proxy alone is your gateway.
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow OpenSSH
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable
sudo ufw status2) Mandatory HTTPS: thanks to Caddy, all traffic is encrypted automatically. Make sure N8N_PROTOCOL=https is set and WEBHOOK_URL starts with https://.
3) Authentication: the owner account is protected by a password. Use a strong, unique password, and enable two-factor authentication (2FA) in the n8n settings if it's available in your version. If you want an extra layer, you can put n8n behind Basic Auth at the proxy level.
4) No exposed internal ports: as in the compose file, we didn't add ports under n8n or postgres, and that's intentional. Access is internal, over the Docker network only.
5) Regular updates: update n8n (the upgrade above) and the server's operating system regularly to close vulnerabilities.
6) Keep the encryption key safe: store N8N_ENCRYPTION_KEY off the server (in a password manager), because it's the key to decrypting all your credentials.
For a deeper dive into securing the server itself (Fail2ban, disabling root login, SSH keys, automatic updates), the guide Setting up your first VPS server from scratch covers these layers with commands.
| Security layer | Action | Priority |
|---|---|---|
| Firewall | Open only 22/80/443 | Critical |
| Encryption | HTTPS via Caddy | Critical |
| Internal ports | Don't expose 5678 and 5432 | Critical |
| Authentication | Strong password + 2FA | High |
| Updates | Regular upgrades for n8n and the OS | High |
| Backups | pg_dump + off-site copies | High |
Reverse proxy options: Caddy vs. Nginx vs. Traefik
We used Caddy for its simplicity, but you have alternatives depending on your needs and experience. They all do the same job (terminating SSL + forwarding to 5678), but they differ in complexity and flexibility:
| Proxy | SSL issuance | Complexity | Best for |
|---|---|---|---|
| Caddy | Fully automatic (two lines) | Very low | Beginners and quick setups |
| Nginx + Certbot | Manual/scheduled via Certbot | Medium | Those who know Nginx and want fine-grained control |
| Traefik | Automatic via Docker labels | Medium to high | Multi-container/multi-service environments |
Nginx + Certbot is a strong option if you already run other services on Nginx. You write a server block whose proxy_pass points to http://127.0.0.1:5678, then issue the certificate with sudo certbot --nginx -d n8n.example.com and renew it automatically with the Certbot timer. The difference is that you handle certificate renewal and webhook header settings yourself.
Traefik shines when you run several containerized services: it discovers containers automatically through labels in the compose file and issues their certificates without a separate configuration file for each one. But it's a bit more complex for a beginner running a single service, which is why we started with Caddy.
The advice: start with Caddy unless you have a specific reason to choose something else. You can switch proxies later without touching n8n or the database.
Queue mode for scaling
In the default setup (regular mode), a single n8n container runs all the workflows. That's enough for most cases. But under heavy load (hundreds of concurrent executions, long-running processing), the single container can become a bottleneck.
That's where queue mode comes in: it separates a main process that handles incoming requests and the interface from several worker processes that run tasks in parallel, connected by a message queue through Redis. You add a Redis container, run one or more workers with the same command plus --worker, and set EXECUTIONS_MODE=queue. The result is horizontal scaling: you add more workers as the load grows.
This is only a pointer, because it's advanced and beginners don't need it. Start with regular mode, and when you notice executions getting delayed or piling up, then consider queue mode and Redis. The architecture we built (Docker Compose + PostgreSQL) scales to queue mode smoothly, without rebuilding from scratch.
Troubleshooting common problems
The most common failures and their direct fixes:
| Error/symptom | Likely cause | Fix |
|---|---|---|
| Webhook doesn't work / points to localhost | WEBHOOK_URL isn't set to the public domain | Set WEBHOOK_URL=https://yourdomain/ and restart |
| 502 Bad Gateway | n8n hasn't started yet, or is on the wrong port | Check docker compose logs n8n and that the proxy forwards to n8n:5678 |
| SSL certificate isn't issued | DNS doesn't point to the server, or port 80 is closed | Make sure the A record is correct and port 80 is open in the firewall |
| Container keeps restarting | Database connection failure | Review the DB_POSTGRESDB_* values and depends_on healthy |
| Credentials lost after an upgrade | N8N_ENCRYPTION_KEY changed | Restore the original key from your backup |
All data lost after down | Volumes deleted by mistake (down -v) | Use down without -v; restore from the backup |
| Database refuses connections | postgres wasn't ready when n8n started | Use condition: service_healthy (already in the compose file) |
| Scheduled tasks run at the wrong time | GENERIC_TIMEZONE isn't set | Set it to your region, such as Asia/Riyadh |
| "Cannot connect to the Docker daemon" | Your user isn't in the docker group | sudo usermod -aG docker $USER, then log in again |
Quick diagnostic commands for any failure:
# Container status
docker compose ps
# Logs for a specific service
docker compose logs --tail=100 n8n
docker compose logs --tail=100 caddy
# Test reaching n8n internally from inside the Docker network
docker compose exec caddy wget -qO- http://n8n:5678/healthz
# Check the database connection
docker compose exec postgres pg_isready -U n8nCritical warning: don't use docker compose down -v unless you really mean to delete all your data. The -v flag deletes the volumes (the database and the settings). For a normal shutdown, use only docker compose down or docker compose stop.
Expert tips before you get going
- Pin an explicit version number in production instead of
:latest, and upgrade deliberately after reading the release notes. - Keep
.envcompletely out of Git and add it to.gitignore; it holds all your secrets. - Actually test restoring a backup once on a test server. A backup that's never tested isn't a backup.
- Monitor memory usage with
docker stats; heavy workflows (AI, file processing) use more than you'd expect. - Set a limit on execution history (pruning) so the database doesn't grow endlessly over time.
- Use a dedicated subdomain for n8n (such as
n8n.orautomation.) to separate it from your main site and make SSL management easier.
With that, you've gone from an empty server to a production n8n instance: isolated containers, a stable database, automatic SSL, working webhooks, backups and tight security. All that's left is building the automations that will save you hours every week. And if you want to launch your instance on a fast, stable server that runs without interruption, pick a suitable VPS plan and start today.
Frequently asked questions
Do I need Linux experience to install n8n on a VPS? Basic experience is enough: connecting over SSH, copying and pasting commands, and editing a text file. This guide gives you every command ready to use. If you're completely new, read the guide to setting up your first VPS first so you get comfortable working with a server.
Why PostgreSQL instead of the default SQLite? SQLite is a single file that's fine for testing, but in production it struggles under concurrent writes, grows without limit and is hard to back up. PostgreSQL is more robust, more reliable and more flexible for backups and migration, and it's the production standard for n8n.
What are the minimum VPS specs for n8n? For testing, one core and 1 GB of RAM are enough. For serious use with PostgreSQL, two cores and 2 GB of RAM are the comfortable minimum. Heavy workloads (dozens of workflows or AI processing) need 4 GB or more.
What if I lose N8N_ENCRYPTION_KEY? n8n won't be able to decrypt your stored credentials, and you'll have to re-enter all of them manually. So generate the key once, save it in a password manager, and never change it. Make it part of your backup.
Why isn't my webhook working?
The most common reason is that WEBHOOK_URL isn't set to your public domain over HTTPS. Set it to https://yourdomain/ and restart the container. Also make sure the domain resolves correctly and that the proxy is issuing a valid certificate.
How do I issue an SSL certificate automatically?
Caddy issues and renews it automatically with a few lines in the Caddyfile, and it's the simplest option. For it to work, a DNS record of type A must point to the server's IP, and port 80 must be open so the Let's Encrypt challenge can complete. The alternative is Nginx with Certbot.
How do I upgrade n8n without losing my data?
Take a pg_dump backup first, then run docker compose pull to fetch the new image, then docker compose up -d to recreate the containers. The data lives in separate volumes, so it stays intact. Avoid down -v, because it deletes the volumes.
Can I run multiple sites/services alongside n8n on the same server? Yes. Put each service in a container and route it through a single reverse proxy using different subdomains. Caddy or Traefik manage certificates for multiple domains automatically. Just make sure there's enough memory for all the services.
What's the difference between regular mode and queue mode? In regular mode a single container runs everything, which is enough for most cases. Queue mode separates the interface from workers that run executions in parallel through Redis, so it scales horizontally for heavy workloads. Start with regular mode and move to queue mode when executions actually start piling up.
Should I expose port 5678 for direct access? No. Never expose 5678 (n8n) or 5432 (PostgreSQL) to the internet. Access goes only through the reverse proxy on 80/443, and the proxy routes traffic internally. Exposing the port directly opens a major security hole.
How much does self-hosting n8n cost compared with n8n Cloud? Self-hosting has a roughly fixed cost equal to the monthly VPS price (starting from a small amount for a modest server), with practically unlimited executions. n8n Cloud's bill grows with the number of executions and active workflows. In the long run and for heavy use, self-hosting is clearly cheaper.
Sources
Frequently asked questions
Do I need Linux experience to install n8n on a VPS?
Basic experience is enough: connecting over SSH, copying and pasting commands, and editing a text file. This guide gives you every command ready to use. If you're completely new, read the guide to setting up your first VPS first so you get comfortable working with a server.
Why PostgreSQL instead of the default SQLite?
SQLite is a single file that's fine for testing, but in production it struggles under concurrent writes, grows without limit and is hard to back up. PostgreSQL is more robust, more reliable and more flexible for backups and migration, and it's the production standard for n8n.
What are the minimum VPS specs for n8n?
For testing, one core and 1 GB of RAM are enough. For serious use with PostgreSQL, two cores and 2 GB of RAM are the comfortable minimum. Heavy workloads (dozens of workflows or AI processing) need 4 GB or more.
What if I lose N8N_ENCRYPTION_KEY?
n8n won't be able to decrypt your stored credentials, and you'll have to re-enter all of them manually. So generate the key once, save it in a password manager, and never change it. Make it part of your backup.
Why isn't my webhook working?
The most common reason is that WEBHOOK_URL isn't set to your public domain over HTTPS. Set it to https://yourdomain/ and restart the container. Also make sure the domain resolves correctly and that the proxy is issuing a valid certificate.
How do I issue an SSL certificate automatically?
Caddy issues and renews it automatically with a few lines in the Caddyfile, and it's the simplest option. For it to work, a DNS record of type A must point to the server's IP, and port 80 must be open so the Let's Encrypt challenge can complete. The alternative is Nginx with Certbot.
How do I upgrade n8n without losing my data?
Take a pg_dump backup first, then run docker compose pull to fetch the new image, then docker compose up -d to recreate the containers. The data lives in separate volumes, so it stays intact. Avoid down -v, because it deletes the volumes.
Can I run multiple sites/services alongside n8n on the same server?
Yes. Put each service in a container and route it through a single reverse proxy using different subdomains. Caddy or Traefik manage certificates for multiple domains automatically. Just make sure there's enough memory for all the services.
What's the difference between regular mode and queue mode?
In regular mode a single container runs everything, which is enough for most cases. Queue mode separates the interface from workers that run executions in parallel through Redis, so it scales horizontally for heavy workloads. Start with regular mode and move to queue mode when executions actually start piling up.
Should I expose port 5678 for direct access?
No. Never expose 5678 (n8n) or 5432 (PostgreSQL) to the internet. Access goes only through the reverse proxy on 80/443, and the proxy routes traffic internally. Exposing the port directly opens a major security hole.
How much does self-hosting n8n cost compared with n8n Cloud?
Self-hosting has a roughly fixed cost equal to the monthly VPS price (starting from a small amount for a modest server), with practically unlimited executions. n8n Cloud's bill grows with the number of executions and active workflows. In the long run and for heavy use, self-hosting is clearly cheaper.