Steps to Configure Digital Ocean Droplet for Nodejs Application Deployment
Angularcode
- #nodejs
- #digital ocean
- #pm2
- #nginx
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 Digital Ocean Cloud droplets for this deployment. Digital Ocean is simple and easy to get started.
Here are the list of steps that we need, in order to complete the setup process
- Create and start a droplet
- Login to that droplet
- Install NodeJS
- Configure timezone
- Install Database (MongoDB)
- Setup nginx and build-essentials
- Copy project files
- Install node modules
- Setup & Start PM2
- Enable https
Create and start a droplet
Head to https://www.digitalocean.com and create a Digital Ocean account.Login to Digital Ocean 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 AddressOption-1 (Using password)
Copy that IP Address and in your command prompt type ssh root@YOUR_IP_ADDRESS
e.g. ssh [email protected]
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 [email protected]
Install NodeJS
Get the node source and install itsudo 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 applicationsudo dpkg-reconfigure tzdata # Add timezone as Kolkata
Install Database (MongoDB)
Create a list file for MongoDBecho "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](https://filezilla-project.org/) to upload our project files to /var/www/litekart folderInstall 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 eversudo 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 instructionssudo 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