Deploying a Node.js App to DigitalOcean Server

Deploying a Node.js App to DigitalOcean Server

  • 2016-09-26
  • 1195

Hosting Node.js application to Digital ocean Server is very easy and cheap in cost. You can begin with $5 basic program to host your first Node.js application live on Server. If you willing to buy one click here to get $10 discount on your first Server with Digital ocean.

What we’re going to do

In this tutorial we are going to host Node.js application in our Digital ocean Server. Our application will be Google recaptcha form using Node.js which we have covered in previous tutorial.

This tutorial will not cover each and every basic steps require such as buying server, setting up basic Nginx Server etc because I believe folks at Digital ocean have already written enough tutorial on it.

This tutorial only focuses on “Hosting Node.js application on a sub-domain”.

Prerequisites

We are assuming you have your Digital ocean Server ready with Nginx installed or probably running your blog or website on same ( Like we are doing ). If you don’t have one and want to begin with, click here to buy your first digital ocean server with $10 discount.

Preparation

You need few things ready before you move to next step, they are :

  • Git
  • Node and NPM.
  • PM2

Login to your Server and install them if you don’t have already, else move on to next section.

Install git

Run following command

sudo apt-get install git

Install latest Node.js

Run following command one after another.

curl -sL https://deb.nodesource.com/setup_5.x | sudo -E bash -
sudo apt-get install -y nodejs

Install PM2

Run following command to install PM2 globally.

npm i -g pm2

Setting up project

Clone your project from Github.

git clone https://github.com/codeforgeek/google-racapcha-node

Switch to folder and install required dependencies.

cd google-racapcha-node && npm install

Hosting Node.js app

To host our application we need sub domain. Let’s create one from Digital ocean control panel.

Create sub domain

Go to digital ocean account then click on Networking and select View main domain.

Node.js hosting

Create new sub domain say google-recaptcha and put your IP in second box. For me it will be google-recaptcha.codeforgeek.com.

Node.js hosting

Create Nginx config and add new block of this code into your config file. This probably be located in /etc/nginx/conf.d/ directory.

server {
    listen 80;

    server_name sub-domain.domain.com;

    root /usr/share/nginx/html/node;
    index index.html index.htm;

    client_max_body_size 10G;

    location / {
        proxy_pass http://localhost:--PORT--];
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_buffering off;
    }
}

Replace –PORT– with the port number on which Node.js app is running.

Deploy app

Go to Node project folder and start it with PM2.

pm2 start app.js

Node.js hosting
Restart nginx by using following command.

sudo service nginx restart

Visit the URL to view the app. Our app is here.

Hosting Node.js app

Wrapping it up

This tutorial uses sub domain to deploy the Node.js application. However if you want to deploy on main domain, steps will be pretty much same.

Conclusion

Digital ocean is one of the cheapest and best hosting services for small-scale business. I am personally using it and I can recommend you this with confidence.

Suggest


Learn Nodejs by Building 12 Projects

The Complete Node JS Developer Course

Learn and Understand NodeJS

Build an Amazon clone: Nodejs + MongoDB + Stripe Payment

Angular 2 and NodeJS - The Practical Guide to MEAN Stack 2.0