set up a self-booting node.js server on a raspberry pi 3 using pm2, nginx, dataplicity, and your mac

  • basic BASH shell (cd / mkdir / touch / sudo nano / exit)
  • running a local server with node.js, such as this one

Get SSH terminal access to your Raspberry Pi

  1. Follow this guide to get Raspian onto your Pi. I would recommend “Download and image Raspian directly”. Briefly:
  • Use the SD Formatter 4.0 to format the SD drive (overwrite format).
  • Download the Raspian image
  • Copy the image to the SD card using
  • Put the micro SD chip into the Pi. Plug a mouse, keyboard, HDMI cable, and power into the Pi.

Install Node.js and your server on the Pi

  1. Connect to your Pi via SSH and type sudo apt update , and then sudo apt full-upgrade .
  2. Install Node Version Manager by copying the curl line from into the Pi terminal.
  3. Install Node.js by typing nvm install 8.0.0 .
  4. You can copy files to your pi using terminal commands, or use CyberDuck for drag and drop. To use CyberDuck, download it, and:
Open Cyberduck and click on File — Open Connection.
Choose SFTP from the dropdown box, then fill out the fields as follows:
Server: your Pi's local IP
Port: 22
Username: the username of your Pi, typically Pi
Password: the password of your Pi
Uncheck the box for Anonymous Login.
Hit Connect

Make your server load when the Pi reboots

  1. When connected to your Pi via SSH, type npm install -g pm2 to do a global install of the pm2 module.
  2. To run a server using pm2, go to your app directory and type pm2 start filename.js . If you need to see if it’s running, type pm2 ls . If you need to stop it, type pm2 delete 0 or whichever thread number you want to delete.
  3. To discover and implement the appropriate boot strategy for pm2, type pm2 startup . In this case, pm2 will create a the file /etc/systemd/system/pm2-pi.service which will include the following line: ExecStart=/home/pi/.nvm/versions/node/v8.0.0/lib/node_modules/pm2/bin/pm2 resurrect . In other words, when the Pi boots, it will execute pm2 resurrect, which will load whatever pm2 threads you have saved.
  4. In order to save the state of pm2 that you want to resurrect, run your server using pm2 start filename.js and then type pm2 save .

Connect your server to the outside world

  1. Set up a currently free account at Dataplicity. Follow the directions to link your Pi to the site by copying a line of code to your Pi.
  2. Click Activate Wormhole to receive an https link to your Pi’s port 80.
  3. To set up port 80 on your Pi, install nginx by typing sudo apt-get install nginx .
  4. Edit the config file by typing sudo nano /etc/nginx/sites-available/default .
  5. Delete everything in the file and, using your Pi’s local IP address and node’s port, replace it with:
server {
listen 80;
server_name whatever;location / {
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;

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Выстраиваем div-блоки в строку

Should I use ‘Async/Await’ instead of just Fetch in JavaScript?

Building a news application using Angular 6 and Google’s material design

Blazor — Detect if you are rendering on server or client

Moving to promises and async/await from callbacks

Document your React components using docz

100 Days of Code: Day 16

Tackling the Hack Reactor Interview

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Andrew Nease

Andrew Nease

More from Medium

Install Node.js using NVM on Ubuntu

Comparison between NodeJS vs. Django

Best practices for reducing the Docker image size for a Node.js application

Smoothing Noisy ADC Readings using Promises (NodeJS)