Deploy Node Application in Digital Ocean
Today let me go through the process of setting up nginx server in a droplet in digital ocean to deploy the node application. This process can be followed to deploy other applications as well. We need several things for this setup.
1. Domain, Digital Ocean account and a droplet in it.
2. Github repo for the Source code.
3. SSH setup in your local device.

When you have above required things with you, then you are good to go for the further process.
Setting up droplet and repo
Login to your digital ocean account and create a droplet with basic plan. For the basic plan, 1 cpu, 2 GB RAM will work, otherwise we can increase the spec anytime in the future.
Open the console from the droplet. Check if there is ssh already setup. If ssh is not setup then run ssh-keygen -t rsa -b 4096
in the console to generate new ssh key. This process is required only if you need to access the git remote repo from the server.
Now create a desired folder to clone the repository of the source code. Make sure you have setup ssh key if your droplet to the remote github settings. Clone your repository inside the droplet.git clone git@github.com:username/repository.git
Install necessary dependencies
Since we are setting up the node application, we need to install node in the droplet. If you are deploying other applications like laravel, you need to install php. To install node, run following commands in the console.
sudo apt update
sudo apt install curl
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install nodejs=20.13.1–1nodesource1
Check for the required node version details in node official documentation if you need specific version. Verify the node installation.
node -v
npm -v
Install and run application
After installing node, we can install the application by using the command npm install
. Now we have several options to run the application. One is to run the application using plain node command like node index.js
. Another one and the popular one is using PM2 which is a popular package manager for node applications. I prefer to use pm2 for several reasons like monitoring, logging and easiness in starting, pausing, restart and deleting of the application.
Run this command to install pm2 globally, npm install -g pm2
.
Now run the application using below commandPORT=3000 pm2 start npm -- name “app-name” -- run start
Make sure the script name used is correct one. Here we are using start command.
Once it is started, we can check the logs of app using pm2 log
. Please check the pm2 documentation to know about other useful commands.
After running the command, your node application is running in the droplet, but it is not accessible from outside world. So for that we need to set up vhost in the nginx server.
Setting up nginx server
Run following commands to setup nginx server.
sudo apt update
sudo apt install nginx
sudo systemctl start nginx
sudo systemctl enable nginx
sudo ufw allow 'Nginx Full'

Now you can access your droplet through the IP address and it will land you the default nginx homepage.http://your_droplet_ip
In order to point the domain name that you have to the application running in the droplet, you need to create a vhost config file inside nginx.
Setting up vhost
In the terminal go to the nginx folder and create a new file with the name of your preferred domain.cd /etc/nginx/sites-available
nano example.com
Copy below code into the file and save. Be sure to update the port with the port number you used when running application using pm2.
server {
# Listen on port 80 for HTTP requests
listen 80;
server_name example.com www.example.com;
# Redirect HTTP to HTTPS (optional, if you have SSL set up)
# return 301 https://$host$request_uri;
# Proxy requests to the Node.js application
location / {
proxy_pass http://localhost:3000; # Node.js server port
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-NginX-Proxy true;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_cache_bypass $http_upgrade;
}
}
Enable the site:sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
Check the nginx configuration status with command nginx -t
. If everything is good then it should show result like this.

If it is not successful then check the vhost config file and fix the errors. Now restart the nginx server using command sudo systemctl reload nginx
.
Now the domain should be pointing to the application.
You can also setup SSL for free using certbot. Follow the commands to configure the certbot SSL.
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d example.com -d www.example.com
sudo certbot renew --dry-run # to renew certificate automatically
Feel free to drop a comment. Thank you and Enjoy.