Logo

n8n Installation Guide on Debian VPS

n8n Installation Guide on Debian VPS

Complete guide to install n8n on Debian VPS. This detailed tutorial covers Node.js installation, n8n configuration with systemd, Nginx reverse proxy setup, SSL/TLS security with Let's Encrypt, and PostgreSQL for production use. Also includes automatic backup configuration, performance optimization, monitoring with journalctl, and advanced security measures like Fail2ban. Perfect for deploying a professional workflow automation platform with full control, security, and scalability.

n8n Installation Guide on Debian VPS

Introduction

n8n is an open-source workflow automation platform that allows you to connect different services and applications. This guide walks you through the complete installation of n8n on a Debian VPS with a secure and professional configuration.

Prerequisites

Before starting the installation, ensure your VPS has:

  • Debian 11 (Bullseye) or Debian 12 (Bookworm)
  • At least 2 GB of RAM (4 GB recommended)
  • 20 GB of available disk space
  • Root or sudo access
  • A domain name pointing to your VPS (recommended for HTTPS)
  • Stable Internet connection

Step 1: System Update

Connect to your VPS via SSH and update the system:

sudo apt update && sudo apt upgrade -y

Reboot the server if necessary:

sudo reboot

Step 2: Installing Node.js

n8n requires Node.js version 18.x or higher.

Installation via NodeSource

  1. Install the necessary dependencies:
sudo apt install -y curl wget gnupg2 ca-certificates lsb-release
  1. Add the NodeSource repository for Node.js 20.x:
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
  1. Install Node.js:
sudo apt install -y nodejs
  1. Verify the installation:
node --version
npm --version

You should see Node.js v20.x and npm 10.x or higher.

Step 3: Installing n8n with npm

Global installation

Install n8n globally with npm:

sudo npm install -g n8n

Verify installation

Check that n8n is installed correctly:

n8n --version

Step 4: System User Configuration

For security reasons, create a dedicated user for n8n:

sudo useradd -m -s /bin/bash n8n

Create the necessary directories:

sudo mkdir -p /home/n8n/.n8n
sudo chown -R n8n:n8n /home/n8n

Step 5: n8n Configuration

Create an environment configuration file:

sudo nano /home/n8n/.n8n/config

Add the following environment variables:

# Basic configuration
N8N_BASIC_AUTH_ACTIVE=true
N8N_BASIC_AUTH_USER=admin
N8N_BASIC_AUTH_PASSWORD=YourSecurePassword

# Domain configuration
N8N_HOST=your-domain.com
N8N_PORT=5678
N8N_PROTOCOL=https

# Editor configuration
N8N_EDITOR_BASE_URL=https://your-domain.com/

# Webhook configuration
WEBHOOK_URL=https://your-domain.com/

# Timezone
GENERIC_TIMEZONE=America/New_York

# Data path
N8N_USER_FOLDER=/home/n8n/.n8n

Adjust the values according to your needs. For a local test environment, you can use:

N8N_HOST=0.0.0.0
N8N_PORT=5678
N8N_PROTOCOL=http

Step 6: Creating the Systemd Service

Create a systemd service to manage n8n automatically:

sudo nano /etc/systemd/system/n8n.service

Add the following content:

[Unit]
Description=n8n - Workflow Automation
After=network.target

[Service]
Type=simple
User=n8n
EnvironmentFile=/home/n8n/.n8n/config
ExecStart=/usr/bin/n8n start
Restart=on-failure
RestartSec=10

[Install]
WantedBy=multi-user.target

Reload systemd and enable the service:

sudo systemctl daemon-reload
sudo systemctl enable n8n
sudo systemctl start n8n

Verify that the service is running:

sudo systemctl status n8n

Step 7: Installing and Configuring Nginx

Nginx will serve as a reverse proxy for n8n.

Installing Nginx

sudo apt install -y nginx

Virtual Host Configuration

Create a configuration file for n8n:

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

Add the following configuration:

server {
    listen 80;
    server_name your-domain.com;

    location / {
        proxy_pass http://localhost:5678;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        
        # Security headers
        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;
        
        # Timeouts for webhooks
        proxy_read_timeout 300;
        proxy_connect_timeout 300;
        proxy_send_timeout 300;
    }
}

Enable the site:

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

Test the configuration:

sudo nginx -t

Reload Nginx:

sudo systemctl reload nginx

Step 8: Installing Certbot for HTTPS

Secure your installation with a free SSL certificate from Let's Encrypt.

Installing Certbot

sudo apt install -y certbot python3-certbot-nginx

Obtaining the SSL Certificate

sudo certbot --nginx -d your-domain.com

Follow the on-screen instructions. Certbot will automatically configure Nginx to use HTTPS.

Automatic Renewal

Test automatic renewal:

sudo certbot renew --dry-run

Automatic renewal is configured by default via a systemd timer.

Step 9: Firewall Configuration

If you're using UFW (Uncomplicated Firewall):

sudo apt install -y ufw
sudo ufw allow 22/tcp
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable

Check the status:

sudo ufw status

Step 10: Accessing n8n

You can now access n8n via your browser:

https://your-domain.com

Log in with the credentials configured in the configuration file (N8N_BASIC_AUTH_USER and N8N_BASIC_AUTH_PASSWORD).

Step 11: Advanced Configuration

By default, n8n uses SQLite. For better performance in production, use PostgreSQL.

  1. Install PostgreSQL:
sudo apt install -y postgresql postgresql-contrib
  1. Create a database and user:
sudo -u postgres psql

In the PostgreSQL shell:

CREATE DATABASE n8n;
CREATE USER n8n_user WITH PASSWORD 'secure_password';
GRANT ALL PRIVILEGES ON DATABASE n8n TO n8n_user;
\q
  1. Add the database configuration in /home/n8n/.n8n/config:
DB_TYPE=postgresdb
DB_POSTGRESDB_HOST=localhost
DB_POSTGRESDB_PORT=5432
DB_POSTGRESDB_DATABASE=n8n
DB_POSTGRESDB_USER=n8n_user
DB_POSTGRESDB_PASSWORD=secure_password
  1. Restart n8n:
sudo systemctl restart n8n

Encryption Configuration

To secure stored credentials, define an encryption key:

N8N_ENCRYPTION_KEY=$(openssl rand -base64 32)
echo "N8N_ENCRYPTION_KEY=$N8N_ENCRYPTION_KEY" | sudo tee -a /home/n8n/.n8n/config

Email Configuration (SMTP)

To receive email notifications, add to /home/n8n/.n8n/config:

N8N_EMAIL_MODE=smtp
N8N_SMTP_HOST=smtp.gmail.com
N8N_SMTP_PORT=587
[email protected]
N8N_SMTP_PASS=your-app-password
[email protected]
N8N_SMTP_SSL=false

Step 12: Backup and Restore

Automatic Backup

Create a backup script:

sudo nano /usr/local/bin/backup-n8n.sh

Add the following content:

#!/bin/bash

BACKUP_DIR="/backup/n8n"
DATE=$(date +%Y%m%d_%H%M%S)
N8N_DIR="/home/n8n/.n8n"

mkdir -p $BACKUP_DIR

# Backup n8n data
tar -czf $BACKUP_DIR/n8n_backup_$DATE.tar.gz $N8N_DIR

# If PostgreSQL is used
if [ -n "$DB_POSTGRESDB_DATABASE" ]; then
    sudo -u postgres pg_dump n8n > $BACKUP_DIR/n8n_db_$DATE.sql
    gzip $BACKUP_DIR/n8n_db_$DATE.sql
fi

# Keep only the last 7 backups
find $BACKUP_DIR -name "n8n_backup_*.tar.gz" -mtime +7 -delete
find $BACKUP_DIR -name "n8n_db_*.sql.gz" -mtime +7 -delete

echo "Backup completed: $DATE"

Make the script executable:

sudo chmod +x /usr/local/bin/backup-n8n.sh

Add a cron job for daily backup:

sudo crontab -e

Add the following line (daily backup at 2 AM):

0 2 * * * /usr/local/bin/backup-n8n.sh

Restore

To restore a backup:

sudo systemctl stop n8n
sudo tar -xzf /backup/n8n/n8n_backup_YYYYMMDD_HHMMSS.tar.gz -C /
sudo chown -R n8n:n8n /home/n8n/.n8n

# If PostgreSQL
gunzip /backup/n8n/n8n_db_YYYYMMDD_HHMMSS.sql.gz
sudo -u postgres psql n8n < /backup/n8n/n8n_db_YYYYMMDD_HHMMSS.sql

sudo systemctl start n8n

Step 13: Updating n8n

To update n8n to the latest version:

sudo systemctl stop n8n
sudo npm update -g n8n
sudo systemctl start n8n

Check the new version:

n8n --version

Step 14: Monitoring and Logs

View logs in real-time

sudo journalctl -u n8n -f

View recent logs

sudo journalctl -u n8n -n 100 --no-pager

Date-specific logs

sudo journalctl -u n8n --since "2024-01-01" --until "2024-01-02"

Troubleshooting

n8n won't start

Check the logs:

sudo journalctl -u n8n -n 50

Verify that the port is not already in use:

sudo netstat -tulpn | grep 5678

Connection issues

Check that Nginx is running:

sudo systemctl status nginx

Test the Nginx configuration:

sudo nginx -t

Permission errors

Check the permissions of the n8n directory:

sudo chown -R n8n:n8n /home/n8n/.n8n

Memory issues

If n8n runs out of memory, add this line in /etc/systemd/system/n8n.service:

Environment="NODE_OPTIONS=--max-old-space-size=2048"

Then reload:

sudo systemctl daemon-reload
sudo systemctl restart n8n

Useful Commands

Managing the n8n service

# Start n8n
sudo systemctl start n8n

# Stop n8n
sudo systemctl stop n8n

# Restart n8n
sudo systemctl restart n8n

# View status
sudo systemctl status n8n

# Enable at startup
sudo systemctl enable n8n

# Disable at startup
sudo systemctl disable n8n

Managing Nginx

# Restart Nginx
sudo systemctl restart nginx

# Reload configuration
sudo systemctl reload nginx

# Test configuration
sudo nginx -t

Additional Security

Fail2ban for n8n

Install Fail2ban to protect against login attempts:

sudo apt install -y fail2ban

Create a filter for n8n:

sudo nano /etc/fail2ban/filter.d/n8n.conf

Add:

[Definition]
failregex = ^<HOST> .* "POST .*" 401
ignoreregex =

Create a jail:

sudo nano /etc/fail2ban/jail.d/n8n.conf

Add:

[n8n]
enabled = true
port = http,https
filter = n8n
logpath = /var/log/nginx/access.log
maxretry = 5
bantime = 3600

Restart Fail2ban:

sudo systemctl restart fail2ban

Connection limiting with Nginx

Add to your Nginx configuration:

limit_req_zone $binary_remote_addr zone=n8n_limit:10m rate=10r/s;

server {
    # ... existing configuration ...
    
    location / {
        limit_req zone=n8n_limit burst=20;
        # ... rest of configuration ...
    }
}

Performance Optimization

Increase file limits

Add to /etc/systemd/system/n8n.service:

LimitNOFILE=65536

Optimized Node.js configuration

Add to /home/n8n/.n8n/config:

N8N_CONCURRENCY_PRODUCTION_LIMIT=10
EXECUTIONS_DATA_PRUNE=true
EXECUTIONS_DATA_MAX_AGE=168

Additional Resources

Conclusion

Your n8n instance is now installed and securely configured on your Debian VPS. Don't forget to:

  • Make regular backups
  • Keep the system updated
  • Monitor the logs
  • Adjust settings according to your needs

Happy automating!

Join our Discord community server

For any questions, suggestions, or just to chat with the community, join us on Discord!

900+Members