Aug 10, 2024 3 min read

Node-RED: A Beginner-Friendly Guide to Self-Hosting

Node-RED: A Beginner-Friendly Guide to Self-Hosting
Table of Contents

Node-RED is a powerful, open-source flow-based development tool designed to connect hardware devices, APIs, and online services in a simple, visual manner. It is an excellent choice for self-hosting because it provides complete control over your data, allows for extensive customization, and integrates seamlessly with a wide range of systems. In this guide, we’ll cover everything from installing Node-RED, configuring it with a reverse proxy, enabling logging, performing backups, and leveraging its unique features to suit your needs.

Installing Node-RED

πŸ“¦ Docker/Docker Compose Setup

Using Docker is one of the fastest and most isolated ways to deploy Node-RED. Below is a step-by-step guide to setting up Node-RED with Docker Compose:

  1. Create a docker-compose.yml file:

version: '3.8'

services:

nodered:

image: nodered/node-red:latest

container_name: node-red

ports:

- "1880:1880" # Map the default Node-RED port

volumes:

- ./node-red-data:/data # Persist data locally

environment:

- NODE_RED_ENABLE_PROJECTS=true # Enable project-based flows

- TZ=UTC # Set timezone

restart: unless-stopped

  1. Start the container using Docker Compose:

docker-compose up -d

  1. Verify the container is running:

docker ps

Node-RED will now be accessible at http://<your-server-ip>:1880.


πŸš€ Manual Installation

For manual installation on a Linux server, use the following commands:

  1. Update your system and install Node.js (at least v14.x):

sudo apt update && sudo apt upgrade -y

curl -fsSL https://deb.nodesource.com/setup_14.x | sudo -E bash -

sudo apt install -y nodejs

  1. Install Node-RED globally:

sudo npm install -g --unsafe-perm node-red

  1. Start Node-RED:

node-red

  1. Enable Node-RED to start on boot using systemd:

sudo systemctl enable nodered.service

sudo systemctl start nodered.service

Node-RED should now be running and accessible via its default port 1880.

Configuring Nginx as a Reverse Proxy

🌐 Nginx Configuration

To route traffic to Node-RED securely, set up an Nginx reverse proxy with the following server block:

  1. Create a new Nginx configuration file:

sudo nano /etc/nginx/sites-available/node-red

Paste the following configuration:


server {

listen 80;

server_name yourdomain.com;

location / {

proxy_pass http://localhost:1880;

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;

}

}

  1. Enable the configuration and reload Nginx:

sudo ln -s /etc/nginx/sites-available/node-red /etc/nginx/sites-enabled/

sudo nginx -t

sudo systemctl reload nginx

πŸ”’ SSL/TLS Setup

For secure communication, use Let’s Encrypt to add SSL/TLS:

  1. Install Certbot:

sudo apt install certbot python3-certbot-nginx -y

  1. Obtain and configure an SSL certificate:

sudo certbot --nginx -d yourdomain.com

  1. Automate certificate renewal:

sudo systemctl enable certbot.timer

πŸ› οΈ Testing and Reloading Nginx

Finally, ensure Nginx is configured correctly and reload it:


sudo nginx -t

sudo systemctl reload nginx

Logging and Debugging Node-RED

πŸ—ƒοΈ Enabling Debug Logs

To enable debug logs, edit the settings.js file:


nano ~/.node-red/settings.js

Set the following property:


logging: {

console: {

level: "debug", // Change to 'debug' to enable detailed logs

metrics: false,

audit: false

}

}

πŸ“„ Viewing Logs

If running Node-RED in Docker:


docker logs -f node-red

For systemd-based setups:


sudo journalctl -u nodered -f

πŸ› οΈ Troubleshooting Common Issues

Look for errors such as binding issues (EADDRINUSE) or missing modules (Module not found). Resolve these by stopping conflicting services or reinstalling required dependencies.

πŸ“€ Exporting Logs

Send logs to an external system like the ELK Stack:


docker run -d --name filebeat \

--volume=$(pwd)/filebeat.yml:/usr/share/filebeat/filebeat.yml \

--volume=/var/lib/docker/containers:/var/lib/docker/containers:ro \

docker.elastic.co/beats/filebeat:7.15.0

Backup and Restore

πŸ—‚οΈ File-Based Backups

Backup Node-RED’s data directory:


tar -czvf node-red-backup.tar.gz ~/.node-red

πŸ”„ Database Backups

If you use a database like SQLite with Node-RED, back up the database file:


cp ~/path-to-database/database.db ~/backups/database_backup.db

πŸ“… Automated Backup Scripts

Create a cron job for periodic backups:


crontab -e

Add the following line:


0 2 * * * tar -czvf /home/user/backups/node-red-$(date +\%F).tar.gz ~/.node-red

Updating and Upgrading Node-RED

⬆️ Updating Docker Images

Update the Node-RED Docker image:


docker-compose pull

docker-compose up -d

πŸ› οΈ Manual Updates

To update Node-RED manually:


sudo npm install -g --unsafe-perm node-red

πŸ” Checking for Updates

Check the currently installed version:


node-red -v

Leveraging Node-RED’s Unique Features

πŸ”§ Enabling APIs

Enable the HTTP Admin API by modifying settings.js:


httpAdminRoot: "/admin",

httpNodeAuth: {user: "admin", pass: "password"},

Access the API using curl:


curl -X POST http://localhost:1880/admin/flows -d '{"name":"new-flow"}' -H "Content-Type: application/json"

🌟 Advanced Configurations

Integrate external services like MQTT:

  1. Install the MQTT node:

cd ~/.node-red

npm install node-red-node-mqtt

  1. Add an MQTT broker node in the Node-RED editor and configure it to connect to your broker.

Wrapping Up

This guide covered the essential steps to self-host Node-RED, from installation to securing it with Nginx, managing logs, automating backups, and leveraging its advanced features. By following these instructions, you can fully harness the power of Node-RED for building custom workflows and automating tasks while maintaining complete control over your deployment. Start implementing these steps today and unlock the full potential of Node-RED!

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to Selfhosted Ninja.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.