If you have ever wondered how to do a clean, secure, and production-ready setup for light-to-medium workloads of self-hosted n8n on a Debian server using Docker Compose, PostgreSQL (much more recommended than SQLite if you plan to scale), Nginx as a reverse proxy, and free HTTPS certificates from Let’s Encrypt, this guide is for you.
The focus is to give you a solid path based on best practices: data persistence, working public webhooks, task runner support (to execute code more efficiently and securely), and mandatory HTTPS.
Prerequisites
Before getting started, make sure you have the following:
- A VPS running Debian 12 (Bookworm) or higher: stable versions with solid support from both Docker and n8n.
- Root access or sudo privileges: you will need them to install packages, configure the firewall, and manage services.
- A domain pointing to the server IP (A record): essential for external webhooks to work and for proper HTTPS.
- Recommended: at least 4 GB of RAM and 2 vCPUs: n8n is lightweight at first, but with multiple active workflows and task runners, memory usage climbs quickly.
1. Server Preparation
We will update the system, harden it slightly, and get it ready for production. First, update everything:
sudo apt update && sudo apt upgrade -y
This refreshes package lists and applies pending security patches. Always do this before installing new software. Next, install a few basic utilities we will need:
sudo apt install -y curl git ufw apt-transport-https ca-certificates gnupg lsb-release
This gives you the tools needed to download files securely, configure the firewall, and add external repositories.
Important security tip: Avoid working as root all the time. Create a standard user:
adduser n8nuser
usermod -aG sudo n8nuser
Choose a strong password. Then switch to that user:
From now on, we work as n8nuser (using sudo when necessary). This is much safer.
Configure a basic firewall with ufw (only opening strictly necessary ports):
sudo ufw allow OpenSSH
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw --force enable
This blocks everything else. SSH, HTTP, and HTTPS are the only ports exposed to the Internet.
2. Install Docker and Docker Compose
Docker allows us to run n8n and PostgreSQL in an isolated, reproducible, and easily updatable environment. Add the official Docker repository (the recommended way to ensure up-to-date versions):
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
Update package lists and install:
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
Note: Docker Compose is now provided as a plugin (docker compose, without a hyphen). You can leave behind the old Python-based docker-compose.
Verify that everything was installed correctly:
docker --versiondocker compose version
If the versions display properly, we are good to continue.
3. Create the Directory Structure and docker-compose.yml
We will organize everything in a dedicated directory to simplify backups, migrations, or cloning configuration:
mkdir -p ~/n8n && cd ~/n8n
mkdir data postgres-data
The data directory will store configurations, credentials, and files generated by n8n.
docker-compose.yml
Here we define our services: PostgreSQL (the persistent database recommended for production), n8n, and the task runners (to run workflows more efficiently and in a decoupled manner).
Key details to keep in mind:
- Internal networks: containers communicate with each other without exposing ports externally.
- Volumes: ensure data persists across restarts, updates, or container recreations.
N8N_*environment variables: control the public URL, credential encryption, execution mode, and more.
Very important: replace all default passwords and secrets with long, unique, and secure values. You can generate a solid encryption key with:
openssl rand -base64 32
Crucial: keep this key safe; you will need it if you ever have to migrate or recover your data.
Create a .env file (essential for keeping credentials out of your yml):
# .env - ¡NO lo subas a git ni lo compartas!
DOMAIN=app.tudominio.com # Cambia esto
POSTGRES_PASSWORD=tu_contraseña_larga_y_segura
N8N_ENCRYPTION_KEY=$(openssl rand -base64 32) # Genera una clave fuerte
N8N_RUNNERS_AUTH_TOKEN=$(openssl rand -base64 48) # Token secreto para runners
Now configure docker-compose.yml (copy and paste this file into ~/n8n/docker-compose.yml):
services:
postgres:
image: postgres:16
restart: always
environment:
POSTGRES_USER: n8n_user
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: n8n_db
volumes:
- ./postgres-data:/var/lib/postgresql/data
networks:
- n8n_net
n8n:
image: n8nio/n8n:latest
restart: always
environment:
- DB_TYPE=postgresdb
- DB_POSTGRESDB_HOST=postgres
- DB_POSTGRESDB_PORT=5432
- DB_POSTGRESDB_DATABASE=n8n_db
- DB_POSTGRESDB_USER=n8n_user
- DB_POSTGRESDB_PASSWORD=${POSTGRES_PASSWORD}
- N8N_PROTOCOL=https
- N8N_HOST=${DOMAIN}
- WEBHOOK_URL=https://${DOMAIN}/
- N8N_ENCRYPTION_KEY=${N8N_ENCRYPTION_KEY}
- N8N_ENFORCE_SETTINGS_FILE_PERMISSIONS=true
# Task Runners – recomendado para producción (ejecuta código de Code nodes de forma aislada y eficiente)
- N8N_RUNNERS_ENABLED=true
- N8N_RUNNERS_MODE=external
- N8N_RUNNERS_BROKER_LISTEN_ADDRESS=0.0.0.0
- N8N_RUNNERS_AUTH_TOKEN=${N8N_RUNNERS_AUTH_TOKEN}
# Descomenta si usas Code nodes en Python nativo:
# - N8N_NATIVE_PYTHON_RUNNER=true
volumes:
- ./data:/home/node/.n8n
depends_on:
- postgres
networks:
- n8n_net
task-runners:
image: n8nio/runners:latest
restart: always
environment:
- N8N_RUNNERS_TASK_BROKER_URI=http://n8n:5679
- N8N_RUNNERS_AUTH_TOKEN=${N8N_RUNNERS_AUTH_TOKEN}
- N8N_RUNNERS_AUTO_SHUTDOWN_TIMEOUT=15
# Descomenta si necesitas módulos Python stdlib completos (con precaución):
# - N8N_RUNNERS_STDLIB_ALLOW=*
depends_on:
- n8n
networks:
- n8n_net
networks:
n8n_net:
driver: bridge
Why these variables?
- WEBHOOK_URL: informs n8n of its actual public URL (mandatory behind a proxy/Nginx so webhooks work externally and render properly in the workflow editor).
- N8N_ENCRYPTION_KEY: encrypts stored credentials (without this, sensitive data remains in plain text).
- Task runners in external mode: isolate code execution (Code nodes, AI agents, etc.) from the main n8n process. This is safer and scales better. The auth token prevents unauthorized external runners from connecting.
4. Start n8n for the First Time
Everything is set up. Bring up the services:
docker compose up -d
Inspect the real-time logs to make sure there are no critical errors:
docker compose logs -f n8n
What should you look for to confirm a successful launch?
- The n8n web server started properly.
- The task broker is listening.
- The task runners connected without issues.
If anything fails (such as Postgres credentials or port conflicts), the logs will point it out clearly.
5. Configure Nginx as a Reverse Proxy
We do not expose n8n’s port 5678 directly to the Internet. We use Nginx as an intermediary to handle HTTPS and preserve real client IP addresses.
Install Nginx:
sudo apt install -y nginx
Create the configuration file at /etc/nginx/sites-available/n8n.conf (or whichever name you prefer):
server {
listen 80;
server_name ${DOMAIN}; # Sustituye por tu dominio real o usa $DOMAIN si lo defines como variable
# Redirige todo HTTP → HTTPS
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name ${DOMAIN};
# Proxy a n8n (puerto interno)
location / {
proxy_pass http://127.0.0.1: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 $scheme;
# Soporte WebSocket (imprescindible para el editor en tiempo real)
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
Enable the site and test the configuration:
sudo ln -s /etc/nginx/sites-available/n8n.conf /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
At this point, n8n should be reachable via your domain over plain HTTP.
6. Configure HTTPS with Let’s Encrypt
Now we encrypt all traffic using a free, trusted SSL certificate.
Install Certbot:
sudo apt install -y certbot python3-certbot-nginx
Obtain the certificate (replace with your actual domain):
sudo certbot --nginx -d app.tudominio.com
Certbot will:
- Verify that the domain points to your server.
- Automatically update the Nginx configuration to enforce HTTPS.
- Set up automatic certificate renewals (via a systemd timer).
Done! All traffic is now secured with HTTPS.
7. Final Checks and Practical Recommendations
We are almost ready to roll. Here are a few things to verify:
- Open https://app.yourdomain.com for the first time — the first user created will become the owner (administrator with full permissions).
- Create a simple webhook and trigger it externally (using Postman, curl, etc.) to verify that the public URL works.
- Ensure port 5678 is not exposed directly to the Internet (
sudo ss -tuln | grep 5678should only display127.0.0.1).
Final best practices (do not skip these):
- Never expose port 5678 directly.
- Check logs after any configuration change or update (
docker compose logs -f). - Perform regular backups: the
datavolume, the PostgreSQL database (pg_dump), and the .env file containing your secret keys. - Update containers every 1–2 months in a controlled fashion:
docker compose pull && docker compose up -d. - If your workloads grow significantly, consider enabling full queue mode with Redis (though that falls into a more advanced architecture tier).
Outcome: you now have a secure, moderately scalable n8n deployment ready for production workloads.
If you want to dive deeper, the official documentation is comprehensive: https://docs.n8n.io/hosting/
That covers it. You now have a solid, secure n8n instance ready for serious tasks.
Hopefully, this guide saves you time and prevents common pitfalls. If you deploy or adapt it, I’d love to hear how it goes. Happy sysadmining!