wesupport

Need help?

Our experts have had an average response time of 13.14 minutes in February 2024 to fix urgent issues.

We will keep your servers stable, secure, and fast at all times for one fixed price.

How to Deploy Node.js Application to DigitalOcean Server

by | Jan 24, 2021

Wondering How 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.

At Bobcares, we often get requests to deploy node.js application to Digital ocean droplets as a part of our Server Management Services.

Today let’s see the steps our Support Engineers follow to do the deployment.

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.

Deploy Node.js Application to DigitalOcean Server

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.

The steps that ou Support Engineers follow are given below:

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 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 bob

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

$ usermod -aG sudo bob

This will add the user bob 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 - bob

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 bob@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/bob) 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 assistance to Deploy Node.js App? We are happy to help you!]

 

Conclusion

To conclude, we saw how to deploy node.js application to DigitalOcean Server.  Also, we saw the steps our Support Engineers follow to implement this.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

var google_conversion_label = "owonCMyG5nEQ0aD71QM";

0 Comments

Submit a Comment

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

Categories

Tags