Setup n8n with Docker on Cloud

🔹 Install & Secure n8n with PostgreSQL + Nginx + HTTPS

🚀 Prerequisites

✅ A cloud server (Ubuntu 24.04)

✅ A domain (yourdomain.com) pointing to your server

✅ Docker & Docker Compose installed

1️⃣ Set Up Docker & Docker Compose

Install Docker & Docker Compose:

curl -fsSL https://get.docker.com | sudo bash
sudo apt install docker-compose -y

Verify installation:

docker --version
docker-compose --version

2️⃣ Create a Directory for n8n

mkdir -p ~/n8n && cd ~/n8n

Create a docker-compose.yml file:

nano docker-compose.yml

Paste the following:

version: "3"

services:
  postgres:
    image: postgres:15
    container_name: n8n_postgres
    restart: always
    environment:
      POSTGRES_USER: n8nuser
      POSTGRES_PASSWORD: strongpassword
      POSTGRES_DB: n8ndb
    volumes:
      - postgres_data:/var/lib/postgresql/data

  n8n:
    image: n8nio/n8n
    container_name: n8n
    restart: always
    depends_on:
      - postgres
    ports:
      - "5678:5678"
    environment:
      - DB_TYPE=postgresdb
      - DB_POSTGRESDB_HOST=postgres
      - DB_POSTGRESDB_PORT=5432
      - DB_POSTGRESDB_DATABASE=n8ndb
      - DB_POSTGRESDB_USER=n8nuser
      - DB_POSTGRESDB_PASSWORD=strongpassword
      - N8N_BASIC_AUTH_ACTIVE=true
      - N8N_BASIC_AUTH_USER=myadmin
      - N8N_BASIC_AUTH_PASSWORD=secureadminpassword
      - N8N_PROTOCOL=https
      - N8N_HOST=n8n.yourdomain.com
      - WEBHOOK_URL=https://n8n.yourdomain.com/
    volumes:
      - n8n_data:/home/node/.n8n

volumes:
  n8n_data:
  postgres_data:

Save and exit (CTRL + X, then Y, then Enter).


3️⃣ Start n8n & PostgreSQL

Run:

docker-compose up -d

Check if everything is running:

docker ps

4️⃣ Configure Nginx Reverse Proxy

Install Nginx:

sudo apt update && sudo apt install nginx -y

Create an Nginx config file:

sudo nano /etc/nginx/sites-available/n8n

Paste the following:

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

    location / {
        proxy_pass http://localhost:5678;
        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 https;
    }

    client_max_body_size 50M;
}

Save and exit.

Enable the configuration:

sudo ln -s /etc/nginx/sites-available/n8n /etc/nginx/sites-enabled/

Test and restart Nginx:

sudo nginx -t
sudo systemctl restart nginx

5️⃣ Secure with SSL (Let’s Encrypt)

Install Certbot:

sudo apt install certbot python3-certbot-nginx -y

Obtain an SSL certificate:

sudo certbot --nginx -d n8n.yourdomain.com

Follow the prompts and choose redirect HTTP to HTTPS.

Verify auto-renewal:

sudo certbot renew --dry-run

6️⃣ Restart n8n for HTTPS

docker-compose down && docker-compose up -d

🎉 7️⃣ Access n8n

Go to:

🔗 https://n8n.yourdomain.com

Log in with:

Username: myadmin

Password: secureadminpassword


✅ Next Steps

Enhance Security: Use Cloudflare proxy.

Set Up Backups: Automate PostgreSQL dumps.

Scale: Deploy n8n workers for larger workflows.

Let me know if you need further tweaks! 🚀

Leave a Reply

Your email address will not be published. Required fields are marked *