Want to deploy your NodeJS app without breaking a sweat? If you’re looking for a simple, cost-effective, and production-ready way to bring your project to life, this guide is your one-stop solution. We’ll walk you through a real-world deployment of a NodeJS app on DigitalOcean—complete with PM2 process management and Nginx reverse proxy setup.
This article is perfect for developers who want full control over their app’s environment, uptime, and domain management, while avoiding complex cloud dashboards like AWS.
Let’s break it down, step by step.
Why DigitalOcean is Ideal for NodeJS Deployment
So, why choose DigitalOcean for your NodeJS deployment?
- Developer-Friendly Interface: Simple and clean dashboard.
- Affordable VPS Hosting: Plans start as low as $4/month.
- Full Root Access: Gives you complete control over your NodeJS server.
- Flexible Scaling: Upgrade your droplet as your traffic grows.
DigitalOcean gives you everything you need without the complications. This is the way to go if you want a stress-free start to your NodeJS deployment journey.
Step 1: Create Your Droplet on DigitalOcean
- Sign in to DigitalOcean and click “Create Droplet.”
- Choose Ubuntu (latest LTS version, e.g., Ubuntu 22.04).
- Select a plan (the $4 basic plan is good for most apps).
- Choose the closest data center to your users.
- Add your SSH key or choose a root password.
Once your droplet is ready, connect to it from your terminal:
ssh root@your_droplet_ip
Boom! You’re in.
Step 2: Install NodeJS and NPM
Let’s get your droplet ready to host your NodeJS app.
sudo apt update
sudo apt install nodejs npm -y
Check if it’s working:
node -vnpm -v
Want to manage multiple Node versions? Install Node with nvm:
curl -o-
https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh |
bash
source ~/.bashrc
nvm install 18
This setup makes version switching super easy.
Step 3: Upload Your NodeJS App
You’ve got two ways to upload your code:
Option A: Use Git
This is the cleanest way if your app lives on GitHub:
git clone https://github.com/yourusername/your-nodejs-app.git
cd your-nodejs-app
npm install
Option B: Use SCP or SFTP
Transfer files directly from your local machine:
scp -r ./your-nodejs-app root@your_droplet_ip:/root/
Then:
cd your-nodejs-appnpm install
And that’s it—your code is live on the server.
Step 4: Run Your NodeJS App with PM2
Running a NodeJS app manually with node index.js isn’t enough for production. You need a process manager, and that’s where PM2 comes in.
What Is PM2?
PM2 NodeJS process manager keeps your app running forever, even if the server restarts or your app crashes. It’s essential for a stable production setup.
Install PM2:
npm install pm2 -g
Start Your App with PM2:
pm2 start index.js --name my-node-app
PM2 will now keep your app alive in the background.
Save the Process:
pm2 save
Enable PM2 on Reboot:
pm2 startup
PM2 will give you a command to copy and paste. This ensures your app auto-starts after a server reboot.
You now have a PM2 NodeJS setup ready for production.
Step 5: Set Up Nginx as a Reverse Proxy
Your app runs locally on http://localhost:3000, but your users will access it via a domain like https://yourdomain.com. To connect the two, use Nginx.
Install Nginx:
sudo apt install nginx -y
Create a New Config File:
sudo nano /etc/nginx/sites-available/your-app
Paste this config:
nginx
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
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_cache_bypass $http_upgrade;
}
}
Activate the Config:
sudo ln -s /etc/nginx/sites-available/your-app
/etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
Now, Nginx routes traffic from your domain to your NodeJS app on port 3000.
Step 6: Secure Your NodeJS App with SSL
Next, let’s add HTTPS using Let’s Encrypt.
Install Certbot:
sudo apt install certbot python3-certbot-nginx -y
Generate SSL Certificate:
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
Let’s Encrypt will handle the certificate and Nginx config for you.
Auto-Renew SSL:
sudo systemctl status certbot.timer
Your NodeJS app with Nginx is now secure and accessible via HTTPS!
Step 7: Monitor and Manage with PM2
With PM2, you can easily monitor, restart, or stop your app.
View Logs:
pm2 logs my-node-app
- Restart App:
pm2 restart my-node-app
- Stop App:
pm2 stop my-node-app
- List Running Apps:
pm2 list
- Bonus: PM2 also offers a monitoring dashboard called PM2 Plus for real-time performance stats.
Pro Tips for NodeJS Deployment on DigitalOcean
- Environment Variables: Use .env files and dotenv for secret keys and configs.
Production Mode:
NODE_ENV=production pm2 restart my-node-app
- Firewall Setup:
sudo ufw allow 'Nginx Full'sudo ufw enable
- Backups: Use DigitalOcean’s backup feature or manually set up cron jobs.
- Security: Install fail2ban to protect from brute-force attacks.
These steps ensure your NodeJS deployment on DigitalOcean remains stable, fast, and secure.
Summary of the Setup
- Droplet on DigitalOcean
- NodeJS & NPM installed
- App uploaded via Git or SCP
- App managed by PM2 NodeJS process manager
- Nginx reverse proxy configured
- Free HTTPS certificate with Certbot
- Monitoring and auto-restart in place
You now have a complete NodeJS app with Nginx setup that runs smoothly and securely in production.
You can also look for: Production-Ready FastAPI Deployment Using Docker and Uvicorn
Final Thoughts – NodeJS App on DigitalOcean
Deploying a NodeJS app doesn’t have to be overwhelming. You’ve created a full production pipeline that works reliably by choosing DigitalOcean, using PM2 for process management, and setting up Nginx for domain routing and SSL.
Whether running a personal project or launching a startup MVP, this guide sets you up for success. Now your app runs 24/7, handles traffic like a pro, and looks legit with HTTPS.