πŸš€ Complete Guide to Deploy a Node.js App with Nginx, MongoDB, PM2, and SSL on Ubuntu

June 18, 2020 (4y ago)

Deploying a Node.js application can feel overwhelming at firstβ€”but with a clear step-by-step process, you’ll have a scalable and secure app running in no time. This guide walks you through creating a DigitalOcean droplet, setting up your server, and deploying your Node.js project with essential tools like MongoDB, PM2, Nginx, Redis, and Let's Encrypt SSL.

🧱 1. Create Your Droplet and Connect via SSH First, spin up a new Ubuntu droplet on DigitalOcean or any cloud provider of your choice.

Once it's created, SSH into your server:

ssh root@your-server-ip
 

πŸ”§ 2. Update the System Keep your system up to date with the latest packages:

 
sudo apt update
sudo apt -y upgrade
sudo apt-get update
 

πŸ“¦ 3. Install Node.js Using NVM Node.js is the runtime environment for running your backend JavaScript code. We'll use NVM (Node Version Manager) to install it:

 
sudo apt-get install build-essential libssl-dev
curl -sL https://raw.githubusercontent.com/creationix/nvm/v0.33.5/install.sh -o install_nvm.sh
bash install_nvm.sh
source ~/.profile
 

Install Node.js

nvm install <nodejsVersion>  # Replace with version like 18, 20, etc.

🌐 4. Install and Configure Nginx Nginx is a powerful web server used to serve your app and reverse proxy incoming requests to your Node.js backend.

Install Nginx

 
sudo apt-get install nginx -y
 
 

Set Up Nginx Configuration

 
sudo rm /etc/nginx/sites-available/default
sudo nano /etc/nginx/sites-available/default
 

Paste the following configuration (replace with your domain):

server {
    listen 80;
    server_name tech.ohmkhay.com;
    location / {
        proxy_pass http://localhost:2000;
        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;
    }
}
 

Test and Reload Nginx

 
sudo nginx -t
sudo systemctl reload nginx
 

πŸ—ƒοΈ 5. Install and Configure MongoDB MongoDB is a NoSQL database used to store application data.

Install MongoDB

 
sudo apt-get install mongodb -y
sudo apt-get update -y
sudo service mongodb start
sudo service mongodb status
 

Create a Database and User

 
mongo
use ssbudb
 
db.createUser({
  user: "khay_tech",
  pwd: "KHAY_DB_2020",
  roles: [
    { role: "readAnyDatabase", db: "admin" },
    "readWrite"
  ]
});
 

Enable Auth in MongoDB

 
sudo nano /etc/mongodb.conf
 

Uncomment or add:

auth=true
 

MongoDB Connection URI

 
mongodb://khay_tech:KHAY_DB_2020@localhost:27017/khaydb
 

βš™οΈ 6. Run Your Node.js App with PM2 PM2 is a process manager that ensures your app runs continuously.

 
pm2 start npm --name "api" -- start
# OR for dev
pm2 start "npm run dev" --name myAppName
 
 

Build Project (if applicable)

npm run build
 

Restart After Build

 
pm2 restart api  # or use app name or process ID
 
 

πŸ’Ύ 7. Backup and Restore MongoDB

 
mongodump --db=your-db-name
 
 

Restore

mongorestore
 

πŸ”’ 8. Enable SSL with Let’s Encrypt To secure your website with HTTPS, use Certbot with Nginx.

Install Certbot

 
sudo apt install certbot python3-certbot-nginx
 
 

Configure UFW Firewall

 
sudo ufw status
sudo ufw enable
sudo ufw default deny
sudo ufw allow 'Nginx Full'
sudo ufw delete allow 'Nginx HTTP'
sudo ufw allow 22  # For SSH
 

Generate SSL Certificate

 
sudo certbot --nginx -d test.ohmkhay.com
 

πŸ” 9. Auto-Renew SSL Check Certbot Timer

 
sudo certbot renew --dry-run
 
 

⚑ 10. Install Redis Server Redis is an in-memory key-value store, often used for caching or session management.

 
sudo apt install redis-server
sudo systemctl status redis
 
 

🧠 11. Fix "JavaScript Heap Out of Memory" Error If your Node.js app runs out of memory, you can increase the memory allocation:

 
export NODE_OPTIONS=--max_old_space_size=4096
 
 

βœ… Wrapping Up Now your production environment is ready:

Node.js app is served behind Nginx reverse proxy.

MongoDB is installed and secured.

App is running continuously with PM2.

SSL is enabled for secure communication.

Redis is ready for caching.

You're all set for deployment and scalability!