×


Deploy Node js Application to DigitalOcean Server

Are you trying to Deploy Node.js Application to DigitalOcean Server?

We can help you.


Node. js is used to build robust, scalable apps and has the capability to handle thousands of requests.

It is very efficient when it comes to web and enterprise applications that have a lot of input-output operations that query the database.

Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to deploy node.js application to Digital ocean droplets.

In this context, we shall look into steps to deploy node.js applications.


How to Deploy Node.js Application to DigitalOcean Server ?

Applications are written in JavaScript and runs on platforms such as Linux, OS X, FreeBSD, and Windows. Node.js also helps to manipulate files programmatically with the built-in fs module.

Though Node.js applications can be run at the command line we can also use it to automatically restart on reboot or failure of service.


Take the following steps to do this:


1. Building a simple NodeJS application:

Following are the commands for creating a sample node.js :

mkdir sample-nodejs-app
cd sample-nodejs-app
npm init -y
npm install express
touch index.js

After that open index.js and paste the following code into it:

// index.js
const express = require('express')
const app = express()
app.get('/', (req, res) => {
res.send('Hey, I\'m a Node.js app!')
})
app.listen(3000, () => {
console.log('Server is up on 3000')
})

We can start the application with the following command:

$ node index.js

And we can access it from http://localhost:3000.

We can see "Hey, I’m a Node.js app!" as output.


2. Creating a New Droplet

First, we will log in to the Digital Ocean account and create a new droplet (server).

We will select NodeJS from the One-click apps.

Next, we will choose the $10 plan which will allow us to install the NPM dependencies as it needs at least 1GB RAM for installing the dependencies.

We will set the datacenter region with the default one.

After that, we can either add a new SSH key or choose from the existing one we have added and then choose a hostname for the droplet and click the Create button.

The new server can be seen up and running on Ubuntu 16.04 and NodeJS version 6.11.2. We have to note down the IP address of the newly created server to access the server using it.


[Need urgent assistance to create a new Droplet? We can help you! ]


3. Creating Non-root User on the Digital Ocean Server.

First, we will to login to the server as root by using the IP address we got.

$ ssh root@SERVER_IP_ADDRESS

Next, we will create a new user:

$ adduser ibmimedia

To give the user bob administrative privileges we will use the below sudo command:

$ usermod -aG sudo ibmimedia

This will add the user ibmimedia to the sudoers group and will allow the user to run commands with superuser privileges.


4. Setup SSH Key For The Newly Created User:

For setting up the SSH key for bob we need to copy the public key to the new server.

We can use the following command:

$ cat ~/.ssh/id_rsa.pub

Then copy it to add the public key to a special file in bob’s home directory.

Before that switch to user bob from the root with the following command.

$ su - ibmimedia

Next, we need to create a new directory called .ssh and restrict its permission:

$ mkdir ~/.ssh
$ chmod 700 ~/.ssh

Inside the .ssh directory, we will create a new file called authorized_keys with the following command:

$ touch ~/.ssh/authorized_keys

Then open the file and paste the public key into the file and set the permissions right:

$ vim ~/.ssh/authorized_keys
$ chmod 600 ~/.ssh/authorized_keys

For returning to the root user we can use the following command:

$ exit

We can ensure access to the server with the new user by using the following command:

$ ssh ibmimedia@SERVER_IP_ADDRESS

5. Clone Node.js application to Digital Ocean Server

We will clone the node.js app on to the server directly in the user’s home directory (/home/ibmimedia) with the following command:

$ git clone https://github.com/ammezie/sample-nodejs-app.git


To install the dependencies give the following commands:

$ cd sample-nodejs-app
$ npm install

We will test the app to make sure everything is working as expected with the help of the following command:

$ node index.js

We can open a new terminal (without exiting the server) and enter the following command:

$ curl http://localhost:3000

The output "Hey, I’m a Node.js app!" can be seen.


Finally, the node.js application will be seen up and running.


[Need urgent assistance to Deploy Node.js App? We are happy to help you! ]


Conclusion

This article will guide you on how to  deploy node.js application to #DigitalOcean Server.  

To Deploying #Node . js application to DigitalOcean — Setting up the #server:

1. Getting familiar with Digital Ocean and creating your first #droplet.

2. Setting up #SSH and connecting to your server from your local #machine.

3. Setting up a simple Express.js web-server in local machine.

4. Using #Git to deploy your code to server.