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!
I bought it on Digital Ocean because I have 200$ free on this service, and I can also
I got a free one on namecheap.com, but you can also buy a domain on sites such as:
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 “
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)
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
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
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.
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
Also published here.