How to Install and Self-Host n8n on Ubuntu VPS (Step-by-Step Guide)

How to Install and Self-Host n8n on Ubuntu VPS (Step-by-Step Guide)

n8n is a powerful open-source tool designed to connect and automate tasks across various applications, enabling seamless data flow without manual intervention. It supports multiple integrations, from APIs and databases to cloud services, making it a flexible choice for developers, businesses, and automation enthusiasts.

By self-hosting n8n on your Ubuntu VPS, you gain complete control over your workflows, enhanced data security, and the ability to customize settings according to your needs—without relying on third-party cloud services. Running n8n on your server also eliminates subscription costs, making it a cost-effective solution for long-term automation.

In this step-by-step guide, we will walk you through the entire installation process using Docker, ensuring a smooth setup with best practices for stability, security, and scalability. Whether automating business processes, managing integrations, or building complex workflows, this guide will help you start with self-hosted n8n effortlessly.

Prerequisites

  • A VPS running Ubuntu 20.04 or later
  • A user with sudo privileges
  • A domain (optional but recommended)
  • Docker and Docker Compose installed

Step 1: Update and Install Required Packages

Update your package list and install necessary dependencies:

sudo apt update && sudo apt upgrade -y
sudo apt install -y curl ca-certificates gnupg

Step 2: Install Docker and Docker Compose

Install Docker

curl -fsSL https://get.docker.com | sh
sudo systemctl enable --now docker

Install Docker Compose

sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

Verify installation:

docker --version
docker-compose --version

Step 3: Create a Directory for n8n

mkdir -p ~/n8n && cd ~/n8n

Step 4: Set Up an Environment File

Create a .env File to store configurations:

nano .env

Add the following:

N8N_BASIC_AUTH_ACTIVE=true
N8N_BASIC_AUTH_USER=admin
N8N_BASIC_AUTH_PASSWORD=securepassword
N8N_HOST=your-domain.com
N8N_PORT=5678
WEBHOOK_URL=https://your-domain.com/

Replace your-domain.com with your actual domain.

Step 5: Create a Docker Compose File

nano docker-compose.yml

Add the following configuration:

version: '3.7'

services:
  n8n:
    image: n8nio/n8n
    restart: unless-stopped
    ports:
      - "5678:5678"
    environment:
      - N8N_BASIC_AUTH_ACTIVE=true
      - N8N_BASIC_AUTH_USER=${N8N_BASIC_AUTH_USER}
      - N8N_BASIC_AUTH_PASSWORD=${N8N_BASIC_AUTH_PASSWORD}
      - N8N_HOST=${N8N_HOST}
      - WEBHOOK_URL=${WEBHOOK_URL}
    volumes:
      - ~/.n8n:/home/node/.n8n

Step 6: Start n8n

docker-compose up -d

Check if the container is running:

docker ps

Step 7: Set Up a Reverse Proxy (Optional)

To secure n8n with SSL, use Nginx and Let’s Encrypt.

Install Nginx

sudo apt install nginx -y

Create an Nginx Configuration

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

Add the following:

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

    location / {
        proxy_pass http://localhost: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;
    }
}

Enable the configuration:

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

Enable SSL with Let’s Encrypt

Install Certbot:

sudo apt install certbot python3-certbot-nginx -y

Generate an SSL certificate:

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

Now, n8n is securely accessible at https://your-domain.com.

Step 8: Access n8n

Open your browser and visit:

https://your-domain.com

Log in using the credentials set in the .env file.

Conclusion

Congratulations! You have successfully installed and self-hosted n8n on your Ubuntu VPS. Now, you can create powerful automation workflows and integrate various applications seamlessly. 🚀

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *