Skip to content

Instantly share code, notes, and snippets.

@ralphcrisostomo
Last active March 28, 2016 07:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ralphcrisostomo/6085ee1ade4a1ed27202 to your computer and use it in GitHub Desktop.
Save ralphcrisostomo/6085ee1ade4a1ed27202 to your computer and use it in GitHub Desktop.
raspberry pi setup

Raspberry Pi Setup

install

/etc/network/interfaces

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
address 192.168.1.20
netmask 255.255.255.0
gateway 192.168.1.1
network 192.168.1.0
broadcast 192.168.1.255

wireless-power off
auto wlan0
allow-hotplug wlan0
iface wlan0 inet manual
address 192.168.1.21
netmask 255.255.255.0
gateway 192.168.1.1
network 192.168.1.0
broadcast 192.168.1.255

wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp

oh-my-zsh

References:

Steps:

Install dependencies:

$ sudo apt-get update
$ sudo apt-get install git-core
$ sudo apt-get install zsh

Install oh-my-zsh

$ sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"

Change current shell to zsh

$ chsh -s $(which zsh)

Restart Raspberry

$ sudo shutdown -r 0

Node

References:

Install Node.js Steps:

Install dependencies:

$ sudo apt-get upgrade
$ sudo apt-get update

Donwload Node.js

$ wget http://nodejs.org/dist/v0.8.19/node-v0.8.19.tar.gz
$ tar xvzf node-v0.8.19.tar.gz
$ cd node-v0.8.19

Build Node from source

$ ./configure && make && sudo make install

Check if node and npm is installed

$ node -v
$ npm -v

Simple Node Server

Cool, now let’s run a simple test app.
Create a new file and call it hello-node.js and paste the following into it:

// Load the http module to create an http server.
var http = require('http');

// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
   response.writeHead(200, {"Content-Type": "text/plain"});
   response.end("Hello World\n");
});

// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(8000);

Now start the server

$ node ./hello.node.js	

And navigate a browser to localhost:8000 and you should see “hello world” print to the screen.

Setup a daemon and automatic start on boot

First cd into /etc/init.d/ and create a new file called node-server.sh

$ cd /etc/init.d
$ sudo nano node-server.sh

Next copy the following into it:
Note to change the path_to_node_app variable to wherever your app lives.

#! /bin/sh
# /etc/init.d/node-server

### BEGIN INIT INFO
# Provides:          node-server
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
### END INIT INFO

# change this to wherever your node app lives # 
path_to_node_app=/var/node/hello-node.js 

# Carry out specific functions when asked to by the system
case "$1" in
  start)
    echo "* starting node-server * "
    echo "* starting node-server * [`date`]" >> /var/log/node-server.log
    /usr/local/bin/node $path_to_node_app >> /var/log/node-server.log 2>&1&
    ;;
  stop)
    echo "* stopping node-server * "
    echo "* stopping node-server * [`date`]" >> /var/log/node-server.log
    killall /usr/local/bin/node
    ;;
  *)
    echo "Usage: /etc/init.d/node-server {start|stop}"
    exit 1
    ;;
esac
exit 0

Next we need to make this file executable via :

$ chmod 755 ./node-server.sh			

Now let’s tell the PI to execute this script and start the server on reboot:

$ update-rc.d node-server.sh defaults

If we ever want to disable this just type :

$ update-rc.d -f node-server.sh remove

And lastly you can now start and stop your node server in the background anytime via :

$ sudo /etc/init.d/node-server.sh start	
$ sudo /etc/init.d/node-server.sh stop	

I personally added aliases to these two commands in my ~/.bashrc

alias nodestart='sudo /etc/init.d/node-server.sh start'
alias nodestop='sudo /etc/init.d/node-server.sh stop'
alias nodetail='tail -n100 /var/log/node-server.log'

So I can start / stop the server and view the log file simply by typing the following :

$ nodestart
$ nodestop
$ nodelog		
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment