Setup hostname for Pi

Once you have NodeJS installed (with NPM which installs with it), the obvious next thing is to build a server. I already had something super basic up and running and I wanted to access it locally on my network.

What would be better is if I could have a URL like: http://myrpi/ and everything just started to work.

This is where the lovely gentleman that is Pete LePage helped me by telling me to install the avahi-daemon and then pointed out that I can access my sites with whatever is in the /etc/hosts file followed by .local.

Let's step through this and get it running.

First install the daemon, which basically broadcasts the host on the local network.

sudoapt-getinstall avahi-daemon

Set a name in the /etc/hosts file, lets say myrpi.

First open up the file to edit

sudonano /etc/hosts

At the bottom of our /etc/hosts/ file add the following.

127.0.1.1 myrpi

This is simply an IP address that can be used to define the hostname for the machine.

One side note of this. In my router I can define a hostname for each attached device, if I set the hostname to 'myrpi' I don't actually need to type in the .local part of the URL, in fact http://myrpi:3000 is all that is needed.

No Port Number & Start on Boot

The next thing I wanted to do was have the Node server start up whenever I started up the Raspberry Pi. I did this by adding a script to /etc/init.d/ which basically starts the Node server using forever.

For those new to forever it's basically an node app which given a seperate Node script to run, it will start the script and run it forever, restarting it if it ever crashes.

To do this, the steps are as follows.

Install forever globally.

Install forever globally so that scripts can access it anywhere on the pi, you do this with the -g command.

sudonpminstall -g forever

Create Your Startup Script

I ended up using something similar to the script below, I try and explain each part afterwards.

Script Start

The script is designed to run with the command sudo /etc/init.d/my-script start or sudo /etc/init.d/my-script stop. In the next bit of the script we take the first variable from the command line, in this case it will be the start or stop text and if it's start we add two rules to the iptables and then start our Node script with sudo forever start $NODESCRIPT/cli.js.

There are a few things to step through for start.

The iptables commands are the thing which makes requests on port 80, redirect to port 3000. What this means is when I type http://myrpi.local/ the request will arrive at the Pi on port 80, this rule then redirect from port 80 to port 3000. The reason we have two is because we need to do it for both ethernet and wifi connections. You might be able to cut this down into one.

If your router supports it, you can forward port 80 to port 3000 and avoid having to do any of this.

I've run the forever script with sudo -u pi. The reason I've done this, is otherwise forever starts the scripts under some unknown user, meaning I can't then stop the forever task reliably. If I use sudo -u pi, I can always run the stop command under pi, which has access to the forever task and can hence, stop it.