Deploy your NodeJS application to a Digital Ocean Droplet || Step by Step Guide

Ikdem Ben Mbarek
Nerd For Tech
Published in
7 min readFeb 2, 2021

Getting Started

As a developer, most probably you asked yourself the very famous question after finishing the design and development of your application: “Now what? How can I make it accessible for users?”. Well, you still can do that by running your application locally on your machine and give your IP address for people to access it from their browsers. Yet, this isn’t the best option considering that your will need to turn off your machine every now and then. The second and best option is to have another machine that is always running. That’s exactly what we’re going to do today with Digital Ocean.

To be able to follow this guide, I’m assuming that you are familiar with basic Linux commands (since we are going to put them to use), that you have a ready-to-use Nodejs application and most importantly that you have a github account with a repo containing that application.

First things first, Digital Ocean is a cloud provider that allows us to rent servers and deploy our applications on them. This is basically what we call “Infrastructure as a Service” or IaaS in short. It’s when, instead of buying our hardware to deploy our applications like it was back in the first days of the web, we could go to a cloud provider, such as AWS, Azure, or Google Cloud and rent from them the hardware we need. This way, we will only pay for the resources we need. If, someday, we’ll need more, we can rent more. That’s what we call “pay as you go”.

Digital Ocean can be a good alternative for developers. It allows you to build more and spend less time managing your infrastructure with our easy-to-use control panel and API.

Step 1: Create a Droplet on Digital Ocean

First thing to do is to create an account on Digital Ocean. For that, you’ll need to provide them with your credit card details. This is just a regular checking and they won’t charge you for it.
There is a way for you to have a 100$ Free Credit upon sign up that will last for 2 months. This will be a good boost for you to start with the cloud.

Follow this link to get the 100 dollars credit

Let’s now create our first droplet. This will be the remote machine that we talked about. It’s somewhere inside a data center that can be hundreds of miles away from your home. Yet, you can access it anywhere and at anytime.

You should click on create on the top right and choose Droplets.
For the purpose of this tutorial, I’ll choose Ubuntu as my Distribution and I will go with the Basic shared CPU plan coming with the cost of 5 dollars a month. Then, you’ll need to choose a datacenter region and choose an authentication method. It’s more secure to go with SSH keys. But if you do not know how to generate one, these links will guide you to do it on Linux and Windows.

Step 2: Connect to the Droplet

Congratulations! You just created your first droplet. But this droplet is empty for now. If you navigate to the IP address of the droplet with your browser you will find that nothing lays there. Let’s go ahead and deploy our application!

We’ll need to connect to our droplet via SSH. To do that, let’s open our Terminal, or our cmd and type

ssh root@ip_address

Root is a default super user created within the droplet. The IP Address that we need to provide is the address on which our droplet was created.

If you see that #, this means that you’re now inside the terminal of your droplet with the power of the root user. Now, let the fun begin!

Step 3: Install NodeJS

Now, we’ll start installing the software needed for our application. Of course, first thing that comes to mind is nodeJS since it’s the running environment that our application needs. In this tutorial, I’ll use nvm (Node Version Manager) to choose which version to install. This way, we’ll make sure we won’t have a mismatch between the version on our local machine and the droplet. Run this command to install nvm with curl.

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash

Then, run this command to update your bash commands and have access to nvm. Then, you can make sure that nvm was successfully installed.

~# source ~/.bashrc~# nvm --version

Now, we can install NodeJS with ease. I’ll install the v14.15.4 version of node since it’s the last one with LTS (Long Time Supprt) and is the one that I used to develop my application on my local machine. You can then run node -v and npm -v to check that the correct versions were installed.

~# nvm install 14.15.4
~# node --version
~# npm --version

Step 4: Clone your repository from Github

With nodeJS and npm installed on the droplet, we can go ahead and bring our code from the github repository and run it. Git is installed by default on the droplet, so we can simply run git clone from the terminal.

~# git clone https://github.com/{ownerName}/{repoName}.git

Of course, you need to substitute the link with the correct link of your repository.
If this was successful, we can find our application in a local directory. Let’s navigate to that directory using cd (Change Directory) and run npm install inside of it to install all the dependencies inside of the package.json file that our application needs.

~# cd ./directory-name
~# npm install

Now, if we run npm start (assuming that we defined a script for start in our package.json), the application will start running on the port we specified in our server.
Meaning that if we navigate to the droplet’s ip address with the port our application is running on, we’ll find our application! Hurray!
But wait, is the user supposed to enter the port number with the url? 😮
Furthermore, how to avoid terminating the application when I close the terminal on my local machine?

This is what we’re gonna answer in the two upcoming steps! 🚀

Step 5: Adding pm2

pm2 is a very great production process manager for nodeJS. It will allow our application to keep running even if we close our terminal. Let’s start by installing it as a global dependency.

~# npm i -g pm2

Now we can start our application with pm2 instead of node and nodemon.
In my case, my starting command is going to run “node ./server.js”. So, to start my application with pm2, I’ll need to run:

~# pm2 start ./server.js

Now, even if you close your terminal, the application will still be running and accessible for all the users anytime. Now, it’s time to fix the problem we have the port.

Step 6: Adding and configuring Nginx

In order to make our application available without specifying the port number, we are going to use a load balancer called Nginx.
Let’s start by installing it to our droplet, the same way we install it mvn and nodeJS:

~# apt install nginx
~# apt update

With Nginx installed, if we navigate to our ip address now, we will stumble upon this page. That’s because we didn’t define our default port for nginx.

To do that, we need to edit the configuration file of nginx.
I’ll use to use nano editor for that (which is installed by default in ubuntu)

~# nano /etc/nginx/sites-available/default

Once inside the file, we should go to location and provide it with these lines:

location / {
proxy_pass http://localhost:8000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}

Notice that my application will be running on localhost:5000. You’ll need to change that according to the port number you have in your server.

Now, we’ll need to test our configuration file with the new updates

~# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

If you get that output, this means that your syntax is correct. Yet, in order for the changes to take place, we need to restart nginx.

~# systemctl restart nginx

Now, your droplet is ready to go! If you navigate to the ip address of your droplet again, your application will be there.

We’re done 🎉

Phew! it wasn’t the easiest thing to do. Yet, it was totally worth it because now your application is online and you can invite users to interact with it!

This can be your first step in a very long trip.

Bare in mind there are simpler ways of doing this with containers and ready to use droplets. We can cover them in future Blogs maybe …

--

--