How I currently set up a Raspberry PI to run a Node.js application

Some notes for myself and maybe others.

Buy Raspberry PI and SD card.

Download Raspbian Jessie Lite:

https://www.raspberrypi.org/downloads/

Unpack the image file, insert the SD cart, and write the image onto the card.

On a mac, using the terminal, first identify the sd card disk:

diskutil list

Unmount the disk identified, for example /dev/disk1 (not disk1s1)

diskutil unmountDisk /dev/disk1

Write the image

sudo dd bs=1m if=2017-04-10-raspbian-jessie-lite.img of=/dev/disk1

Now over to the PI

Insert the SD card, connect a monitor and a keyboard, connect a network cable, boot up

Set password and enable SSH in raspi config that starts on first boot.

If you don’t have a monitor, these are the keystrokes needed in the current version of Rasbpian Jessie Lite (4.4) to enable SSH, enter them slowly to be sure the raspberry has time to do stuff:

Power on the PI. Wait 2 minutes for it to boot to be sure.
Type "pi" + ENTER
Type "raspberry" + ENTER
Type "sudo raspi-config" + ENTER (if you're not on an american keyboard the - sign is on the key to the right of 0)
Arrow down 8 times + ENTER
Arrow down 3 times + ENTER 
ENTER + ENTER
TAB + TAB + ENTER

Find out the IP and connect via SSH using the terminal:

ssh pi@XXX.XXX.X.X

If you want to connect the raspberry directly to your mac via an ethernet cable;

Make sure you have DHCP enabled in Network Preferences, Go to Sharing menu, enable Internet Sharing from Wi-Fi.

Connect the raspberry, let it boot up, type

arp -a

to find out the IP, one of the devices should be it, it’s usually something like 192.168.2.2.

Connect with the ssh command as mentioned above.

Once you’r in, first update and upgrade

sudo apt-get update
sudo apt-get upgrade

Set the su password:

sudo passwd

Install node via for example from Heroku

wget http://node-arm.herokuapp.com/node_latest_armhf.deb
sudo dpkg -i node_latest_armhf.deb

Ok, now node is installed and we’re all set.

The following instructions assumes you already have created a node app that you want to pull from a git repository.

Create a deployment directory, i usually create deploy in /opt/deploy/xxx

You should generally never use sudo with git commands, so make sure pi is the owner of the deployment directory:

sudo chown -R pi /opt/deploy/

To set up git access, if this PI will be managed by others that don’t know your git password, it’s a good idea to set up deployment ssh key. That way you can pull from git without a password.

Refer to your git service documentation on how to set this up.

I use bitbucket, here’s the procedure:

First, on the PI, create a SSH key:

ssh-keygen

Just press enter for “enter a file” and password.

To get the content of the key, type:

cat ~/.ssh/id_rsa.pub

Select and copy to clipboard.

Then go to the git hosting service in add this key.

Then you should be able to clone:

git clone ssh://git@bitbucket.org:XXXX/XXXX.git

If this does not work and you get permission denied errors – make sure you don’t run the command as sudo, but instead set the correct owner.

Next install node modules, in the root folder of the project

npm install

Assuming you have a package.json file that specifies the dependencies.

Test run the app:

node app.js

Next up, you want to set up the node app to start on boot.

Recently I’ve been using Express generator more and more;
express –view=hbs –git myproject

I prefer using Process Manager 2:

sudo npm install pm2 -g

Next, set up pm2 to start on boot

sudo pm2 startup systemd -u pi

Next start the app

pm2 start app.js

Try it out by rebooting. It’s recommended to restart and stop the PI using commands like reboot, to prevent possible errors and loss of data:

sudo reboot

When you log on again, the command:

pm2 list

Should show the app running.

Also, get to know the other pm2 commands, like pm2 restart, pm2 stop and pm2 delete.

And note that pm2 puts everything that is written to the console in ~/.pm2/logs/ for debugging.

If you want to use port 80 without running the app as sudo (which you generally shouldn’t) here’s one way to solve this:

sudo apt-get install libcap2-bin
sudo setcap cap_net_bind_service=+ep /usr/local/bin/node

That’s it. Now you have an autostarting, git deployable node.js application running on the raspberry.

I will soon add: How to set fixed ip + how to connect the pi directly to a mac.