Introduction
In today’s digital age, automation is key to streamlining workflows and increasing efficiency. n8n, a powerful and flexible automation tool, empowers you to connect various applications and services. By self-hosting n8n on your Ubuntu 24.04 server, you gain complete control over your automation processes.
This blog post will guide you through the installation and configuration process, ensuring a smooth setup.
Prerequisites
- Ubuntu 24.04 Server
- SSH Access
- Root or sudo Privileges
Installation Steps
Update the System:
sudo apt update && sudo apt upgrade -y
Install Node.js and npm:
Get the version of NodeJS from https://deb.nodesource.com/
The url will be avaialble from above, just follow and there will be command avaialble which is down below;
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo bash -
sudo apt-get install -y nodejs
Check Version of both nodejs and npm
node --version
npm --version
Ensure to Update npm version to latest
sudo npm install -g [email protected]
Install n8n using npm
sudo npm install -g n8n
So it will take a while to install, my Node Version and NPM Versions are below, i have shared screenshot
You might need to update the version of npm, look for the latest version and update that as below
Now your n8n installation is compelted and you can even run and test it now ;
n8n
It will run n8n on port 4578 in my scenario it is
http://10.11.12.116:5678 your ip and port 5678 on http
Setup up a Database (postgreSQL) – Optional
By default, n8n uses SQLite. For production, you can configure a database recommended by n8n is PostgreSQL. For this tutorial we will isntall database on same server.
Use below to Install Database
sudo apt install postgresql postgresql-contrib
Now create database, user and password, you can use commands one by one. Make sure to use your own password instead of your_password.
sudo -u postgres psql
CREATE DATABASE n8n;
CREATE USER n8n_user WITH PASSWORD 'your_password';
GRANT ALL PRIVILEGES ON DATABASE n8n TO n8n_user;
\q
Environment variables for the database are explained Production Setep.
Running n8n for Production Environment
Though you can use n8n to start as service automatically when system reboots, but in case you want to monitor the performance and ensure the applicaiton keeps running then i recommend using PM2. However, if you are using only one applicaiton then you can go with systemd which I have explained at end. For recommended setting use below;
Install Nginx and configure n8n in production
First we will ensure n8n runs automatically when the server starts, so we will create n8n as service and we will ensure it workds automatically.
Step1 : Ensure DNS Points to Your Server
Once you have configured, ensure that it is correctly pointing to the correct server
nslookup example.syncbricks.com
Step2 : Configure Reverse Proxy (Nginx)
Reverse Proxy and SSL Certificate
For SSL and Reverse Proxy you can use nginx proxy manager , however, if this is your public server you can install nginx on same server as below
sudo apt install -y nginx
Set up a reverse proxy and configure SSL with Let’s Encrypt:
Step3 : Create an enable Nginx Configuration for n8n:
Now create the configuration file for n8n in nginx.
sudo nano /etc/nginx/sites-available/n8n
In below example replace the dns with correct dns, for n8n websocket support must be enabled
server {
listen 80;
server_name lms.syncbricks.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;
# WebSocket headers
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
Now Now enable the configuration and restart nginx
sudo ln -s /etc/nginx/sites-available/n8n /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
Great now nginx is configured and your site will be now working, but wait, we need to now configure ssl certificate.
Step4 : Secure with SSL ( Recommended):
Install Certbot to configure Let’s Encrypt SSL:
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d lms.syncbricks.com
Certbot will automatically update the Nginx configuration to use HTTPS.
Now your site is ready on production environment.
Step 5 : Verify SSL Renewal Setup
Check the timer using this command
sudo systemctl list-timers | grep certbot
You should see the following
certbot.timer Thu 2024-12-28 10:00:00 UTC 12h left certbot.service
You can also test the renewal process
sudo certbot renew --dry-run
Or you can manually renew as below
sudo certbot renew
Running N8N Automatically Using PM2 :
Install PM2: To Automatically Run n8n
PM2 can keep applications running, reload them without downtime, and perform other DevOps tasks. It also has a built-in load balancer and can run multiple instances of an application.
sudo npm install -g pm2
Start n8n with PM2:
sudo pm2 start n8n
Save the Process Configuration:
sudo pm2 save
Start PM2 on System Boot:
pm2 startup
Script for Startup
Now it will generate the script
sudo env PATH=$PATH:/usr/bin pm2 startup systemd -u ubuntu --hp /home/ubuntu
Run above command to save script and use below command to save the configuration for startup.
sudo pm2 save
Now reboot the server
sudo reboot
Now check the service the pm2 list, n8n will be running
pm2 list
n8n Envrionment Variable Configuration
nano ~/.n8n/.env
Secure Your n8n Installation and if you are using database as mentioned above in you can also change the envrinoment variables accordingly. Make sure to use your own password instead of your_password the one you used in setting up database.
N8N_BASIC_AUTH_ACTIVE=true
N8N_BASIC_AUTH_USER=your_username
N8N_BASIC_AUTH_PASSWORD=your_password
DB_TYPE=postgresdb
DB_POSTGRESDB_HOST=localhost
DB_POSTGRESDB_PORT=5432
DB_POSTGRESDB_DATABASE=n8n
DB_POSTGRESDB_USER=n8n_user
DB_POSTGRESDB_PASSWORD=your_password
Save and exist above.
Change WEBHOOK_URL
This step is essential for various authentication, webhooks and so on, ensure to update it based on the domain name that you have configured or your installation. Let us edit the environment variable again.
nano ~/.n8n/.env
Update the following variables:
WEBHOOK_URL=https://lms.syncbricks.com
WEBHOOK_TUNNEL_URL=https://lms.syncbricks.com
N8N_HOST=0.0.0.0
N8N_PORT=5678
Export Environment Variables
Now Export the Environment variables;
export $(cat ~/.n8n/.env | xargs)
Running n8n as System Service
In case you don’t want to install pm2, and you only want to run n8n as serivce you can use this option. But if you follow above you will not need to use service.
Create n8n Service
sudo nano /etc/systemd/system/n8n.service
Now simply copy below data and paste in this new file and ensure to change the name of the the ubuntu user , if you have done installation using root then you can mention root. In below example my user is amjid
[Unit]
Description=n8n Automation Service
After=network.target
[Service]
# Replace "your_username" with your actual username
User=amjid
# Specify the working directory for n8n
WorkingDirectory=/home/amjid/.n8n
# Load environment variables from .env
EnvironmentFile=/home/amjid/.n8n/.env
# Command to start n8n
ExecStart=/usr/bin/n8n
# Restart service on failure
Restart=on-failure
RestartSec=5s
[Install]
WantedBy=multi-user.target
Now save the file and exit
Now enable the service by typing below command
sudo systemctl enable n8n
Now Start the service
sudo systemctl start n8n
Additional Considerations
- Security:
- Use a strong password for the n8n user.
- Consider using a reverse proxy like Nginx or Apache to enhance n8n’s security.
- Keep n8n and its dependencies updated to address security vulnerabilities.
- Database:
- For larger-scale installations, consider using a database like PostgreSQL or MySQL.
- Configure n8n to connect to your chosen database.
- Monitoring:
- Leverage PM2’s monitoring features to track n8n’s performance and resource usage.
- Implement alerts for critical issues to proactively address potential problems.
- Backup:
- Regularly back up your n8n data and configuration files to safeguard against data loss.
Conclusion
By following these steps, you’ve successfully self-hosted n8n on your Ubuntu 24.04 server. You’re now empowered to harness n8n’s robust automation capabilities to streamline your workflows and boost productivity. Remember to keep n8n updated and secure to ensure optimal performance and reliability.