I wanted to get node.js and mongoDB up and running on my pi. I’ve got a couple of pet projects which I’m going to tackle and I’ve decided to use the MEAN stack (MongoDB, Express, AngularJS and Node.js). The future, for now, is JavaScript and I’m looking to know enough to make informed decisions about my place in that future.
These are the steps that I followed to get it going …
First up, node.js …
Credit to: http://blog.rueedlinger.ch/2013/03/raspberry-pi-and-nodejs-basic-setup/
Before doing this step you want to check what the latest version is – go to http://nodejs.org/dist/ and browse, starting from the biggest number backwards, looking for a release that includes “linux-arm-pi” in the title. That’s the one to use in the following code snippet, for me it was v0.10.24/node-v0.10.24-linux-arm-pi.tar.gz
1 2 3 4 5 |
sudo mkdir /opt/node wget http://nodejs.org/dist/v0.10.24/node-v0.10.24-linux-arm-pi.tar.gz tar xvzf node-v0.10.24-linux-arm-pi.tar.gz sudo cp -r node-v0.10.24-linux-arm-pi/* /opt/node sudo nano /etc/profile |
add the following before “export PATH”
1 2 |
NODE_JS_HOME="/opt/node" PATH="$PATH:$NODE_JS_HOME/bin" |
That’s it! Next let’s get a server running so we can make sure it’s working … create the following content in a file called nodejs.sh
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#!/bin/bash NODE=/opt/node/bin/node SERVER_JS_FILE=/home/pi/app/server.js USER=pi OUT=/home/pi/nodejs.log case "$1" in start) echo "starting node: $NODE $SERVER_JS_FILE" sudo -u $USER $NODE $SERVER_JS_FILE > $OUT 2>$OUT & ;; stop) killall $NODE ;; *) echo "usage: $0 (start|stop)" esac exit 0 |
then do the following to get it to run on pi boot
1 2 3 |
chmod 755 nodejs.sh sudo cp nodejs.sh /etc/init.d sudo update-rc.d nodejs.sh defaults |
now for the server.js that will run our server
1 2 3 |
mkdir /home/pi/app cd /home/pi/app nano server.js |
put the following in the server.js file
1 2 3 4 5 6 7 8 9 10 |
var http = require('http'); http.createServer(function(req,resp) { resp.writeHead(200, {"Content-Type": "text/plain"}); resp.write("Hello World"); resp.end(); console.log("sample output to console"); }).listen(8080); |
then start the server
1 |
sudo /etc/init.d/nodejs.sh start |
A quick visit to http://192.168.0.123:8080 should result in a nice “Hello World” message.
Finally a bit of tidying up in /home/pi/
1 2 3 |
rm nodejs.sh rm -rf node-v0.10.24-linux-arm-pi rm node-v0.10.24-linux-arm-pi.tar.gz |
Next up – mongoDB
Credit to: https://github.com/RickP/mongopi and http://ni-c.github.io/heimcontrol.js/get-started.html
Note, the scons commands performs the build and will take a few hours each
1 2 3 4 5 6 7 |
sudo apt-get install git-core build-essential scons libpcre++-dev xulrunner-dev libboost-dev libboost-program-options-dev libboost-thread-dev libboost-filesystem-dev git clone git://github.com/RickP/mongopi.git cd mongopi scons sudo scons --prefix=/opt/mongo install scons -c sudo nano /etc/environment |
add this before “export PATH”
1 |
PATH=$PATH:/opt/mongo/bin/ |
Then set up a new db and configure it to start on boot:
1 2 3 4 5 6 7 8 9 10 |
sudo useradd mongodb sudo mkdir /var/lib/mongodb sudo chown mongodb:mongodb /var/lib/mongodb sudo mkdir /etc/mongodb/ sudo sh -c 'echo "dbpath=/var/lib/mongodb" > /etc/mongodb/mongodb.conf' cd /etc/init.d sudo wget -O mongodb https://gist.github.com/ni-c/fd4df404bda6e87fb718/raw/36d45897cd943fbd6d071c096eb4b71b37d0fcbb/mongodb.sh sudo chmod +x mongodb sudo update-rc.d mongodb defaults sudo service mongodb start |
You can test everything went ok by launching the MongoDB shell with the command “mongo” … see the docs for more info.
Job Done!