Multiple NodeRed Instances on different Ports - Linux/SystemD

#1

I know this has been addressed because I've made it work in the past, so I apologize for rehashing an already discussed topic... but I can't seem to find the instructions again. I've tried multiple times using some of the other methods I've found to get this to work and have done nothing but break Node_Red repeatedly.

I have an AWS server and I need to run 2 instances of NodeRed on 2 different ports. I'm running Ubuntu and using SystemD to autostart Node-Red on boot.

The goal is to have 2 admin panels so that 2 different people can test and develop without having to boot up a second server with its own Node-Red install... and be able to point 2 different domains to the UI of those instances.

I'd like to avoid using PM2 if possible, as I used this method of autostart when I installed: https://nodered.org/docs/hardware/raspberrypi#adding-autostart-capability-using-systemd

I tried the following instructions, but only managed to get an error saying it can't find red.js and Node-Red failed to boot: https://nodesource.com/blog/running-your-node-js-app-with-systemd-part-2/

So I've rolled back... fresh install of Node-Red... What's the easiest way to get 2 instances running on 2 different ports?

Thanks in advance.

0 Likes

#2

What I do, is create two different directories and do npm i node-red in each one and then edit the settings.js file in node_modules/node-red so that they use different ports.

And then, in each directory, I launch two node-reds using a command like node node_modules/node-red/red -s node_modules/node-red/settings.js -u ./

This tells node-red to use the settings.js file found in the node_modules/node-red directory and to create a flow.json file in the current directory.

0 Likes

#3

Use the command line options.

Command-line usage

Usage: node-red [-v] [-?] [--settings settings.js] [--userDir DIR] [flows.json]

Options:
  -s, --settings FILE  use specified settings file
  -u, --userDir  DIR   use specified user directory
  -v                   enable verbose output
  -?, --help           show usage

You only need to install once then run a second, third, nth instance with different command line options.

1 Like

#4

Typically I do ...

node-red -u /node-red-1881 -p 1881

Just noticed the -p option is missing from documentation (but it definitely works)

Perhaps documentation is out of date?

https://nodered.org/docs/getting-started/running

1 Like

#6

Hi @Steve-Mcl, good point. The part to be updated is the result of the node-red --help, which would be:

doc-01

I will fork the doc repository and try to submit a pull request.

0 Likes

#7

Thanks for the reply. I've seen several posts regarding the CLI options...

I just can't seem to get those incorporated into the auto-startup option. I may not have been very clear in my post, sorry.

I am using systemd to auto-start the node-red service. Currently it starts 1 instance on port 1880.

I need it to also auto-start a second instance on port 1881.

I've tried copying the /lib/systemd/system/nodered.service, renaming it, modifying the internal settings to try and pull from different settings.js files... and then modifying those settings.js files to use different ports.

I've tried modifying the Environment line to include the options you've specified for the CLI.... but no matter what I try, it either tries to start a second instance on the same port, or throws some other error...like can't find "red.js".

Am I over-complicating this by trying to use SystemD to autostart both... and maybe I should just write a script?

0 Likes

#8

Show us your modified systemd script that tries to specify the settings and flow file.

1 Like

#9

As long as your settings.js file is right, you can do this via an environment variable. I posted my setup a few days ago that showed how to do this via an environment file.

settings.js:

uiPort: process.env.PORT || 1880,

/etc/systemd/system/multi-user.target.wants/nrlive.service

Obviously you can call this whatever you want.

# See https://gist.github.com/Belphemur/3f6d3bf211b0e8a18d93
#     http://www.freedesktop.org/wiki/Software/systemd/
#
# If changing this file, run:
# sudo systemctl daemon-reload && sudo systemctl enable nrlive

[Unit]
Description=Node-Red Home Automation production (live) service
Documentation=file:///home/pi/nrlive/README.md http://nodered.org/docs
After=syslog.target network.target
Wants=mosquitto.service influxdb.service

[Service]
Type=simple

#Environment="NODE_OPTIONS=--max-old-space-size=128" "NODE_RED_OPTIONS=--userDir /home/pi/nrlive/.data -v"
#Environment="NODE_RED_OPTIONS=-v"

# Environment variables file, overrides the Environment settings
# @See https://www.freedesktop.org/software/systemd/man/systemd.exec.html#EnvironmentFile=
EnvironmentFile=/home/pi/nrlive/.data/envFile.txt

# No path available here so make sure absolute folders specified
ExecStart=/usr/bin/node $NODE_OPTIONS /home/pi/nrlive/node_modules/node-red/red.js $NODE_RED_OPTIONS

WorkingDirectory=/home/pi/nrlive

# User/Group that launches node-RED (it's advised to create a new user for Node-RED)
# You can do : sudo useradd node-red then change the User=root by User=node-red
User=pi
Group=pi

Nice=10

SyslogIdentifier=NRlive
StandardOutput=syslog

# Make Node-RED restart if it fails
Restart=on-failure

#Restart=always
#RestartSec=100

# Node-RED need a SIGINT to be notified to stop
KillSignal=SIGINT

# Prevent this service changing the OS or /etc
# @See https://www.freedesktop.org/software/systemd/man/systemd.exec.html#ProtectSystem=
ProtectSystem=full

[Install]
WantedBy=multi-user.target

envFile.txt

# Environment variables file for running nrlive under SystemD
# Each line is a separate variable that will be available to the ExecStart
# and to the running process.
# @see https://www.freedesktop.org/software/systemd/man/systemd.exec.html#EnvironmentFile=
#
# Called from /etc/systemd/system/multi-user.target.wants/nrlive.service
# Which is linked from /home/pi/nrlive/systemd/nrlive.service

NODE_OPTIONS=--max_old_space_size=256
NODE_RED_OPTIONS=--userDir /home/pi/nrlive/.data -v
PORT=1880
1 Like

#10

@Colin I mentioned in my first post that I wiped everything and started fresh, so my systemd file is currently a wget from: https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/nodered.service

as instructed here: https://nodered.org/docs/hardware/raspberrypi#adding-autostart-capability-using-systemd

I probably should have started with my modified file and asked for help, but nothing worked, and I wasn't even sure I was on the right track really.

@TotallyInformation Thanks for the post! I am pretty sure I can get that to work. I'll try first thing when I get back in the office tomorrow morning.

0 Likes

#11

No problem. Just note that I don't use the standard installation of Node-RED. Instead I prefer to install to a "master" folder with a sub-folder containing the userDir. I'm sure you will be able to adjust according to your needs.

0 Likes