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:
- 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
- Start the container using Docker Compose:
docker-compose up -d
- 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:
- 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
- Install Node-RED globally:
sudo npm install -g --unsafe-perm node-red
- Start Node-RED:
node-red
- 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:
- 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;
}
}
- 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:
- Install Certbot:
sudo apt install certbot python3-certbot-nginx -y
- Obtain and configure an SSL certificate:
sudo certbot --nginx -d yourdomain.com
- 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:
- Install the MQTT node:
cd ~/.node-red
npm install node-red-node-mqtt
- 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!