n8n Workflow Automation tool installation on Ubuntu 20.04

n8n is an extendable workflow automation tool. With a fair-code distribution model, n8n will always have visible source code, be available to self-host, and allow you to add your own custom functions, logic and apps. n8n's node-based approach makes it highly versatile, enabling you to connect anything to everything.

Follow below step to install n8n package via npm on Ubuntu 20.04.

1. Install npm and others useful package.

sudo apt-get install npm cron nano rsyslog logrotate lsof

2. Install node.js version 14 and above globally.

sudo npm install n -g
sudo n stable

3. Install n8n package globally via npm.

sudo npm install n8n -g --unsafe-perm

4. Setup systemd script (/lib/systemd/system/n8n.service) to auto stop start n8n services.

[Unit]
Description=n8n
Documentation=https://n8n.io
After=network.target

[Service]
EnvironmentFile=/path/n8n-env.conf
Type=simple
User=ubuntu
ExecStart=/usr/local/bin/n8n
Restart=on-failure

[Install]
WantedBy=multi-user.target

5.Create n8n-env.conf to parse n8n environment variable at start with the systemd script. Remember to restart n8n service if you update/modify the environment variable.

# basic authentication
N8N_BASIC_AUTH_ACTIVE=true
N8N_BASIC_AUTH_USER=admin
N8N_BASIC_AUTH_PASSWORD=P@ssw0rd

# timezone
GENERIC_TIMEZONE=Asia/Kuala_Lumpur

# webhook http/https setup
WEBHOOK_TUNNEL_URL=https://YOUR-FQDN/
N8N_HOST=YOUR-FQDN
N8N_PROTOCOL=https
VUE_APP_URL_BASE_API=https://YOUR-FQDN/

# Activate automatic data pruning
EXECUTIONS_DATA_PRUNE=true

# Number of hours after execution data will be deleted
EXECUTIONS_DATA_MAX_AGE=168

# Run sqlite vacuum operation at startup
DB_SQLITE_VACUUM_ON_STARTUP=true

6. Another option to parse environment variable in n8n is to to use json file. Create n8n-env.conf and n8n-env.json with below setup. Btw, not all environment variable are available on the json configuration.

# n8n json format configuration file
N8N_CONFIG_FILES=/path/n8n-env.json

# webhook http/https setup
WEBHOOK_TUNNEL_URL=https://YOUR-FQDN/
N8N_HOST=YOUR-FQDN
N8N_PROTOCOL=https
VUE_APP_URL_BASE_API=https://YOUR-FQDN/

#n8n-env.json

{
    "database": {
        "sqlite": {
            "executeVacuumOnStartup": true
        }
    },
    "executions": {
        "pruneData": true,
        "pruneDataMaxAge": "168"
    },
    "generic": {
        "timezone": "Asia/Kuala_Lumpur"
    },
    "security": {
        "basicAuth": {
            "active": true,
            "user": "admin",
            "password": "P@ssw0rd"
        }
    }
}

7. Reload, enable and start the n8n service using systemctl.

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

8. Install nginx web server for n8n's webhook function and also reverse proxy the n8n webUI. You need to register a FQDN for your setup.

sudo apt-get install nginx

9. Disable nginx default configure and create /etc/nginx/sites-available/reverse-proxy.conf to reverse proxy to n8n webUI.

sudo unlink /etc/nginx/sites-enabled/default
sudo ln -s /etc/nginx/sites-available/reverse-proxy.conf /etc/nginx/sites-enabled/reverse-proxy.conf 
sudo systemctl restart nginx

10. If you want to use HTTPS for the webhook and n8n webUI. Get a free SSL certificate from LetsEncrypt via certbot for nginx.

sudo apt-get install python3-certbot-nginx
sudo certbot --nginx -d YOUR-FQDN

11. Optional, if you setup this on Oracle Cloud. Remember to enable HTTP and HTTPS access in iptables (/etc/iptables/rules.v4) and Oracle's VCN firewall.

-A INPUT -p tcp -m state --state NEW --dport 80 -j ACCEPT
-A INPUT -p tcp -m state --state NEW --dport 443 -j ACCEPT



Comments