⚡ This is the AMP version of this page. View full version

Deploy Nodejs on digital Ocean

Published: 7/7/2025 | Updated: 6/7/2025
  1. generate ssh key
ssh-keygen
  1. copy the public key and paste in the digital ocean (the key can be found where you created the private key with a .pub extension)
  2. Create new user Creating new user
adduser username
  1. Give the user sudo privileges:
usermod -aG sudo username
  1. Switch to that user:
# Switch to another user (with their environment) 
su - username 
  1. Install nvm from https://github.com/nvm-sh/nvm
  2. nvm install 16
  3. install git
sudo apt install git-all
  1. clone your github repo (for private repos on AWS EC2, see Clone a Private Repo ( for EC2 or digitalOcean or any other VM))
  2. install the dependencies by npm i and copy your env variables.
  3. install a process manager to run the app.
npm i pm2@latest -g
  1. start your app using pm2
pm2 start entry_file
#example pm2 start index.js
 
# pm2 start "npm run dev" --name myAppName

Tip

to start your app on reboot

pm2 startup ubuntu 
  1. Now your api is live on droplet_id:port.

Additional steps

  1. setup ufw firewall
ufw enable 
ufw allow ssh && ufw allow http && ufw allow https
#check the status of firewall 
ufw status
  1. Install nginx reverse proxy to listen on default port
sudo apt install nginx
  1. edit the /etc/nginx/sites-available/default file. Find the server_name and location lines and replace with these
server_name yourdomain.com www.yourdomain.com;
 
location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        proxy_pass http://localhost:8000; #or your app 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;
}
# To run two sepearte backend on two different domain
server {
    listen 80;
    server_name abc.com;
 
    location / {
        proxy_pass http://localhost:3000;  # First backend on port 3000
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}
 
server {
    listen 80;
    server_name xyz.com;
 
    location / {
        proxy_pass http://localhost:4000;  # Second backend on port 4000
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}
 
  1. restart the nginx server
$ sudo /etc/init.d/apache2 stop
Stopping apache2 (via systemctl): apache2.service.
$ sudo systemctl restart nginx
$ sudo service nginx restart
  1. install a free ssl certificate form Let’s Encrypt
# Install Certbot
sudo snap install --classic certbot
 
# Prepare the Certbot command
sudo ln -s /snap/bin/certbot /usr/bin/certbot
 
# Run this command to get a certificate
sudo certbot --nginx
 
# Test automatic renewal
sudo certbot renew --dry-run