Setup: VirtualBox 4.3.6, Ubuntu 13.10 CLI, Node.js, Forever.js

In my last post I covered a popular implementation of BackboneJS in Visual Studio.
[http://michaeldukehall.com/visual-studio-2013-single-page-application-with-backbonejs/]

I was not happy with it. Primarily because mixing a JavaScript Client-Side MV* solution with a ASP.Net Server-Side MVC solution feels clunky.
So, I’ve set out to do a clean Backbone setup. Which led me to the desire for a clean Web Server, which led me to NginX, and then to Node.
So, this post is a walkthrough of setting up a Node Web Server in Virtual Box.

The Steps:

  1. Install Virtual Box
  2. Create a Virtual Machine with Ubuntu 13.10 64bit CLI iso
  3. Install Guess Additions for your VBox version
  4. Install Node
  5. Install Express
  6. Install Forever

For brevity, I will skip the Virtual Box and Guest Additions steps as I’ve already covered this before.
The ISO for Ubuntu 13.10.CLI is here:
http://releases.ubuntu.com/saucy/
For this walkthrough, ensure you get the Server version: 64-bit PC (AMD64) server install image

In the settings for the VM, you can do what you like or use what I did:

  • RAM: 4gb
  • Select “create a virtual hard drive now”
  • choose VDI
  • Use dynamically allocated
  • Give it 20GB
  • Go to “settings”
  • Select General –> Advance
  • Enable shared clipboard and drag ‘n’ drop
  • Uncheck the mini toolbar
  • Go to storage
  • Click the empty disk under controller: IDE
  • Click the little disk icon with a down arrow and select your Ubuntu server iso.
  • Networking:
  • Leave as NAT unless you know how to setup a bridged connection on your computer

ERROR ALERT: If you get the error: VT-x is disabled in the BIOS.
FIX:

  • Reboot your machine into BIOS mode.

  • Find the Virtualization Setting
  • Enable it (sometimes there are two)
  • Save and Exit
  • Error should be fixed

Start the server and it will go into the Ubuntu install screens

ERROR ALERT: If you get the error: This kernel requires an x86 CPU, but only detected i686 CPU.
FIX: Close the machine, open settings, and change your OS to the Ubuntu/Linux 64 bit version.

Setup your username and password and jot that down for later.

When you get to the Software Selection screen; choose LAMP or SSH.
Azure Note: When you setup Ubuntu on a VM in Azure, this choice is made for you; SSH I believe.

Once complete, a command line interface will come back and ask you for the server login.
Enter your username and password

INSTALLING NODE
There are many, many ways to install Node on Unix. If you’re searching the intertubes, you will find a few different scenarios:

  • Installing from source on git. I don’t suggest this route
  • Installing through apt-get; this is a nice and easy route
  • Installing through NVM; this is also nice and easy
  • Installing straight from your Ubuntu; also nice and easy

There’s no “right way” so I will cover from git and from apt-get.
If you watch the Lynda.com training on Node; Joseph LeBlanc uses the NVM so he can quickly switch out which version of Node he is using.

Here is how to install it from GitHub…
First, install dependencies:


  sudo apt-get install g++ curl libssl-dev apache2-utils
  sudo apt-get install git-core
  sudo apt-get install make
  Then install:
  git clone git://github.com/joyent/node
  cd node
  ./configure
  make
  sudo make install

Here is how to install it from apt-get

sudo apt-get install nodejs

HELLO WORLD:
cd node/
sudo nano hello_world.js


var http=require('http');

http.createServer(function (req,res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello Node\n');
}).listen(8080, "127.0.0.1");

console.log('server running at port http://localhost:8080');

What you’ve done here is very rudimentary, but core.
You’ve bought in a core Node module called HTTP and set it to a local variable.
Then you used that local variable to access the “createServer” function to start a basic HTTP server.
Then you configured the “res”-response variable of that server to write out an HTTP 200 with a content type of text (not HTML).
Then you tell it to “listen” on port 8080 of your localhost.
And finally you write to the console where it should be running.

Save and exit.
Now use the “node” command to run your script.

sudo node hello_world.js

At this point you can open a browser to http://localhost:8080 and see your message.

This brings you to the first challenge of Node: How do you make it run as a service?
There are several ways, I will cover the Forever solution.
https://github.com/nodejitsu/forever

INSTALL FOREVER

sudo npm install forever -g

This will use the Node Package Manager to install forever (-g makes it a global installation, so it’s usable at the command line).

Once complete:

sudo forever start hello_world.js

Now your node server is running as a service and if it dies, Forever will restart it.

Leave a comment


Name*

Email(will not be published)*

Website

Your comment*

Submit Comment

© Copyright Duke Hall - Designed by Pexeto