paint-brush
Deploying a Node.js App with an Associated Domainby@zhadan
822 reads
822 reads

Deploying a Node.js App with an Associated Domain

by Anatolii ZhadanOctober 24th, 2023
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

This comprehensive guide walks you through deploying a Node.js app and associating it with a domain. It covers the entire process, including setting up a VPS, configuring Nginx, adding HTTPS for security, and ensuring your Node.js app is accessible via your domain. Detailed commands and explanations are provided for each step. Whether you're a beginner or experienced developer, this guide helps you successfully deploy your Node.js app.
featured image - Deploying a Node.js App with an Associated Domain
Anatolii Zhadan HackerNoon profile picture


In this article, I’ll show you how to deploy a Node.js app and associate it with a domain.


Let’s get right to it!


1. Buying everything we need:

Server (VPS):

I bought it on Digital Ocean because I have 200$ free on this service, and I can also give you 200$, but you can also buy a VPS on sites such as:

  1. hostinger.com
  2. contabo.com

Domain:

I got a free one on namecheap.com, but you can also buy a domain on sites such as:

  1. hostinger.com
  2. godaddy.com
  3. domain.com


2. Setting up a VPS:

Server settings:

The server settings depend on the load your site will be under and the complexity of the project. Since I will be deploying a simple site, I chose the most minimal settings.



For the authentication method, I chose a password.


Once our server is up and running, we can start connecting to it.


Copy the IPv4 or IPv6 address, then open a terminal and enter the following command:

Ssh [email protected](instead of 164.90.218.8 put your IPv4)



After this, it will ask you to enter the password that you set when creating the VPS:

When you successfully log in, you will see something similar to this:



From there, we have two ways to transfer our project folder. If you have it in a zip file on your local computer, you can use these commands to transfer the zip file with your project to the server.


#if you have authentication method thought password
scp your-archive.zip [email protected]:~/
#if you have authentication method thought ssh key
scp -i ~/.ssh your-archive.zip [email protected]:~/


However, I suggest that you use GitHub.


Log in to GitHub and use “git clone” to put your project in the global directory. For example, I will use my Node.js project called “qr_code_hunt_challenge”.



Then, we need to install packages. Use the following commands on our VPS:

#to update everything
sudo apt update
#after it shows us that we have packages that we can upgrade
sudo apt upgrade
#when you get Do you want to continue? [Y/n], you just write Y


After this, go to the git repository using this link and choose the version of Node.js you need. In my case, I will use the latest LTS version.


Press the copy button next to the commands and insert them in our VPS:




If you encounter any errors:

sudo apt-get update
sudo apt-get install -f
sudo apt-get clean
sudo apt-get install -y nodejs
sudo apt


Then:

#to install vim, zip, nginx
sudo apt install vim zip nginx


Now, we need to make some changes in the firewall settings:

sudo ufw allow 'OpenSSH'
sudo ufw allow 'Nginx HTTP'
sudo ufw enable 
#after all this commands you need to write
sudo ufw status
#and you will get something like this
Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere                  
Nginx HTTP                 ALLOW       Anywhere                  
OpenSSH (v6)               ALLOW       Anywhere (v6)             
Nginx HTTP (v6)            ALLOW       Anywhere (v6) 



3. Setting up a domain:

First, we need to input our domain on the VPS hosting service. In my case, it is DigitalOcean. Go to the dashboard where you can check your VPS and start:


When you insert the domain name and click “Add Domain,” you need to point to DigitalOcean in your nameservers. You can follow the guide from DigitalOcean using this link.


In my case, I go to my dashboard in Namecheap and add everything as instructed in the guide:


Second, we need to create a directory:

sudo mkdir /var/www/your_domain.com
sudo chown -R $USER:$USER /var/www/your_domain.com/
sudo vim /etc/nginx/sites-available/your_domain.com


The last command will open Vim, and here we need to insert this Nginx configuration:

server {
    server_name your_domain.com www.your_domain.com;

    location / {
        proxy_pass http://localhost:3000;#here you need to change to your port
        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;
    }
}


Then, use these commands:

rm -rf /etc/nginx/sites-available/default
rm -rf /etc/nginx/sites-enabled/default
sudo ln -s /etc/nginx/sites-available/your_domain.com /etc/nginx/sites-enabled/your_domain.com
#by next command you can check if everything okay
nginx -t
#now we need to reload our nginx
nginx -s reload


Now, we need to move our Node.js project to another directory:

cd /var/www/your_domain.com
mv ~/name_of_your_project_folder 


After using “ls,” you will see the folder or “.zip” file in your directory. If you just moved the “.zip” file, you need to unzip it using the command “unzip your_file.zip.”


Now, we need to move all files from this directory:

mv name_of_your_project_folder/* .
#and now we can delete this directory
rm -rf name_of_your_project_folder


After that, use these commands:

npm install
#and if you need some packages you can install now, in my case i will use:
npm init
npm install express
npm install ejs
npm install path
#and when you installed everything that you need, you can start your node.js
node server.js




To make this site work without stopping:

sudo npm install pm2@latest -g

pm2 startup systemd

pm2 start /var/www/your_domain.com/server.js

pm2 save

pm2 status
#after last command you should see 




If everything is okay, start using these commands:

sudo systemctl start pm2-root

sudo systemctl reboot



4. Setting up HTTPS:

To use HTTPS on our app, use these commands:

sudo ufw allow 'Nginx Full'

sudo apt install certbot python3-certbot-nginx

sudo certbot --nginx -d your_domain.com -d www.your_domain.com

sudo systemctl status certbot.timer

sudo certbot renew --dry-run

sudo ufw delete allow 'Nginx HTTP'

#and if you upload some file on your app
sudo vim /etc/nginx/nginx.conf
#and here you need to insert in http {}
client_max_body_size 1000M; #here you can input any amout of megabytes

#and after all this
 nginx -t && nginx -s reload




Now, our Node.js app is deployed and reachable through our domain, and everything is working.



Finishing Up

I hope you understood the instructions correctly. If you have any thoughts on this article, I will be waiting for your comments. If I really helped you, I kindly ask you to subscribe to my LinkedIn and provide feedback on this article.


Also published here.