Unlock Powerful Automation with a Secure, Custom-Built n8n Instance
Discover the power of n8n for seamless automation. This guide provides a clear, step-by-step tutorial on how to self-host n8n on Ubuntu using Docker Compose with built-in security and OpenRouter AI integration.
Setting Up n8n with Docker Compose
Prerequisites: A clean Ubuntu 20.04+ server and sudo access.
Installation: Update and install Docker:
sudo apt update && sudo apt install -y docker.io docker-compose-plugin
Create directory and config:
mkdir n8n-setup && cd n8n-setup
nano docker-compose.yml
Paste this secure configuration with basic auth and OpenRouter:
services:
n8n:
image: n8nio/n8n
ports:
- "127.0.0.1:5678:5678"
volumes:
- n8n_data:/home/node/.n8n
restart: unless-stopped
environment:
- N8N_BLOCK_ENV_ACCESS_IN_NODE=false
- N8N_ENFORCE_SETTINGS_FILE_PERMISSIONS=false
- N8N_BASIC_AUTH_ACTIVE=true
- N8N_BASIC_AUTH_USER=admin
- N8N_BASIC_AUTH_PASSWORD=${N8N_PASSWORD}
- WEBHOOK_URL=https://n8n.texgo.it/
- N8N_PROXY_HOPS=1
- NODE_ENV=production
- OPENROUTER_API_KEY=${OPEN_API_KEY}
- OPENROUTER_BASE_URL=https://openrouter.ai/api/v1
- OPENROUTER_MODEL=x-ai/grok-code-fast-1
extra_hosts:
- "felipeferreira.net:51.178.46.243"
volumes:
n8n_data:
Save (Ctrl+O, Enter, Ctrl+X). Create .env file:
echo "N8N_PASSWORD=your_secure_password" > .env
sudo docker compose up -d
Verification: Check sudo docker ps (status Up). Access http://localhost:5678 (login: admin/your_secure_password).
Troubleshooting: Permission errors? Use sudo. Crashes? Check sudo docker logs n8n.
Configuring HAProxy for HTTPS Access
Secure external access with HAProxy reverse proxy and Let’s Encrypt.
1. Install and Get Certificate (stop n8n port 80 first if needed):
sudo apt update && sudo apt install haproxy certbot -y
sudo certbot certonly --standalone -d n8n.example.com
cat /etc/letsencrypt/live/n8n.example.com/fullchain.pem /etc/letsencrypt/live/n8n.example.com/privkey.pem | sudo tee /etc/haproxy/n8n.pem
2. Configure HAProxy Append to /etc/haproxy/haproxy.cfg:
listen n8n-https
bind *:443 ssl crt /etc/haproxy/n8n.pem
mode http
server n8n_app 127.0.0.1:5678 check
3. Restart and Verify
sudo systemctl restart haproxy
curl -I https://n8n.example.com
Tips: 503 error? Verify docker ps. SSL issues? Check PEM permissions.
Quick Setup: Download and run the automated setup.sh script: setup.sh (Co-authored by
References
