← Back to blog

How to Self-Host n8n on Ubuntu: A Step-by-Step Guide

n8nupgradehow to

n8n is a powerful, extendable workflow automation tool that you can self-host for full control and customization. This guide will walk you through installing and configuring n8n on an Ubuntu server.

However, you can ignore these steps if you think it’s complicated and go straight to the paid cloud subscription by n8n where everything is setup and ready to use.

Prerequisites

Before starting, make sure you have:

  1. An Ubuntu 20.04 or newer server (VPS or local machine). I will use 24.04 as a demo
  2. Root or sudo access
  3. A domain name pointing to your server’s IP (e.g., n8n.example.com)
  4. Docker and Docker Compose installed

Step 1: Update Your System

sudo apt update && sudo apt upgrade -y

Step 2: Install Docker and Docker Compose

Skip this step if you have Docker installed

sudo apt install -y docker.io docker-compose

sudo systemctl enable docker

sudo systemctl start docker

Verify installation:

docker —version

docker-compose —version

Step 3: Create Docker Setup for n8n

Option A: Create Docker via Docker Compose

Create a directory for n8n:

mkdir ~/n8n && cd ~/n8n

Create a .env file:

nano .env

Add environment variables (edit for your domain):

N8N_HOST=n8n.example.com
N8N_PORT=5678
N8N_PROTOCOL=https
WEBHOOK_URL=https://n8n.example.com/

Create docker-compose.yml:

version: "3"
services:
n8n:
image: n8nio/n8n
restart: always
ports:
- "5678:5678"
environment:
- GENERIC_TIMEZONE=Australia/Sydney
- N8N_BASIC_AUTH_ACTIVE=true
- N8N_PUSH_BACKEND=websocket
- N8N_HOST=${N8N_HOST}
- N8N_PORT=${N8N_PORT}
- N8N_PROTOCOL=${N8N_PROTOCOL}
- WEBHOOK_URL=${WEBHOOK_URL}
volumes:
- ~/.n8n:/home/node/.n8n

Start n8n

docker-compose up -d

Option B: Create Docker Using Inline Expression

Run this command:

sudo docker run -d --name n8n -p 5678:5678 -v n8n_data:/home/node/.n8n -e WEBHOOK_URL="n8n.example.com" -e N8N_PUSH_BACKEND=websocket -e GENERIC_TIMEZONE="Australia/Melbourne" docker.n8n.io/n8nio/n8n

Step 4: Accessing your n8n to see if it works

You should now be able to access n8n at:

http://your-server-ip:5678 or https://n8n.example.com (once SSL is configured)

Step 5: Set Up (Apache)

Skip to Step 6 if you already have Apache installed

Install Apache:

sudo apt update
sudo apt install apache2 -y

Start Apache & Enable the Apache service to start automatically at boot time.:

sudo systemctl start apache2
sudo systemctl enable apache2

Check Apache Status:

sudo systemctl status apache2

Allow connections to the HTTP port 80 through the default firewall configuration:

sudo ufw allow 80/tcp

Step 6: Create a new Apache Virtual Host for n8n

Create a new Apache configuration file for n8n:

sudo nano /etc/apache2/sites-available/n8n.conf

Paste the configuration below into the file. This will be your reverse proxy for n8n and redirect user to n8n hosted from docker on port 5678 when they access https://n8n.example.com

<VirtualHost *:80>
ServerName n8n.example.com
ProxyPreserveHost On
ProxyRequests Off
# WebSocket proxy for n8n real-time push
ProxyPass "/rest/push" "ws://127.0.0.1:5678/rest/push"
ProxyPassReverse "/rest/push" "ws://127.0.0.1:5678/rest/push"
# Usual HTTP proxy for everything else
ProxyPass "/" "http://localhost:5678/"
ProxyPassReverse "/" "http://localhost:5678/"
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/n8n/(.*)$
RewriteRule ^/n8n/(.*)$ /$1 [L,P]
ErrorLog ${APACHE_LOG_DIR}/n8n-error.log
CustomLog ${APACHE_LOG_DIR}/n8n-access.log combined
</VirtualHost>

Disable the default Apache virtual host configuration:

sudo a2dissite 000-default

Enable the new Apache virtual host configuration:

sudo a2densite n8n

Test the Apache configuration for errors. It will tell you whether the syntax is ok or not

sudo apachectl configtest

Restart the Apache web server to apply your configuration changes:

sudo systemctl restart apache2

Step 7: Set Up SSL (Let’s Encrypt)

Install the Certbot Let’s Encrypt client package using the Snap package manager:

sudo snap install certbot --classic

Request a new Let’s Encrypt SSL certificate on your server using your virtual host domain. Replace app.example.com with your actual domain and hello@example.com with your email address.

sudo certbot --apache --agree-tos --redirect -d app.example.com -m hello@example.com

Test the Certbot SSL certificate renewal process:

sudo certbot renew --dry-run

Restart the Apache web server to apply your SSL configuration changes.

sudo systemctl restart apache2

Done!

You’ve successfully self-hosted n8n on Ubuntu. You can now start building workflows securely from your own server.

For additional security:

  1. Use HTTPS and strong passwords
  2. Set up firewall rules
  3. Backup your workflows regularly

Happy automating!