Deploying your Node.js application with OpenShift (Free-Plan)

Even wondering how to deploy your web-based Node.js application on internet? Sadly, I will tell to you that: You can’t do that inside your ordinary cpanel-ed web hosting because they mostly only support PHP. Yes, that very sad news because you can rent a web hosting as low as a buck per month.

So the solution is to deploy it inside a PAAS (Platform as a Service) provider which is one of Cloud Computing solution to deploy a web application with many kind of supported platform (such as Ruby, Python, Google Go, and even Java). Unlike ordinary cpanel based web hosting, your web doesn’t run inside shared machine with many other web application. But also unlike dedicated hosting, it doesn’t run your application in single physical machine because it run inside a sandboxed virtual machine which will isolated your application thus makes it not dependent each other.

There are some PAAS solution which provided by major company such as Google Apps Engine, Windows Azure (by Microsoft), OpenShift (by Redhat) and Amazon Web Service (a.k.a AWS). Some of them provided a free plan and all of them of course provided pay plan which unlike traditional web hosting, the plan is not only based on how much space you need but also how much the CPU power, network bandwidth you need to run your application.

Now in this post, I will tell you how to deploy your Node.js application inside Redhat’s OpenShift which has a free-plan (with maximum 3 application per account). Deploying Node.js application is very easy if you know how to make it easy.

Pre-Deployment

You need an online Git repository to host and hold your code. Come on, you can use Github for this purposes aren’t you?

Hold it, before you pushed your code and make it clear, you need this two step to make your application inside your OpenShift gear : Be sure you create a packages.json which tell you everything about your application and modify your server code regarding app.listen() part.

Your packages.json should look like this, mention all of your application’s dependency clearly because it will npm-ed automatically when you start your application, then mention how to start your script in script.start part and what is your main server script’s file name inside main part.

{
    "name": "amankata",
    "description": "amankata node.js edition",
    "private": "true",
    "version": "0.0.1",
    "dependencies": {
        "express": "3.x",
        "ejs": "~0.7.1",
        "socket.io": "0.9.16"
    },
    "scripts": {
        "start": "node server.js"
    },
    "main": "server.js"
}

Save it, then open server.js or another name depends on your configuration. Find the application listening part which is a line which indicating where your application will be listening to. It might look something like this :

app.listen(80);

Change the port it will be listening by reading it first from process.env.OPENSHIFT_NODEJS_PORT and the host simulcast address from process.env.OPENSHIFT_NODEJS_IP. That above code now look like this :

var port =  process.env.OPENSHIFT_NODEJS_PORT || 8080;   // Port 8080 if you run locally
var address =  process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1"; // Listening to localhost if you run locally
app.listen(port, address);

Save it, then push into your Git repository. Now copy your Git repository’s clone address and go to next step.

Register into OpenShift

You can register OpenShift for free from https://www.openshift.com/app/account/new. Complete the form, verify with your email and go to next step.

Add an Application

OpenShift Dashboard

After you verify your account, login into the OpenShift’s dashboard and do “Add Application”

OpenShift Add an Application

Scroll down in “Create a New Application” page and you will see “NodeJS 0.10 or 0.06″. Choose one of them to create a NodeJS gear. A gear is an environment which hold and deploy your application. If you not sure, choose NodeJS 0.10 which is the latest version.

Create a Node.js Application gear

Enter your public URL of your application (don’t worry, you can use your own domain later), your application’s git clone URL (Do paste into it) and scalability option you want. If everything’s OK, click “Create Application”.

Wait some seconds (It might take long time). And if there is no major issue, you can open your application in your chosen URL.

Running OpenShift application, which use my own domain

Using your own domain

AmanKata OpenShift Dashboard

If you want to use your own domain, open OpenShift’s Dashboard, click into your application and click “Change alias” beside your application domain name. Then input your domain you wish and do create alias.

Woa woa woa, it can’t run that easy. Remember to configure your domain server which parked your domain and add a CNAME record to redirect into your previous original domain.

Database

Add new cartridge inside your newly created application

If your application use database, then you need to create a database cartridge for your application. Open your application dashboard (remember, application specific dashboard), look at bottom of the page and choose your database cartridge which depend on what you use. OpenShift provided MySQL, PostgreSQL and even MongoDB solution to use.

I ordered MySQL for one
Cuplikan layar dari 2014-04-20 20:43:01

You will provided with something you need (like username, password) to connect into your database cartridge. Go change your code.

SSHing your PAAS (and modify your code via Git)

If you want to do SSH for administration purposes (such as starting/restarting/stopping your gear, MySQL command line, etc), you can register your SSH public key from “Settings” menu, and click “Add new key” inside “Public key” section.

SSH public key management

Don’t know how to create a pair of SSH public/private key, here is a command to do it :

ssh-keygen -t dsa

Your SSH’s public key in ~/.ssh/id_rsa.pub or ~/.ssh/id_dsa.pub. If you use Windows machine, then do it with your MSWIN/Cygwin (that usually installed within Git for Windows).

Now you can log into SSH (per application) from an address which given on sidebar of your application’s dashboard. Oh yeah, you can also do git-ing from git clone address provided in the same place.

My address to do git and ssh with newly created application. Yes, we don't need to pixelize that because the one who know my SSH private key is only me :D

1 Comments

  1. budyk_ir

    Ane udh run…

    tpi pas ane testing pake

    app.get(‘/test’, function(req,res){

    res.write(“tes openshift\n\n”);
    });

    di dlm server.js seperti dicontohkan….gk jalan” gan..ttp tampilan default

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Current ye@r *