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
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
Step 3: Install Required Packages
Install Node.js, npm, and Nginx:
sudo apt install -y nodejs npm nginx
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
Load NVM into current session:
source ~/.bashrc
Install and configure Node.js 20.10.0:
nvm install 20.10.0
nvm use 20.10.0
nvm alias default 20.10.0
Verify installation:
node --version
npm --version
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
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
Update packages and install MongoDB:
sudo apt update
sudo apt install -y mongodb-org
Start and enable MongoDB:
sudo systemctl start mongod
sudo systemctl enable mongod
sudo systemctl status mongod
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"
Press Enter to accept default file location and optionally set a passphrase.
Display your public key:
cat ~/.ssh/id_rsa.pub
Add the key to GitHub:
- Copy the entire output
- Go to GitHub → Settings → SSH and GPG keys
- Click "New SSH key"
- 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
Install dependencies:
npm install
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
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;
}
}
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/
Test Nginx configuration:
sudo nginx -t
Reload Nginx:
sudo systemctl reload nginx
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
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
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
Obtain SSL certificate:
sudo certbot --nginx -d example.com -d www.example.com
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
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
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
Alternative: Using PM2 (Recommended for Production)
PM2 is a production process manager for Node.js applications.
Install PM2:
npm install -g pm2
Start your application:
pm2 start app.js --name "your-app-name"
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
Troubleshooting
Application not accessible:
- Check if the app is running:
tmux attach -t myapp - Verify Nginx config:
sudo nginx -t - Check firewall:
sudo ufw status - Review Nginx logs:
sudo tail -f /var/log/nginx/error.log
MongoDB connection issues:
- Check if MongoDB is running:
sudo systemctl status mongod - Review MongoDB logs:
sudo tail -f /var/log/mongodb/mongod.log
SSL certificate issues:
- Ensure domain DNS points to your server IP
- Check Certbot logs:
sudo certbot certificates - 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)