Skip to contentcloudvix
cloudvix
GuideDevelopers & AI

Install n8n on your own server with Docker (and with an AI assistant)

Denys DunaievskyiFounder, Cloudvix
  • 19 min read
  • Published
  • LevelIntermediate
Guides
On this page

Key takeaways

  • Only Caddy publishes ports, so n8n and the database are reachable inside Docker only.
  • N8N_ENCRYPTION_KEY encrypts the credentials that workflows store: if you lose it, those credentials cannot be read again, even from a backup.
  • You should see SSH, 80 and 443, and nothing for 5678 or 5432. Docker publishes ports around ufw, so this list is the reliable check.

n8n connects services and automates tasks with workflows. Run on your own server it keeps your workflows and the credentials they use under your control. This guide sets up n8n with PostgreSQL behind a reverse proxy that gets the HTTPS certificate for you.

Before you start

You need an Ubuntu 24.04 server with Docker and the Compose plugin, a user with sudo and a domain name you control. If you are missing one of these, the first-hour and Docker guides come first; links are at the end of this page. Commands use sudo docker; leave sudo out if your user is in the docker group.

  • an A record (and an AAAA record for IPv6 if you want it) for a name such as n8n.example.com that points to the server
  • ports 80 and 443 reachable from the internet: the proxy needs 80 to get its certificate
  • a password manager or another place outside the server to keep one secret key (step 2 explains which)

The steps

Point the domain at the server

Create the DNS record at your domain provider, wait a few minutes and check that the name resolves to your server’s address. Without this the certificate step in step 5 fails. Use your real name instead of n8n.example.com everywhere.

On the serverbash
getent hosts n8n.example.com

Create the folder and the secrets

The file .env holds the settings and two random secrets generated on the server. Edit the domain in it afterwards, for example with nano .env. N8N_ENCRYPTION_KEY encrypts the credentials that workflows store: if you lose it, those credentials cannot be read again, even from a backup.

On the serverbash
mkdir -p ~/n8n && cd ~/n8n
cat > .env <<EOF
N8N_HOST=n8n.example.com
POSTGRES_PASSWORD=$(openssl rand -hex 24)
N8N_ENCRYPTION_KEY=$(openssl rand -hex 32)
TIMEZONE=Etc/UTC
EOF
chmod 600 .env
Tip

Copy that one line to your password manager now.

Write compose.yaml

Save this file in the same folder. It starts PostgreSQL, n8n and Caddy. Only Caddy publishes ports, so n8n and the database are reachable inside Docker only. Caddy obtains and renews the HTTPS certificate for the domain in N8N_HOST by itself. The n8n image has no version tag here, which gives you the latest release at each pull; for steady operation pin a version you have read the release notes of.

On the server · ~/n8n/compose.yamlyaml
services:
  postgres:
    image: postgres:16
    restart: unless-stopped
    environment:
      POSTGRES_USER: n8n
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: n8n
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U n8n -d n8n"]
      interval: 5s
      timeout: 5s
      retries: 10

  n8n:
    image: docker.n8n.io/n8nio/n8n
    restart: unless-stopped
    environment:
      DB_TYPE: postgresdb
      DB_POSTGRESDB_HOST: postgres
      DB_POSTGRESDB_DATABASE: n8n
      DB_POSTGRESDB_USER: n8n
      DB_POSTGRESDB_PASSWORD: ${POSTGRES_PASSWORD}
      N8N_HOST: ${N8N_HOST}
      N8N_PROTOCOL: https
      WEBHOOK_URL: https://${N8N_HOST}/
      N8N_ENCRYPTION_KEY: ${N8N_ENCRYPTION_KEY}
      GENERIC_TIMEZONE: ${TIMEZONE}
      TZ: ${TIMEZONE}
    volumes:
      - n8n_data:/home/node/.n8n
    depends_on:
      postgres:
        condition: service_healthy

  caddy:
    image: caddy:2
    restart: unless-stopped
    command: caddy reverse-proxy --from ${N8N_HOST} --to n8n:5678
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - caddy_data:/data
    depends_on:
      - n8n

volumes:
  postgres_data:
  n8n_data:
  caddy_data:

Open the web ports in the firewall

Only 80 and 443 are needed. SSH should already be allowed from the first-hour setup.

On the serverbash
sudo ufw allow 80,443/tcp
sudo ufw status

Start it

The first start downloads the images and takes a while. When docker compose ps shows all three services as running, the n8n log should end with a line saying the editor is accessible.

On the server · ~/n8nbash
cd ~/n8n
sudo docker compose up -d
sudo docker compose ps
sudo docker compose logs --tail 50 n8n

Open it and create the owner account

Open https://n8n.example.com in a browser. n8n asks you to create the owner account on first visit: do it at once, because until then anyone who finds the address could. From your computer you can also check the answer of the proxy.

On your computerbash
curl -I https://n8n.example.com

Check what listens

You should see SSH, 80 and 443, and nothing for 5678 or 5432. Docker publishes ports around ufw, so this list is the reliable check.

On the serverbash
sudo ss -ltnp

Back up the database

This writes a dump of the n8n database to a file. Copy it off the server together with the encryption key from step 2; one without the other does not bring your credentials back.

On the server · ~/n8nbash
cd ~/n8n
sudo docker compose exec -T postgres pg_dump -U n8n n8n > n8n-backup.sql
ls -lh n8n-backup.sql
Warning

A copy on the same disk does not protect you from losing the server.

Update

Make a backup first and read the n8n release notes for breaking changes. Then pull the new images and recreate the containers; your data stays in the volumes.

On the server · ~/n8nbash
cd ~/n8n
sudo docker compose pull
sudo docker compose up -d
sudo docker compose ps

Settings used in this guide

VariableExampleWhat it does
N8N_HOSTn8n.example.comThe public name n8n is reached at.
N8N_PROTOCOLhttpsTells n8n that visitors arrive over HTTPS through the proxy.
WEBHOOK_URLhttps://n8n.example.com/The address n8n shows for webhook nodes. A wrong value gives wrong webhook links.
GENERIC_TIMEZONEEtc/UTCThe time zone for schedule triggers.
N8N_ENCRYPTION_KEY64 hex charactersEncrypts saved credentials. Keep it; never change it on a running setup.
POSTGRES_PASSWORD48 hex charactersThe database password. The commands above generate it into .env.

If something goes wrong

The browser shows a certificate error

The DNS record is missing or has not spread yet, or port 80 is blocked. Read the Caddy log with sudo docker compose logs caddy; it names the reason.

n8n restarts again and again

Read sudo docker compose logs n8n. A wrong database password usually shows up as a connection error; if you changed POSTGRES_PASSWORD after the first start, the database keeps the old one.

Webhook addresses show the wrong host

WEBHOOK_URL must be the public https address with a slash at the end, built from N8N_HOST.

The server feels slow or containers are killed

Look at memory with sudo docker stats --no-stream and choose a larger plan if it is full.

Questions people ask

Is self-hosted n8n free?

The Community Edition can be self-hosted without a licence fee under n8n’s own licence. You pay for the server it runs on. Read the licence terms on the n8n site before you use it for a commercial service.

Do I need a domain name?

For anything beyond a quick test, yes. Webhooks from other services and sign-in with OAuth both need an HTTPS address, and a certificate is issued for a name, not for a bare IP address.

Can I move n8n to a bigger server later?

Yes. Copy your backup and the files .env and compose.yaml to the new server, restore the volumes, point the DNS record at the new address and start the containers with docker compose up -d.

Where to go next

Set up a regular copy of the database dump to a place off the server, and put a reminder in your calendar to update. The Docker guide covers the engine settings this setup relies on. Links are at the end of this page.

About the author

Denys Dunaievskyi

Founder, Cloudvix

Runs Cloudvix and is responsible for the guides published here.

Related guides

Your server is a few clicks away.

Choose the plan and the system in one order form; the location too, when there is more than one.

Create server