Install NodeJS Server in Raspberry Pi
Check this tutorial: https://fleetstack.io/blog/cloudflare-tunnel-raspberry-pi-setup
1
2
| sudo apt update
sudo apt install nodejs npm -y
|
Create app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello from Raspberry Pi');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
|
Run our application
Setting Up Cloudflare Tunnel on Raspberry Pi
In this guide, we will walk through the steps to set up Cloudflare Tunnel on a Raspberry Pi. This is particularly useful for creating a secure and private web server without opening ports on your router or main network.
Prerequisites:
- A Raspberry Pi (any model should work)
- An active Cloudflare account with a domain name pointing to your Raspberry Pi
- The Cloudflare Tunnel client installed on your Raspberry Pi
Step 1: Install the Cloudflare Tunnel Client
1
| sudo apt update && sudo apt upgrade -y
|
1
| sudo apt install -y curl
|
1
| wget https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-arm.deb sudo dpkg -i cloudflared-linux-arm.deb
|
1
| cloudflared tunnel login
|
1
| cloudflared tunnel create your-tunnel-name
|
Replace your-tunnel-name with a name of your choice.
1
| cloudflared tunnel route dns raspberrypitunnel raspberry.wanttedbot.com
|
1
| cloudflared tunnel run --url localhost:3000 your-tunnel-name
|
cloudflared tunnel run –url localhost:3000 raspberrypitunnel
1
| sudo nano ~/.cloudflared/config.yml
|
1
2
3
4
5
6
7
| tunnel: raspberrypitunnel
credentials-file: /home/pi/.cloudflared/fxe956cx8-a2x4-47e3-bx46-xxxxxxxxx.json
ingress:
- hosname: raspberry.wantedbot.com
service: http://localhost:3000
- service: http_status:404
|
1
| sudo cloudflared --config ~/.cloudflared/config.yml service install
|
1
| sudo systemctl enable cloudflared --now
|
Ensure that the cloudflared tunnel starts automatically on reboot.
start the command when reboot
We can modifiy the local file.
1
| sudo nano /etc/rc.local
|
make the file excutable
1
| sudo chmode +x /etc/rc.local
|
1
2
3
4
5
6
7
8
| #!/bin/sh -e
#
# rc.local
#
node /home/fengyi/web/app.js &
date >> /home/fengyi/logs/boot-log.txt
exit 0
|
Then sudo reboot to see if it works!