VPS - Deploying Multiple Node.js Apps on a VPS with Nginx, PM2, and SSL
1. Connect to Your VPS via SSH
ssh root@your_server_ip
Ajibolatao1.
2. Update Your VPS
sudo apt update && sudo apt upgrade -y
Create a New User (recommended instead of using root):
sudo adduser newuser
sudo usermod -aG sudo newuser
su - newuser
sudo whoami
3. Install Node.js, NPM, and Git
curl -fsSL https://deb.nodesource.com/setup_22.x -o nodesource_setup.sh
sudo -E bash nodesource_setup.sh
sudo apt install -y nodejs
sudo apt install git -y
Verify Installation:
node -v
npm -v
git --version
4. Install PM2 (Process Manager)
sudo npm install -g pm2
Verify Installation:
pm2 -v
5. Install and Configure Nginx
sudo apt install nginx
Start & Enable Nginx:
sudo systemctl start nginx
sudo systemctl enable nginx
sudo systemctl status nginx
Allow Access to Required Ports:
sudo ufw allow OpenSSH
sudo ufw allow 'Nginx HTTP'
sudo ufw allow 'Nginx Full'
sudo ufw allow 8080
6. Setup SSH Keys for GitHub (Private Repo Access)
ssh-keygen -t ed25519 -C "[email protected]"
cat ~/.ssh/id_ed25519.pub
- (Add the key at GitHub → Settings → SSH Keys) (here)
git clone [email protected]:your-username/your-private-repo.git
7. Clone Your App
git clone your_repository_url /path/to/your/app
git pull
Navigate & Install Dependencies:
cd /path/to/your/app
npm install
8. Run Apps with PM2
Navigate into app folder, then run:
pm2 start npm --name *appName* --watch -- start
# e.g pm2 start npm --name RevampCode --watch -- start
OR
pm2 start server.js --name app1 --watch -- 5000
pm2 start app.js --name app2 --watch -- 5001
PM2 Useful Commands:
pm2 list
pm2 stop all
pm2 delete all
pm2 monit
pm2 restart app1
pm2 save
9. Configure Nginx Reverse Proxy
Default config (drop unwanted requests):
sudo nano /etc/nginx/sites-available/default
server {
listen 80 default_server;
server_name _;
location / {
return 444;
}
}
App 1 Configuration:
sudo nano /etc/nginx/sites-available/app1
# e.g sudo nano /etc/nginx/sites-available/RevampCode
# For a node app
server {
listen 80;
server_name app1.domain.com; # Replace with api.revampcode.com if using subdomain
location / {
proxy_pass http://localhost:8080;
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;
}
}
# HTML website (static files)
server {
listen 80;
server_name revampcode.com www.revampcode.com;
root /var/www/revampcode;
index index.html;
}
server {
listen 80;
server_name mywatch.com www.mywatch.com;
root /var/www/mywatch;
index index.html;
}
Enable Config:
sudo ln -s /etc/nginx/sites-available/app1 /etc/nginx/sites-enabled/
(Repeat for each app.)
10. Reload Nginx
sudo nginx -t
sudo systemctl reload nginx
11. Update DNS A Records
| Type | Name | Value |
|---|---|---|
| A Record | app1 | VPS_IP_ADDRESS |
| A Record | app2 | VPS_IP_ADDRESS |
12. Setup SSL with Let’s Encrypt
Install Certbot:
sudo apt install certbot python3-certbot-nginx
Obtain Certificates:
sudo certbot --nginx -d app1.domain.com -d app2.domain.com
Auto-Renew:
sudo certbot renew --dry-run
13. Enable PM2 on Reboot
pm2 startup
pm2 save
14. Monitoring & Maintenance
Monitor CPU & Memory:
htop
free -h
Check Used Ports:
sudo lsof -i :8081
sudo kill -9
Misc:
passwd username # Change user password
USED PORTS
8080- ScholarshipPulse
8081 - RevampCode
8082 - CSMTech
8083 - Photonics
8084 - RecolorPro
8085 - Qdyizhiyou
8086 - STN
8087 - MzorlineWears
8088 - Template