How to Install n8n with Docker: Complete VPS Setup Guide
TUTORIAL 15 min read fordnox

How to Install n8n with Docker: Complete VPS Setup Guide

Step-by-step guide to self-hosting n8n on a VPS with Docker. Includes SSL setup, PostgreSQL database, automatic backups, and production-ready configuration.


How to Install n8n with Docker: Complete VPS Setup Guide

Step-by-step guide to self-hosting n8n on a VPS with Docker. This production-ready setup includes SSL certificates, PostgreSQL database, Nginx reverse proxy, and automatic backups.

What You'll Set Up

Time required: 15-20 minutes

Prerequisites

Step 1: Initial Server Setup

SSH into your VPS:

ssh root@your-server-ip

Update the system:

apt update && apt upgrade -y

Step 2: Install Docker

# Install Docker
curl -fsSL https://get.docker.com | sh

# Install Docker Compose
apt install docker-compose-plugin -y

# Verify installation
docker --version
docker compose version

Step 3: Create Project Structure

mkdir -p /opt/n8n
cd /opt/n8n

Create the Docker Compose file:

nano docker-compose.yml

Paste:

version: '3.8'

services:
  n8n:
    image: n8nio/n8n:latest
    restart: always
    ports:
      - "5678:5678"
    environment:
      - N8N_HOST=n8n.yourdomain.com
      - N8N_PORT=5678
      - N8N_PROTOCOL=https
      - NODE_ENV=production
      - WEBHOOK_URL=https://n8n.yourdomain.com/
      - GENERIC_TIMEZONE=Europe/London
      # Database
      - DB_TYPE=postgresdb
      - DB_POSTGRESDB_HOST=postgres
      - DB_POSTGRESDB_PORT=5432
      - DB_POSTGRESDB_DATABASE=n8n
      - DB_POSTGRESDB_USER=n8n
      - DB_POSTGRESDB_PASSWORD=your-secure-password
      # Encryption
      - N8N_ENCRYPTION_KEY=your-32-char-encryption-key
    volumes:
      - n8n_data:/home/node/.n8n
    depends_on:
      - postgres
    networks:
      - n8n-network

  postgres:
    image: postgres:15
    restart: always
    environment:
      - POSTGRES_USER=n8n
      - POSTGRES_PASSWORD=your-secure-password
      - POSTGRES_DB=n8n
    volumes:
      - postgres_data:/var/lib/postgresql/data
    networks:
      - n8n-network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U n8n"]
      interval: 5s
      timeout: 5s
      retries: 5

volumes:
  n8n_data:
  postgres_data:

networks:
  n8n-network:

Important: Replace:

Generate an encryption key:

openssl rand -hex 16

Step 4: Install Nginx

apt install nginx -y

Create the Nginx config:

nano /etc/nginx/sites-available/n8n

Paste:

server {
    listen 80;
    server_name n8n.yourdomain.com;

    location / {
        proxy_pass http://localhost: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-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_read_timeout 86400;
        proxy_buffering off;
    }
}

Enable the site:

ln -s /etc/nginx/sites-available/n8n /etc/nginx/sites-enabled/
nginx -t
systemctl reload nginx

Step 5: Set Up SSL with Let's Encrypt

# Install Certbot
apt install certbot python3-certbot-nginx -y

# Get SSL certificate
certbot --nginx -d n8n.yourdomain.com

Follow the prompts. Certbot will automatically configure Nginx for HTTPS.

Step 6: Start n8n

cd /opt/n8n
docker compose up -d

Check if it's running:

docker compose ps
docker compose logs -f n8n

Access n8n at: https://n8n.yourdomain.com

Step 7: Configure Firewall

ufw allow ssh
ufw allow 'Nginx Full'
ufw enable

Step 8: Set Up Automatic Backups

Create backup script:

nano /opt/n8n/backup.sh

Paste:

#!/bin/bash
BACKUP_DIR="/opt/n8n/backups"
DATE=$(date +%Y%m%d_%H%M%S)

mkdir -p $BACKUP_DIR

# Backup PostgreSQL
docker compose exec -T postgres pg_dump -U n8n n8n > $BACKUP_DIR/db_$DATE.sql

# Backup n8n data
docker compose exec -T n8n n8n export:workflow --all --output=/home/node/.n8n/backups/workflows_$DATE.json 2>/dev/null || true

# Keep only last 7 days
find $BACKUP_DIR -type f -mtime +7 -delete

echo "Backup completed: $DATE"

Make it executable and schedule:

chmod +x /opt/n8n/backup.sh

# Add to crontab (daily at 3 AM)
crontab -e

Add:

0 3 * * * /opt/n8n/backup.sh >> /var/log/n8n-backup.log 2>&1

Step 9: Enable Auto-Start on Boot

Docker containers with restart: always will auto-start. Verify Docker is enabled:

systemctl enable docker

Updating n8n

To update to the latest version:

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

Troubleshooting

n8n won't start

docker compose logs n8n

Check for database connection errors or missing environment variables.

SSL certificate issues

certbot renew --dry-run

Database connection failed

Ensure PostgreSQL is healthy:

docker compose exec postgres pg_isready -U n8n

Out of memory

Upgrade your VPS or optimize n8n:

# In docker-compose.yml, add under n8n service:
deploy:
  resources:
    limits:
      memory: 2G

Performance Optimization

1. Execution Pruning

Automatically delete old executions to save space. In n8n settings:

2. Increase Workers

For heavy workloads:

environment:
  - EXECUTIONS_MODE=queue
  - EXECUTIONS_PROCESS=own

3. Use Redis for Queue Mode

Add Redis service for high-volume workflows:

redis:
  image: redis:alpine
  restart: always
  networks:
    - n8n-network

Recommended VPS Specs

Workload RAM CPU VPS
Light 2GB 1 vCPU Hostinger KVM 1
Medium 4GB 2 vCPU Hostinger KVM 2
Heavy 8GB 4 vCPU Contabo VPS S

Conclusion

You now have a production-ready n8n installation with:

✅ PostgreSQL database for reliability
✅ Nginx reverse proxy for performance
✅ SSL encryption for security
✅ Automatic backups for peace of mind
✅ Auto-restart for 24/7 availability

Your self-hosted n8n is ready for unlimited automations!

~/n8n-docker-setup/get-started

Ready to get started?

Get the best VPS hosting deal today. Hostinger offers 4GB RAM VPS starting at just $4.99/mo.

Get Hostinger VPS — $4.99/mo

// up to 75% off + free domain included

// related topics

n8n docker setup self-host n8n n8n vps installation n8n ssl setup n8n postgresql

fordnox

Expert VPS reviews and hosting guides. We test every provider we recommend.

// last updated: February 3, 2026. Disclosure: This article may contain affiliate links.