Deploy Nodejs app on Digital Ocean

Digital ocean is one of the best cloud providers out there its flexibility allows easy deployments and proper management of your online system and don't get me started with hoe easy it is to deploy a Dockerized app and i feel like it was built for micro services using it with k8s has eased the pain of deployment and all this at a cheep price
Steps to deploy a Node.js app to Digital Ocean using PM2, NGINX as a reverse proxy and an SSL from Lets Encrypt
1. Sign up for Digital Ocean
If you use the referral link below, you get $10 free (1 or 2 months) https://m.do.co/c/995600f29635
2. Create a droplet and log in via ssh
To gain the most from this i would suggest getting the Ubuntu version 18
I will suggest creating a new user for your app and add them to the sudo group.
To access your server use ssh
ssh root@ip
Don`t have ssh key pairs just do this in your terminal
You can generate your SSH Keys using the following commands on your terminal,
ssh-keygen
You will then be prompted to save and name the key as follows,
Generating public/private rsa key pair. Enter file in which to save the key (/Users/USER/.ssh/id_rsa):
Hit the Enter or Return key.
Next you will be asked to create and confirm a passphrase for the key as follows,
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
This will generate two files, by default called id_rsa
and id_rsa.pub
.
To get the contents of your .pub file run the following command in your terminal,
cat ~/.ssh/id_rsa.pub
Copy and Paste the contents (SSH Key) of your .pub file in the SSH Setup of your droplet , name your public SSH Key and choose Add SSH Key.
creating a new user and adding them to sudo group then switching to the new user
adduser newuserusermod -aG sudo newusersu - newuser
or
sudo adduser newusersudo usermod -aG sudo newusersu - newuser
3. Install Node/NPM
install node js it will install with NPM package manager the code below will ensure you install the latest node js version
curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
sudo apt install nodejs
node --version
4. Clone your project from GitHub
There are ways to get your files on to the server, but I would suggest using Git ant be sure to have pushed your code to GitHub then copy the link to your repo and clone it
git clone yourproject.git
5. Install dependencies and test app
Once you have cloned your repo to the Ubuntu server navigate to the folder and install all the dependencies
cd yourproject
npm install
npm start (when you have a start script in your package.json or whatever your start command)
# stop app
ctrl+C
6. Setup PM2 process manager to keep your app running
We need to now ensure that our app keeps running even when the sever goes down and comes back up we need to ensure that if any error occurs the app does not crush with no logs and i found out that it sometimes restarts our app when it crushes but take it with a pinch of salt
sudo npm i pm2 -g
pm2 start app (or whatever your file name)# Other pm2 commands
pm2 show app
pm2 status
pm2 restart app
pm2 stop app
pm2 logs (Show log stream)
pm2 flush (Clear logs)# To make sure app starts when reboot
pm2 startup ubuntu
You should now be able to access your app using your IP and port. Now we want to setup a firewall blocking that port and setup NGINX as a reverse proxy so we can access it directly using port 80 (http)
7. Setup ufw firewall
sudo ufw enable
sudo ufw status
sudo ufw allow ssh (Port 22)
sudo ufw allow http (Port 80)
sudo ufw allow https (Port 443)
8. Install NGINX and configure
We use nginx as a reverse proxy so that we can access our app from port 80 which is the default port for HTTP and it will some sort of redirect us to our server on port 3000 or whatever port you use
sudo apt install nginxsudo nano /etc/nginx/sites-available/default
Add the following to the location part of the server block and add the domain name to
server_name yourdomain.com www.yourdomain.com; location / {
proxy_pass http://localhost:3000; #whatever port your app runs on
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;
}# Check NGINX config
sudo nginx -t# Restart NGINX
sudo service nginx restart
You should now be able to visit your IP with no port (port 80) and see your app. Now let’s add a domain so as to access our app via the domain name because as at now we cannot access our app via the domain name
9. Add domain in Digital Ocean
In Digital Ocean, go to networking and add a domain
Add an A record for @ and for www to your droplet


Register and/or setup domain from registrar
I use Truehost for domains.
Choose “Custom nameservers” and add these
- ns1.digitalocean.com
- ns2.digitalocean.com
- ns3.digitalocean.com
It may take a bit to propagate you may ask where do i locate the name servers ? Well in truehost locate your domain and click manage then on the manage page you will find a menu and you can locate name servers click and you will be redirected to the name servers page and you can do the rest or an easier option is to seek help from them support
- Add SSL with LetsEncrypt
- This will work well with Ubuntu version 18 i found out the hard way
sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update
sudo apt-get install python-certbot-nginx
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com# Only valid for 90 days, test the renewal process with
certbot renew --dry-run
Now visit https://yourdomain.com and you should see your Node app