DEV Community

Swadesh Behera
Swadesh Behera

Posted on

Steps to Configure Vultr for Nodejs Deployment

vultr

I finished building my NodeJS app. Next step is to deploy it to some cloud server.
In this tutorial I will guide you how to setup and deploy nodejs application for production to ubuntu cloud. 
We will be using Vultr cloud for this deployment. Vultr is simple and easy to get started.

Here are the list of steps that we need, in order to complete the setup process

  1. Create and start a droplet
  2. Login to that droplet
  3. Install NodeJS
  4. Configure timezone
  5. Install Database (MongoDB)
  6. Setup nginx and build-essentials
  7. Copy project files
  8. Install node modules
  9. Setup & Start PM2
  10. Enable https

Create and start a droplet

Head to https://www.vultr.com and create a Vultr account.

Login to Vultr panel and create the droplet. for a nodejs project it is recommended to go for 2GB RAM droplet

Choose a region closer to your target audience location
Select UBUNTU 18.x as the operating system

Login to that droplet

The droplet creation will take somewhere between 1–3 minutes. Upon completion, it will be assigned with an IP Address

Option-1 (Using password)

Copy that IP Address and in your command prompt type ssh root@YOUR_IP_ADDRESS
e.g. ssh root@100.100.0.100
Enter the password and you must be logged in as root

Option-2 (Using a public/private key pair)

e.g. ssh -i D:/ssh/key-file.ppk root@100.100.0.100
key-file.ppk is your private key

Install NodeJS

Get the node source and install it

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

Configure timezone

This is essential when you deal with dates in your application

sudo dpkg-reconfigure tzdata # Add timezone as Kolkata

Install Database (MongoDB)

Create a list file for MongoDB

echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.2.list

Install

sudo apt-get update
sudo apt-get install -y mongodb-org

Start MongoDB Service

sudo service mongod start

Edit the mongod.config

sudo nano /etc/mongod.conf

Change line 23 to bindIp: 0.0.0.0

sudo service mongod restart

Setup nginx and build-essentials

Why nginx?

We will use nginx as a reverse proxy as well as a server to serve static files. This gives us advantage like 

  • https
  • caching
  • abstraction
  • speed
  • reduces nodejs load
  • load balancing
sudo apt-get install nginx -y
sudo service nginx restart

Install build-essentials

sudo apt-get install build-essential -y

Copy project files

We can use Filezilla to upload our project files to /var/www/litekart folder

Install node modules

Navigate to /var/www/litekart and run

npm i --production

Setup PM2

Why PM2?

In production environment we are going to use PM2. Because NodeJS commands are valid for that terminal session and kills the server once terminal session ends. To overcome this issue we are going to use a process manager that will demonize the node process and will keep it running for ever

sudo npm install -g pm2
sudo pm2 --name litekart start npm -- start

The above will install PM2, execute npm start command of the project and start a PM2 process named litekart

Enable https

Our NodeJS app is up and running. Now we need to enable https which is a mandatory requirement now a days. We are going to use a free https certificate from Certbot. Run the following command and follow the instructions

sudo certbot --nginx certonly

The installed certificate is valid for 6 months. We need to setup auto renewal of the https certificate through cron job

crontab -e

Enter the following at the end

@monthly /home/ubuntu/letsencrypt/letsencrypt-auto certonly --reinstall --nginx -d www.litekart.in && sudo service nginx reload

This article was originally published on The Angularcode.

Oldest comments (0)