Set Up Node-RED on a Raspberry Pi and Access It From Anywhere with Ngrok
Introduction
The Raspberry Pi is a versatile and affordable computer that has become popular among hobbyists, educators, and developers alike. When combined with Node-RED, a powerful flow-based development tool for visual programming, it becomes an effective tool for building Internet of Things (IoT) applications. However, accessing your Node-RED dashboard from outside your local network can be tricky without the right tools. Exposing a home server directly to the public internet by modifying router settings (such as enabling port forwarding) can introduce several risks and challenges. While it can be convenient to access your home server from anywhere on the internet, doing so without proper security measures can lead to potential vulnerabilities. That’s where ngrok comes in — an easy-to-use utility that exposes local servers behind NATs and firewalls to the public internet over secure tunnels.
In this blog, we’ll walk through setting up Node-RED (for sure not limited to this) on a Raspberry Pi and making it securely accessible from the public internet using ngrok.
What You’ll Need
- Raspberry Pi (any model with network capability, but Pi 3 or later is recommended)
- SD card with Raspberry Pi OS installed
- Internet connection
- Access to a terminal on the Raspberry Pi (either directly or via SSH)
- Free account on ngrok
Setting Up Node-RED on Raspberry Pi
First, ensure your Raspberry Pi OS is up to date:
sudo apt-get update
sudo apt-get upgrade
Next, install Node-RED follow the steps on its official website for raspberry pi.
bash <(curl -sL https://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered)
During the installation, enable user security feature. So that only you can login to your node-red server.
And enter your username and password. Then leave the rest as default.
Here we use PM2 to manage the node-red. PM2 is a popular process manager for Node.js applications that is designed to keep applications running continuously and facilitate common system administration tasks. To get Node-RED to start automatically whenever your instance is restarted, you can use pm2:
sudo npm install -g pm2
pm2 start `which node-red` -- -v
pm2 save
pm2 startup
Note: this final command will prompt you to run a further command — make sure you do as it says. Use following command to verify the node-red service status
pm2 list
You should see Node-RED status is online , and you can access the Node-Red from your host or remotely by <ip address>:1880
Installing and Setting Up ngrok
Before start, make sure you create an account on ngrok. Login and go to raspberry pi setup page
Follow the Ngrok official to install ngrok and config the auth
# install
curl -s https://ngrok-agent.s3.amazonaws.com/ngrok.asc \
| sudo tee /etc/apt/trusted.gpg.d/ngrok.asc >/dev/null \
&& echo "deb https://ngrok-agent.s3.amazonaws.com buster main" \
| sudo tee /etc/apt/sources.list.d/ngrok.list \
&& sudo apt update \
&& sudo apt install ngrok
# config auth
ngrok config add-authtoken <your-token>
Exposing Node-RED with ngrok
With ngrok installed and configured, you can now expose your Node-RED instance to the internet:
ngrok http http://localhost:1880
This command tells ngrok to tunnel traffic from the ngrok service to your local machine on port 1880, where Node-RED is running. Ngrok will display a screen with several bits of information, most importantly the public URL (e.g., https://abc123.ngrok.io
). This URL is now publicly accessible and routes traffic to your Node-RED instance.
To improve the experience, you can create a static domain on ngrok dashboard
Then it will provide a domain and command for you to link the domain.
Also you would need run ngrok on backgroud, I would suggest screen
. The commands to run ngrok in screen
are:
screen
ngrok http --domain=<your-domain> http://localhost:1880
# exit the screen by ctrl + a d
Now you can access the your Node-Red server from public with the domain from Ngrok.
Conclusion
You now have Node-RED running on your Raspberry Pi and accessible securely from anywhere in the world through ngrok. This setup allows you to monitor and control your IoT projects even when you are away from home.