Complete Guide to Self-Hosting n8n: Installation, Setup & Best Practices

Introduction
Ready to take control of your workflow automation with n8n? Self-hosting n8n gives you complete ownership of your data, unlimited scalability, and the freedom to customize your automation platform exactly how you need it. This comprehensive guide will walk you through everything you need to know to get n8n up and running on your own infrastructure.
Whether you're a developer looking to integrate n8n into your existing stack or a business owner wanting to maintain data sovereignty, this step-by-step tutorial will have you automating workflows in no time.
Prerequisites
Before we dive into the installation process, make sure you have:
- A server or VPS with at least 2GB RAM and 20GB storage
- Docker and Docker Compose installed (recommended method)
- Basic command line knowledge
- A domain name (optional but recommended for production)
- SSL certificate (for secure HTTPS access)
System Requirements
Minimum Requirements:
- CPU: 1 core
- RAM: 2GB
- Storage: 20GB
- OS: Ubuntu 20.04+, CentOS 8+, or any Docker-compatible Linux distribution
Recommended for Production:
- CPU: 2+ cores
- RAM: 4GB+
- Storage: 50GB+ SSD
- Backup strategy in place
Installation Method 1: Docker Compose (Recommended)
Docker Compose is the easiest and most reliable way to deploy n8n with all necessary dependencies.
Step 1: Install Docker and Docker Compose
On Ubuntu/Debian:
# Update package index
sudo apt update
# Install Docker
sudo apt install docker.io docker-compose
# Add your user to docker group
sudo usermod -aG docker $USER
# Log out and back in, or restart your session
On CentOS/RHEL:
# Install Docker
sudo yum install -y docker docker-compose
# Start and enable Docker
sudo systemctl start docker
sudo systemctl enable docker
# Add user to docker group
sudo usermod -aG docker $USER
Step 2: Create Project Directory
# Create project directory
mkdir n8n-self-hosted
cd n8n-self-hosted
# Create necessary directories
mkdir data
mkdir postgres-data
Step 3: Create Docker Compose Configuration
Create a docker-compose.yml file:
version: '3.8'
services:
postgres:
image: postgres:13
restart: unless-stopped
environment:
- POSTGRES_USER=n8n
- POSTGRES_PASSWORD=your_secure_password_here
- POSTGRES_DB=n8n
- POSTGRES_NON_ROOT_USER=n8n
- POSTGRES_NON_ROOT_PASSWORD=your_secure_password_here
volumes:
- ./postgres-data:/var/lib/postgresql/data
healthcheck:
test: ['CMD-SHELL', 'pg_isready -h localhost -U n8n -d n8n']
interval: 5s
timeout: 5s
retries: 10
n8n:
image: n8nio/n8n:latest
restart: unless-stopped
environment:
- N8N_BASIC_AUTH_ACTIVE=true
- N8N_BASIC_AUTH_USER=admin
- N8N_BASIC_AUTH_PASSWORD=your_admin_password_here
- N8N_HOST=your-domain.com
- N8N_PORT=5678
- N8N_PROTOCOL=https
- NODE_ENV=production
- WEBHOOK_URL=https://your-domain.com/
- GENERIC_TIMEZONE=UTC
- 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_here
ports:
- "5678:5678"
volumes:
- ./data:/home/node/.n8n
depends_on:
postgres:
condition: service_healthy
nginx:
image: nginx:alpine
restart: unless-stopped
ports:
- "80:80"
- "443:443"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf:ro
- ./ssl:/etc/nginx/ssl:ro
depends_on:
- n8n
Step 4: Configure Nginx (Optional but Recommended)
Create an nginx.conf file for reverse proxy and SSL termination:
events {
worker_connections 1024;
}
http {
upstream n8n {
server n8n:5678;
}
server {
listen 80;
server_name your-domain.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl http2;
server_name your-domain.com;
ssl_certificate /etc/nginx/ssl/cert.pem;
ssl_certificate_key /etc/nginx/ssl/key.pem;
client_max_body_size 50M;
location / {
proxy_pass http://n8n;
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;
# WebSocket support
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
}
Step 5: Start n8n
# Start all services
docker-compose up -d
# Check if services are running
docker-compose ps
# View logs
docker-compose logs -f n8n
Installation Method 2: Direct Installation with npm
For those who prefer a direct installation without Docker:
Step 1: Install Node.js
# Install Node.js 18.x
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt-get install -y nodejs
# Verify installation
node --version
npm --version
Step 2: Install n8n
# Install n8n globally
npm install n8n -g
# Create data directory
mkdir ~/.n8n
Step 3: Configure Environment
Create a .env file:
N8N_BASIC_AUTH_ACTIVE=true
N8N_BASIC_AUTH_USER=admin
N8N_BASIC_AUTH_PASSWORD=your_secure_password
N8N_HOST=your-domain.com
N8N_PORT=5678
N8N_PROTOCOL=https
WEBHOOK_URL=https://your-domain.com/
GENERIC_TIMEZONE=UTC
Step 4: Start n8n
# Start n8n
n8n start
# Or run with PM2 for production
npm install pm2 -g
pm2 start n8n
pm2 startup
pm2 save
Post-Installation Configuration
Security Hardening
1. Enable Authentication
Always enable basic authentication or set up proper user management in production environments.
2. Configure Firewall
# Allow only necessary ports
sudo ufw allow ssh
sudo ufw allow 80
sudo ufw allow 443
sudo ufw enable
3. Set Up SSL/TLS
Use Let's Encrypt for free SSL certificates:
# Install Certbot
sudo apt install certbot
# Get certificate
sudo certbot certonly --standalone -d your-domain.com
# Copy certificates to nginx ssl directory
sudo cp /etc/letsencrypt/live/your-domain.com/fullchain.pem ./ssl/cert.pem
sudo cp /etc/letsencrypt/live/your-domain.com/privkey.pem ./ssl/key.pem
Database Configuration
For production use, configure a proper database instead of the default SQLite:
- PostgreSQL (recommended for production)
- MySQL/MariaDB (alternative option)
- SQLite (development only)
Backup Strategy
Set up automated backups for your n8n data:
# Create backup script
#!/bin/bash
DATE=$(date +%Y%m%d_%H%M%S)
BACKUP_DIR="/path/to/backups"
# Backup n8n data
tar -czf $BACKUP_DIR/n8n_data_$DATE.tar.gz ./data
# Backup database
docker-compose exec postgres pg_dump -U n8n n8n > $BACKUP_DIR/n8n_db_$DATE.sql
# Keep only last 7 days of backups
find $BACKUP_DIR -name "n8n_*" -mtime +7 -delete
Accessing Your n8n Instance
Once installation is complete:
- Open your browser and navigate to
https://your-domain.com - Log in with your configured credentials
- Complete the initial setup wizard
- Start creating your first workflow!
Common Troubleshooting
Service Won't Start
# Check service status
docker-compose ps
docker-compose logs n8n
# Restart services
docker-compose restart
Database Connection Issues
# Check database connectivity
docker-compose exec postgres psql -U n8n -d n8n -c "SELECT version();"
Permission Issues
# Fix file permissions
sudo chown -R $USER:$USER ./data
sudo chmod -R 755 ./data
Performance Optimization
Resource Monitoring
Monitor your n8n instance performance:
- CPU Usage: Monitor for high CPU during workflow executions
- Memory: Watch RAM usage, especially with complex workflows
- Disk I/O: Ensure adequate storage for workflow data and logs
- Network: Monitor bandwidth for webhook-heavy workflows
Scaling Considerations
For high-volume usage:
- Consider using Redis for session storage
- Implement queue-based workflow execution
- Use dedicated database servers
- Set up load balancing for multiple n8n instances
Maintenance and Updates
Regular Updates
# Update n8n with Docker Compose
docker-compose pull
docker-compose down
docker-compose up -d
# Verify update
docker-compose logs n8n
Health Monitoring
Set up monitoring and alerting:
- Application uptime monitoring
- Database health checks
- Disk space monitoring
- SSL certificate expiration alerts
Next Steps
With your n8n instance up and running, you're ready to:
- Explore the workflow editor and create your first automation
- Connect your favorite apps and services
- Set up webhooks for real-time triggers
- Configure user management and permissions
- Join the n8n community for tips and workflow templates
Conclusion
Congratulations! You now have a fully functional self-hosted n8n instance. This setup gives you complete control over your automation platform while ensuring your data stays on your infrastructure. Remember to keep your instance updated, monitor its performance, and implement proper backup strategies.
Self-hosting n8n is just the beginning of your automation journey. With this foundation in place, you can build sophisticated workflows, integrate countless services, and automate processes that drive real business value—all while maintaining complete ownership and control of your automation platform.
Start small, experiment with simple workflows, and gradually build more complex automations as you become familiar with n8n's capabilities. The investment in self-hosting will pay dividends in flexibility, cost savings, and data sovereignty.