Install Node-RED with MQTT and Modbus on Ubuntu
Node-RED is a flow-based programming tool for IoT and automation. This guide installs Node-RED on Ubuntu with an Apache reverse proxy, Let's Encrypt SSL, the Mosquitto MQTT broker, and the Modbus TCP node.
Prerequisites
- Ubuntu server with root access
- A domain name pointed to the server
Steps
# Install base packages
apt install curl git zip software-properties-common apache2
# Install Node.js 18
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
apt-get install -y nodejs
# Install Node-RED globally
npm install -g --unsafe-perm node-red
# Install and configure Certbot
snap install core
snap install certbot --classic
certbot --apache
# Configure Node-RED settings
cd ~/.node-red/
nano settings.js
# Link Let's Encrypt certs for Node-RED HTTPS
ln -s /etc/letsencrypt/archive/yourdomain.com/cert1.pem cert.pem
ln -s /etc/letsencrypt/archive/yourdomain.com/privkey1.pem privkey.pem
# Generate a hashed admin password
node-red admin hash-pw
# Install build tools and Modbus TCP node
apt install g++
npm install node-red-contrib-modbustcp
Install Mosquitto MQTT Broker
apt-add-repository ppa:mosquitto-dev/mosquitto-ppa
apt install mosquitto libmosquitto-dev mosquitto-clients
service mosquitto status
Verify
# Start Node-RED
node-red
# Test MQTT pub/sub
mosquitto_pub -t test/topic -m 'hello'
mosquitto_sub -t test/topic
Notes
- Run Node-RED as a systemd service for production:
node-red admin hash-pwto generate the password hash, then set it insettings.jsunderadminAuth. - By default Node-RED listens on port 1880 — proxy it through Apache or Nginx for HTTPS.