# n8n — self-hosting guide (Docker Compose + reverse proxy)

> Visual workflow automation with many integrations and AI agents; a self-hosted Zapier alternative.

- Page: https://deploy.cc/en/apps/n8n
- Category: AI & automation
- Server: No official minimum; our generic suggestion: 2 vCPU / 2 GB RAM and up (not an official figure)
- Ports: 5678 (Web)
- Install: `curl -fsSL https://deploy.cc/i/n8n.sh -o n8n.sh && sudo bash n8n.sh --domain example.com` (sha256 b58d14c034721f78d68784dfa19ca66dd5d19ff12a0843aa513ffaf9047efa4f)
- Repository: https://github.com/n8n-io/n8n
- Docs: https://docs.n8n.io/hosting/
- Deploy source: https://github.com/n8n-io/n8n-hosting/blob/main/docker-compose/withPostgres/docker-compose.yml
- Verification: config-checked (compose config passes, images exist; not started)
- Last verified: 2026-11-13

## docker-compose.yml (/opt/n8n)

```yaml
# n8n — docker-compose.yml (deploy.cc)
# 官方来源 / Official source: https://github.com/n8n-io/n8n-hosting/blob/main/docker-compose/withPostgres/docker-compose.yml
# 核对日期 / Checked: 2026-11-13
# 改动: 去掉 init-data.sh 与 n8n-runner，数据库直接用 POSTGRES_USER
# 改动: 版本固定在 .env 的 N8N_VERSION
# 改动: 端口只监听 127.0.0.1
services:
  postgres:
    image: postgres:18
    restart: always
    environment:
      - POSTGRES_USER=n8n
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD:?set POSTGRES_PASSWORD in .env}
      - POSTGRES_DB=n8n
      - PGDATA=/var/lib/postgresql/data
    volumes:
      - ./db:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -h localhost -U n8n -d n8n"]
      interval: 5s
      timeout: 5s
      retries: 10

  n8n:
    image: docker.n8n.io/n8nio/n8n:${N8N_VERSION:-stable}
    restart: always
    environment:
      - DB_TYPE=postgresdb
      - DB_POSTGRESDB_HOST=postgres
      - DB_POSTGRESDB_PORT=5432
      - DB_POSTGRESDB_DATABASE=n8n
      - DB_POSTGRESDB_USER=n8n
      - DB_POSTGRESDB_PASSWORD=${POSTGRES_PASSWORD:?set POSTGRES_PASSWORD in .env}
      - WEBHOOK_URL=${WEBHOOK_URL:-}
      - GENERIC_TIMEZONE=${TZ:-Asia/Shanghai}
    ports:
      - "127.0.0.1:5678:5678"
    volumes:
      - ./n8n:/home/node/.n8n
    depends_on:
      postgres:
        condition: service_healthy
```

## .env

```bash
# n8n — deploy.cc .env 模板 / template
# __GEN_*__ 由一键脚本替换为随机值；手动部署请自己改掉。
TZ=Asia/Shanghai
# PostgreSQL 密码（自动生成） / PostgreSQL password (generated)
POSTGRES_PASSWORD=__GEN_PW__
# n8n 镜像标签 / n8n image tag
N8N_VERSION=stable
# 对外地址（Webhook 用） / Public URL for webhooks
WEBHOOK_URL=__URL__/
```

## Notes

- Behind a reverse proxy set WEBHOOK_URL.
- Simplified from the official example: no init-data.sh and no external task-runner container.

## Nginx

```nginx
# n8n 反向代理 / reverse proxy — generated by deploy.cc
# 把 example.com 换成你的域名，证书路径按实际修改（certbot / acme.sh 签发）。
# Replace example.com and the certificate paths. More options: https://nginx.cc/zh/gen
# 放到 /etc/nginx/conf.d/<name>.conf，然后 nginx -t && systemctl reload nginx

map $http_upgrade $connection_upgrade_n8n {
    default upgrade;
    ''      close;
}

server {
    listen 80;
    listen [::]:80;
    server_name example.com;

    location /.well-known/acme-challenge/ {
        root /var/www/html;
    }

    location / {
        return 301 https://$host$request_uri;
    }
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    http2 on;  # nginx < 1.25.1: 删除此行，改为 listen 443 ssl http2;
    server_name example.com;

    ssl_certificate     /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_protocols TLSv1.2 TLSv1.3;

    client_max_body_size 50m;

    location / {
        proxy_pass http://127.0.0.1:5678;
        proxy_http_version 1.1;
        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_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade_n8n;
        proxy_read_timeout 3600s;
        proxy_buffering off;  # 流式输出 / streaming responses
    }
}
```

## Caddy

```caddy
# n8n — Caddyfile (deploy.cc). Caddy 会自动申请 HTTPS 证书 / automatic HTTPS
example.com {
    reverse_proxy 127.0.0.1:5678
}
```

## Update

```bash
cd /opt/n8n && docker compose pull && docker compose up -d && docker image prune -f
```

## Backup

```bash
cd /opt/n8n && docker compose stop && tar czf /root/n8n-$(date +%F).tar.gz -C /opt n8n && docker compose start
```

## FAQ

### What is n8n?

Visual workflow automation with many integrations and AI agents; a self-hosted Zapier alternative. Repository: https://github.com/n8n-io/n8n

### What server do I need for n8n?

No official minimum; our generic suggestion: 2 vCPU / 2 GB RAM and up (not an official figure)

### Which ports does n8n use?

5678 (Web). The web port binds to 127.0.0.1 only; expose it through Nginx or Caddy with HTTPS.

### How do I install n8n with one command?

As root on a Linux server: curl -fsSL https://deploy.cc/i/n8n.sh -o n8n.sh && sudo bash n8n.sh --domain example.com. It installs Docker via the official get.docker.com if missing, writes files to /opt/n8n with random secrets and runs docker compose up -d; it stops instead of overwriting when the directory exists or a port is taken.

### Where is n8n's data and how do I back it up?

All data and .env live in /opt/n8n (bind mounts). Back up with: cd /opt/n8n && docker compose stop && tar czf /root/n8n-$(date +%F).tar.gz -C /opt n8n && docker compose start

### How do I update n8n?

cd /opt/n8n && docker compose pull && docker compose up -d && docker image prune -f. Back up and read the release notes before major upgrades.

### How does this compose differ from the official example?

Source: https://github.com/n8n-io/n8n-hosting/blob/main/docker-compose/withPostgres/docker-compose.yml. Our changes are listed (in Chinese) in the file header; typically ports bound to 127.0.0.1, named volumes replaced by bind mounts and example secrets moved to .env.

---
deploy.cc (unofficial; trademarks belong to their owners) · v0.2.2
