NodeJS on OpenShift

This tutorial will get you through starting your OpenShift application. This document assumes you are on a mac or a any other unix based system.

We will be installing ExpressJS 3.0 on a NodeJs application.

Create your app

make sure you you add express@3.0 and ejs to your global node_module_list file that openshift creates for you.

make sure you use __dirname to call the node_module/express module.

Installing NodeJs on a Server

NodeJS’s blazing fast performance is gaining popularity very fast. It’s only a matter of time before it becomes the de facto development platform. Interest in NodeJS is far out stripping the supply-demand chain of shared hosting providers. Therefore, people like our selves should turn to Virtual Environment servers to get things done from scratch, rather than having to rely on providers to play catch up.

NodeJS is strengthening the relationship between the physical and the digital in social development. From Arduino projects to iPhone applications; it’s currently being used by IBM, Facebook, Foursquare and by many more projects found in this list of companies. Node.js is well-suited for building solutions that require real-time, synchronous interaction.

I’ve written a tutorial into getting NodeJS installed on a brand new server from scratch. We will be using Debian 6 and Nginx as the web server on a 512MB RAM virtual environment. Also, we’ll want to install a database to get you ready, I’ve chosen MongoDB.

Installing NodeJS, Nginx, MongoDB on Debian

To get started we need to purchase a web server. I have Mediatemple’s (ve) 512MB with Debian 6.0 Squeeze. I highly recommend them to any other hosting company. Go ahead and purchase your (ve) server. I also highly recommend you work on a MAC!! Since it’s UNIX based, and every cool programmer has one now anyway, so best to get one and strengthen the community.

Top Tip: You can get to know mediatemple’s super friendly contact support staff by calling them for FREE! It’s a common secret, but Mediatemple’s call centre in LA, California is hooked up to Skype. Go ahead and add ‘contact-me’ to Skype! They’re great. #mediatemple.

Configuring Debian

Presuming you have your server, installed Debian 6 and have your root access credentials ready, we can begin.

SSH into your server as root. Change the below zeros to your’ servers IP address.

ssh root@00.00.00.00

Once you’ve gone through the initial instructions. Go ahead and update, upgrade you’re server.

apt-get update
apt-get upgrade

Make sure you have you’re locale settings corrected for your Debian system.

export LANGUAGE=en_US.UTF-8
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
locale-gen en_US.UTF-8

Select your’ locale, by entering the below command. There should be en_US.UTF-8, select that and press enter.

dpkg-reconfigure locales

Remember we won’t be needing Apache, MySQL or PHP so we won’t install them, since they’re total resource whores. Let’s get curl installed, by entering the following command.

apt-get install curl

You may need a means of accessing your files via FTP. So go ahead and install VSFTPD.

apt-get install vsftpd

You can use your root username and password to FTP into your server with SFTP, port 22.

Installing the Pre-Requisite

Now for this very important command, which installs NodeJS, Nginx, and the pre-requisites to get started.

apt-get install vim-nox nginx unzip g++ screen libssl-dev git-core

You’ll have to go through all the instruction given during the installation. After installation has finished, you can install NodeJS, with the following command. Make sure your /root/ directory when entering the below command.

git clone git://github.com/ry/node.git

Then build it.

./configure
make
make install

That process will take a very long time, mostly because it is compiling Google’s V8 engine from source, which plays a big part of NodeJs. Let it work, and then install NPM (Node Package Manager).

curl http://npmjs.org/install.sh | sh

Configuring Nginx

Now let’s create a file named /usr/bin/php-fastcgi with the following command.

nano /usr/bin/php-fastcgi

Add the following contents.

#!/bin/bash

FASTCGI_USER=www-data
FASTCGI_GROUP=www-data
SOCKET=/var/run/php-fastcgi/php-fastcgi.socket
PIDFILE=/var/run/php-fastcgi/php-fastcgi.pid
CHILDREN=6
PHP5=/usr/bin/php5-cgi

/usr/bin/spawn-fcgi -s $SOCKET -P $PIDFILE -C $CHILDREN -u $FASTCGI_USER -g $FASTCGI_GROUP -f $PHP5

Save and exit that file, and now let’s make it executable.

chmod +x /usr/bin/php-fastcgi

Now let’s create a file named php-fastcgi in /etc/init.d/ with the following command.

nano /etc/init.d/php-fastcgi

And add the following content to that file, then save and exit.

#!/bin/bash

PHP_SCRIPT=/usr/bin/php-fastcgi
FASTCGI_USER=www-data
FASTCGI_GROUP=www-data
PID_DIR=/var/run/php-fastcgi
PID_FILE=/var/run/php-fastcgi/php-fastcgi.pid
RET_VAL=0

case "$1" in
    start)
      if [[ ! -d $PID_DIR ]]
      then
        mkdir $PID_DIR
        chown $FASTCGI_USER:$FASTCGI_GROUP $PID_DIR
        chmod 0770 $PID_DIR
      fi
      if [[ -r $PID_FILE ]]
      then
        echo "php-fastcgi already running with PID `cat $PID_FILE`"
        RET_VAL=1
      else
        $PHP_SCRIPT
        RET_VAL=$?
      fi
  ;;
    stop)
      if [[ -r $PID_FILE ]]
      then
        kill `cat $PID_FILE`
        rm $PID_FILE
        RET_VAL=$?
      else
        echo "Could not find PID file $PID_FILE"
        RET_VAL=1
      fi
  ;;
    restart)
      if [[ -r $PID_FILE ]]
      then
        kill `cat $PID_FILE`
        rm $PID_FILE
        RET_VAL=$?
      else
        echo "Could not find PID file $PID_FILE"
      fi
      $PHP_SCRIPT
      RET_VAL=$?
  ;;
    status)
      if [[ -r $PID_FILE ]]
      then
        echo "php-fastcgi running with PID `cat $PID_FILE`"
        RET_VAL=$?
      else
        echo "Could not find PID file $PID_FILE, php-fastcgi does not appear to be running"
      fi
  ;;
    *)
      echo "Usage: php-fastcgi {start|stop|restart|status}"
      RET_VAL=1
  ;;
esac
exit $RET_VAL

Start php-fastcgi and nginx by issuing the following commands.

chmod +x /etc/init.d/php-fastcgi
update-rc.d php-fastcgi defaults
/etc/init.d/php-fastcgi start
/etc/init.d/nginx start

Creating a NodeJS App

Let’s make your website’s directory ready. You’ll need to make a directory for it in /var/www/example.com/ be sure to change example.com with your website address anywhere in this tutorial. To make that directory, enter the following command.

mkdir /var/www/example.com

Let’s chown that directory.

chown -R www-data:www-data /srv/www/www.example.com

Now let’s get started by writing our first NodeJS app in that directory. Enter the following command, be sure to change example.com with your website.

nano /var/www/example.com/app.js

Type in the following code and save the file by tapping ctrl-o and ctrl-x (these are the nano text editor’s commands). Be sure to change the IP address to your server’s address.

var http = require('http');
http.createServer(function (req, res) {
 res.writeHead(200, {'Content-Type': 'text/plain'});
 res.end('Hello World!\n');
}).listen(8000, "222.222.222.222");
console.log('This Node app is running on port 8000.');

Now it’s time to configure you’re Nginx web server. You may not need to, but add a line to reference your host on the file. However this may be auto generated with a line to your primary domain name.

nano /etc/hosts

Save and exit that, and now its time to enable our site by adding a virtual host to Nginx. Create the virtual host with the following command.

nano /etc/nginx/sites-available/example.com

Add the following in that file.

upstream app_example_com {
 server 222.222.222.222:8000;
}

server {
 listen 216.70.83.122:80;
 server_name example.com example.com;
 access_log /var/log/nginx/examplecom.log;
 location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-NginX-Proxy true;

    proxy_pass http://app_example_com/;
    proxy_redirect off;
 }
}

Let us now enable the site and create a symbolic link to it, using the following commands.

cd /etc/nginx/sites-enabled/
ln -s /etc/nginx/sites-available/example.com example.com

Let’s restart Nginx to test it, with the following command.

/etc/init.d/nginx restart

Using Forever

As you might have gathered, NodeJS only runs once you use the ‘node app.js’ command and will quit as soon as you close the SSH connection. Therefore, we’ll be using forever to keep node child processes running in the background. Do the following command to install Forever globally. The installation takes a while.

npm install forever -g

Start your node application using forever.

forever start /var/www/example.com/app.js

Now you can go to the browser and view example.com even when the SSH connection and the terminal has closed.

 

Glossary

  • Nginx, a HTTP and mail proxy server licensed under a 2-clause BSD-like license. By Igor Sysoev.
  • MongoDB, MongoDB (from “humongous”) is an open source, high-performance, schema-free, document-oriented NoSQL database system written in the C++ programming.
  • vsFTPd, the “Very Secure FTPD” is a *nix (Unix, Linux) FTP Server.
  • NodeJS, a software system designed for writing highly-scalable internet applications, notably web servers.
  • ExpressJS, a Sinatra inspired web development framework for node.js.
  • Debian, is a free distribution of the GNU/Linux operating system.
  • NPM, a package manager for node. Installs, publishes and manages node programs.

Common Commands

  • Restarting Nginx, /etc/init.d/nginx reload

References

Some notable references that may help.

  • http://stackoverflow.com/questions/5009324/node-js-nginx-and-now
  • http://nodenode.com/post/1197688151/installing-node-js-on-ubuntu-screencast-tutorial
  • http://awebfactory.com.ar/node/467#nodejs
  • https://github.com/remy/nodemon
  • http://blog.martinfjordvald.com/2010/07/nginx-primer/
  • http://library.linode.com/web-servers/nginx/php-fastcgi/ubuntu-11.04-natty
  • http://www.shinstudio.com/blog/backend-tech/setting-up-node-js-in-nginx/
  • http://www.mongodb.org/display/DOCS/Quickstart+Unix
  • http://dailyjs.com/2010/03/15/hosting-nodejs-apps/

 

Buy Transport Company WordPress Theme Now

Introducing the worlds first fully automated Transport Company WordPress Theme. Developed by Sirwan Qutbi, it is on Sale Now for $39. A WordPress theme for any transport company, from taxi ranks to mini cab stations and coach services to any aviation carriers. Get bookings and payment online, expand your business for as little as $39. If you run a mini-cab business or a fleet of coaches and need your customers to reach you via the web. Then this solution will be the best for you. Instant management of all the orders and reservations, and much more. Check out the features below.

Click here to Purchase it Now for $39 and instantly Download it!

View the Demo here.

A booking system designed to take reservations online using Google Map’s API to estimate the price according to your rates, from location A to location B. The program is intuitive enough to let the operator set specific rates for different vehicle types, which would calculate the time taken for the best possible route. It works in any country in the world.

Have a look through its features.

Top Features

  • PayPal Intergrating (also allows for non-PayPal members to pay via credit card)
  • Easy navigation of journey with Google Maps API integrated into the theme.
  • Calculates distance, time and price based on Googles Map navigation.
  • Many vehicle types such taxis, coaches, mini buses, limos and more.
  • Customers can pay and/or reserve the booking online.
  • Customers Reference number automatically generated.
  • Colour co-ordinated bookings.
  • Access Control for employees, from administrator to employee to check the bookings table but not edit office settings.

Office Features

  • Easily enter your phone number and address from admin panel.
  • Switch between calculating the price based on the distance of the journey or have a preset database of locations.
  • Easily switch between Kilometres or Miles for unit of distance.
  • Option to restrict the journey area to just inside the city.
  • Option to set distance limit for pick up area.
  • Set the maximum distance from office to pick-up location.
  • Powerful Price Calculator for flexible pricing, can include higher rates on certain vehicle types and can price different rates for more than a certain distance.
  • Set any curreny symbol.
  • Have upto 4 different Vehicle types.

Web Settings

  • Enter a custom Welcome message on the frontpage.
  • Option to allow customers to check price based on the Google Map.
  • Option to allow customers to book online based on Google Map.
  • Option to ask customers for there email.
  • Option to email customer confirmation email of booking.
  • Set the waiting for confirmation message.
  • Set the confirmation message.
  • Option to use cookies to remember customers.
  • Option to delete reservations older than a certain amount of time.

PayPal Settings

  • Option to allow customers to use PayPal.
  • Set the PayPal email address.
  • Set the Payment Currency type.
  • Set the custom title for the payment.

Grouped Common Locations

  • Add, edit, activate and delete Grouped locations such as common places like the Airports, Train Station, etc.
  • Add, set group, activate, edit and delete common locations.
  • Set the Fares for each common journey.

Bookings

  • View Future, Pending and Archived Bookings.
  • See and Set if the driver has gone the time, confirmed, from, to, any notes, phone number of customer, car type, price, customer’s payment.
  • Easily Edit or Delete any booking.
  • Easily add a booking from admin panel.

Files Included

  • Layered and Slided PSDs of the design includes for easy modification.
  • JavaScript file for Google Maps API, for everything including the price calculation of journey based on the Google Maps API’s results.