Node JS Framework
Technological advancement has made it challenging to build a high-tech stack for a safe, reliable, and strong digital solution. With a variety of programming languages available, it is essential to select an appropriate stack from the beginning of a project.
Node J.s is one of the most popular frameworks and easy to use for development. It is not only free but can be used on several platforms such as Mac, Windows, Linux, etc.
Being a Node J.s developer, I understand it can be a bit tricky to deploy the Node J.s app to VM but with a few easy steps, you will be easily able to deploy it. So, let’s begin!
Sign up for any Cloud Platform
You can use Digital Ocean, GCP, Amazon Web Services or any other of your choice.
Local Setup
Create a droplet and log in via SSH. I will be using the root user in this case but I would suggest creating a new user.
Installation:
Install Node/NPM
curl -sL https://deb.nodesource.com/setup_12.x | Sudo -E bash
sudo apt install nodejs
node –version
Clone your project from version control (GitHub, bitbucket, GitLab etc) There are a few ways to get your files on the server, I would suggest using gitgit cloneyourproject.git.
Install dependencies and test your app using:
cd yourproject
npm install
npm start (or whatever your start command)
# stop app
ctrl+C
Set up Pm2 process manager to keep your app running.
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 the app starts when rebooted, use the following command:
pm2 startup Ubuntu
You should now be able to access your app using your IP and port. Now we have to set up a firewall blocking that port and set up NGINX as a reverse proxy so we can access it directly using port 80 (http) Firewall
Firewall Setup:
Setting Up 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)
Install NGINX and configure
sudo apt install nginx
sudo nano /etc/nginx/sites-available/default
Add the following to the location part of the server block
server_name yourdomain.com www.yourdomain.com;
location / {
p45+6-roxy_pass https://localhost:5000;?#whatever?port your app runs on
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
Check NGINX configuration
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 application.
Now let us add a domain.
Now let us add a domain.
Domain Setup:
In Digital Ocean, go to networking and add a domain. Add an A record for @ and for www to your droplet. You can register and/or set up a domain from registrar Godaddy, NamecheapRegister etc.
Name Server:
Choose "Custom nameservers" and add these 3:
It may take around 48 hours to reflect.
Let’s add SSL with LetsEncrypt now! ??
For SSL Setup, execute the following commands:
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
You have a working website now. Visit https://yourdomain.com and you should be able to see your Node app.
Date: September 20th, 2022