DEV Community

Cover image for Complete Guide: Deploying Node.js Application on Ubuntu VPS
Sahinur
Sahinur

Posted on

Complete Guide: Deploying Node.js Application on Ubuntu VPS

A step-by-step guide to deploy your Node.js application with MongoDB, Nginx reverse proxy, and SSL certificate on a DigitalOcean Droplet or any Ubuntu VPS.

Prerequisites

  • A DigitalOcean Droplet (or any Ubuntu VPS)
  • A domain name (optional, but required for SSL)
  • SSH access to your server
  • Your Node.js application hosted on GitHub

Step 1: Connect to Your Server

SSH into your droplet using the root user:

ssh root@YOUR_DROPLET_IP
Enter fullscreen mode Exit fullscreen mode

Replace YOUR_DROPLET_IP with your actual server IP address.


Step 2: Update System Packages

Always start by updating and upgrading your system packages:

sudo apt update && sudo apt upgrade -y
Enter fullscreen mode Exit fullscreen mode

Step 3: Install Required Packages

Install Node.js, npm, and Nginx:

sudo apt install -y nodejs npm nginx
Enter fullscreen mode Exit fullscreen mode

Step 4: Install Node Version Manager (NVM)

NVM allows you to manage multiple Node.js versions easily.

Download and install NVM:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
Enter fullscreen mode Exit fullscreen mode

Load NVM into current session:

source ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

Install and configure Node.js 20.10.0:

nvm install 20.10.0
nvm use 20.10.0
nvm alias default 20.10.0
Enter fullscreen mode Exit fullscreen mode

Verify installation:

node --version
npm --version
Enter fullscreen mode Exit fullscreen mode

Step 5: Install MongoDB

MongoDB is a popular NoSQL database for Node.js applications.

Import the public key:

curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg --dearmor
Enter fullscreen mode Exit fullscreen mode

Create the list file for Ubuntu 22.04:

echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
Enter fullscreen mode Exit fullscreen mode

Update packages and install MongoDB:

sudo apt update
sudo apt install -y mongodb-org
Enter fullscreen mode Exit fullscreen mode

Start and enable MongoDB:

sudo systemctl start mongod
sudo systemctl enable mongod
sudo systemctl status mongod
Enter fullscreen mode Exit fullscreen mode

For complete MongoDB installation instructions, refer to the official documentation.


Step 6: Setup SSH Key for GitHub

Generate an SSH key to clone your private repositories:

ssh-keygen -t rsa -b 4096 -C "your-email@example.com"
Enter fullscreen mode Exit fullscreen mode

Press Enter to accept default file location and optionally set a passphrase.

Display your public key:

cat ~/.ssh/id_rsa.pub
Enter fullscreen mode Exit fullscreen mode

Add the key to GitHub:

  1. Copy the entire output
  2. Go to GitHub → Settings → SSH and GPG keys
  3. Click "New SSH key"
  4. Paste your key and save

Step 7: Clone Your Repository

Navigate to your preferred directory and clone your repository:

cd /var/www
git clone git@github.com:username/repository.git
cd repository
Enter fullscreen mode Exit fullscreen mode

Install dependencies:

npm install
Enter fullscreen mode Exit fullscreen mode

Step 8: Configure Nginx as Reverse Proxy

Nginx will handle incoming HTTP requests and forward them to your Node.js application.

Create a new Nginx configuration file:

sudo nano /etc/nginx/sites-available/your-app-name
Enter fullscreen mode Exit fullscreen mode

Add the following configuration:

server {
    listen 80;
    server_name YOUR_DOMAIN_OR_IP;

    location / {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_cache_bypass $http_upgrade;
    }
}
Enter fullscreen mode Exit fullscreen mode

Replace YOUR_DOMAIN_OR_IP with your domain name or server IP address.

Enable the configuration:

sudo ln -s /etc/nginx/sites-available/your-app-name /etc/nginx/sites-enabled/
Enter fullscreen mode Exit fullscreen mode

Test Nginx configuration:

sudo nginx -t
Enter fullscreen mode Exit fullscreen mode

Reload Nginx:

sudo systemctl reload nginx
Enter fullscreen mode Exit fullscreen mode

Step 9: Managing Your App with Tmux

Tmux keeps your application running even after you disconnect from SSH.

Tmux Commands Reference:

Command Description
tmux new -s session_name Create a new session
tmux attach -t session_name Attach to existing session
tmux ls List all sessions
tmux kill-session -t session_name Kill a session
tmux rename-session -t old_name new_name Rename a session
Ctrl+B then D Detach from current session

Start your application:

tmux new -s myapp
cd /var/www/repository
npm start
Enter fullscreen mode Exit fullscreen mode

Press Ctrl+B then D to detach and leave it running.


Step 10: Configure Firewall (UFW)

Enable and configure the firewall:

sudo ufw enable
sudo ufw allow 'Nginx Full'
sudo ufw allow OpenSSH
sudo ufw status
Enter fullscreen mode Exit fullscreen mode

Step 11: Install SSL Certificate (HTTPS)

Secure your application with a free SSL certificate from Let's Encrypt.

Install Certbot:

sudo apt install certbot python3-certbot-nginx
Enter fullscreen mode Exit fullscreen mode

Obtain SSL certificate:

sudo certbot --nginx -d example.com -d www.example.com
Enter fullscreen mode Exit fullscreen mode

Replace example.com with your actual domain name.

Certbot will automatically configure Nginx for HTTPS and set up auto-renewal.

Test auto-renewal:

sudo certbot renew --dry-run
Enter fullscreen mode Exit fullscreen mode

Quick Reference Commands

Server Management:

# Restart Nginx
sudo systemctl restart nginx

# Check Nginx status
sudo systemctl status nginx

# View Nginx error logs
sudo tail -f /var/log/nginx/error.log

# Restart MongoDB
sudo systemctl restart mongod

# Check MongoDB status
sudo systemctl status mongod
Enter fullscreen mode Exit fullscreen mode

Application Management:

# Pull latest changes
cd /var/www/repository
git pull origin main

# Install new dependencies
npm install

# Restart application (inside tmux session)
tmux attach -t myapp
# Press Ctrl+C to stop, then npm start
Enter fullscreen mode Exit fullscreen mode

Alternative: Using PM2 (Recommended for Production)

PM2 is a production process manager for Node.js applications.

Install PM2:

npm install -g pm2
Enter fullscreen mode Exit fullscreen mode

Start your application:

pm2 start app.js --name "your-app-name"
Enter fullscreen mode Exit fullscreen mode

PM2 Commands:

pm2 list              # List all processes
pm2 restart app-name  # Restart application
pm2 stop app-name     # Stop application
pm2 logs              # View logs
pm2 startup           # Configure PM2 to start on boot
pm2 save              # Save current process list
Enter fullscreen mode Exit fullscreen mode

Troubleshooting

Application not accessible:

  1. Check if the app is running: tmux attach -t myapp
  2. Verify Nginx config: sudo nginx -t
  3. Check firewall: sudo ufw status
  4. Review Nginx logs: sudo tail -f /var/log/nginx/error.log

MongoDB connection issues:

  1. Check if MongoDB is running: sudo systemctl status mongod
  2. Review MongoDB logs: sudo tail -f /var/log/mongodb/mongod.log

SSL certificate issues:

  1. Ensure domain DNS points to your server IP
  2. Check Certbot logs: sudo certbot certificates
  3. Renew manually: sudo certbot renew

Conclusion

Your Node.js application is now deployed with:

  • ✅ Latest Node.js version via NVM
  • ✅ MongoDB database
  • ✅ Nginx reverse proxy
  • ✅ SSL/HTTPS encryption
  • ✅ Firewall protection
  • ✅ Process management with Tmux or PM2

For production applications, consider implementing additional security measures, setting up automated backups, and configuring monitoring tools.


Last updated: January 2026

Top comments (0)