New Detail

#
Writen by

admin

Guide to Installing n8n with Docker Compose and Nginx

I. Prerequisites

  1. Docker and Docker Compose installed on your machine

    • Docker: Tool for running containers.

    • Docker Compose: Tool for managing multiple containers using YAML.

    docker --version
    docker-compose --version

    – If you haven’t installed them yet, you can refer to the Docker installation guide [here].

  2. A computer or server with sufficient resources

    • Minimum RAM: 2GB

    • CPU: at least 2 cores

    • Disk space: 20GB+


II. Creating the Project

1. Create a directory to store the configuration files and n8n data:

mkdir n8n-docker
cd n8n-docker

2. Inside this directory, create a compose.yaml file to configure n8n.

nano compose.yaml

Add the following content:

services:
n8n:
image: docker.n8n.io/n8nio/n8n:1.85.0
restart: always
ports:
- "5678:5678"
environment:
- N8N_HOST=yourdomain.com/ # Replace with your domain
- N8N_PORT=5678
- N8N_PROTOCOL=https
- N8N_BASIC_AUTH_ACTIVE=true
- N8N_BASIC_AUTH_USER=admin # Replace with your username
- N8N_BASIC_AUTH_PASSWORD=PASSWORD123 # Replace with your password
- N8N_TRUSTED_PROXIES=nginx
- N8N_RUNNERS_ENABLED=true
- N8N_SECURE_COOKIE=true
- TZ=Asia/Ho_Chi_Minh
- DB_TYPE=postgresdb
- DB_POSTGRESDB_HOST=postgres
- DB_POSTGRESDB_PORT=5437
- DB_POSTGRESDB_DATABASE=database # Replace with your database name
- DB_POSTGRESDB_USER=POSTGRESTUSER # Replace with your Postgres user
- DB_POSTGRESDB_PASSWORD=PASSWORD123 # Replace with your password
- WEBHOOK_URL=https://yourdomain.com/ # Replace with your domain
volumes:
- ./n8n-data:/home/node/.n8n
depends_on:
- postgres
postgres:
image: postgres:latest
restart: always
ports:
“5432:5432”
environment:
POSTGRES_USER=POSTGRESTUSER # Replace with your Postgres user
POSTGRES_PASSWORD=PASSWORD123
POSTGRES_DB=database # Replace with your Postgres database name
TZ=Asia/Ho_Chi_Minh
volumes:
./postgres-data:/var/lib/postgresql/datanginx:
image: nginx:latest
restart: always
ports:
“80:80” # HTTP port
“443:443” # HTTPS port
volumes:
./nginx.conf:/etc/nginx/nginx.conf # Nginx configuration file
./certsn:/etc/nginx/certs # Directory containing SSL certificates
depends_on:
n8n
networks:
n8n-networknetworks:
n8n-network:
driver: bridge


3. Create the Nginx configuration file

In the n8n-docker directory, create the nginx.conf file:

nano nginx.conf

Add the following content:

user nginx;
worker_processes 2;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;events {
worker_connections 1024;
}http {
server {
listen 80;
server_name youdomain.com; # Replace with your domain

# Redirect all HTTP traffic to HTTPS
return 301 https://$host$request_uri;
}

server {
listen 443 ssl;
server_name youdomain.com; # Replace with your domain

ssl_certificate /etc/nginx/certs/fullchain.pem; # Path to SSL certificate
ssl_certificate_key /etc/nginx/certs/privkey.pem; # Path to private key

location / {
proxy_pass http://n8n:5678; # Forward requests to n8n
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade; # WebSocket support
proxy_set_header Connection “Upgrade”; # WebSocket support
proxy_set_header Host $host;
chunked_transfer_encoding off;
proxy_buffering off;
proxy_cache off;
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;
}
}
}


4. Run the project

After configuring both compose.yaml and nginx.conf, start the project with:

docker compose up -d

Then check if the containers are running successfully:

docker ps

If the output is similar to the example shown above, the project has started successfully.


III. Common Docker Management Commands

# Start Docker Compose
docker compose up -d
# Stop Docker Compose
docker compose down# Restart Docker Compose
docker compose restart# Check Docker Compose logs
docker-compose logs -f