☁️Hosting·7 min read

Node.js Deployment Guide

Deploy and manage Node.js applications on your hosting instance.

This guide applies to instances provisioned with the Node.js blueprint, which comes with Node.js, npm, and PM2 pre-installed via Bitnami.

Accessing Your Server

SSH into your instance:

ssh -i your-key.pem bitnami@YOUR-IP

Verify Node.js is installed:

node --version

npm --version

pm2 --version

Deploying Your Application

Step 1: Upload Your Code

Option A — Git clone:

cd /home/bitnami

git clone https://github.com/your/repo.git myapp

Option B — SCP/SFTP:

scp -i your-key.pem -r ./myapp bitnami@YOUR-IP:/home/bitnami/myapp

Step 2: Install Dependencies

cd myapp

npm install --production

Step 3: Configure Environment Variables

Create a .env file or set environment variables:

nano .env

Add your variables:

PORT=3000

DATABASE_URL=postgresql://...

NODE_ENV=production

Step 4: Start with PM2

pm2 start server.js --name myapp

pm2 save

pm2 startup

PM2 ensures your app restarts if it crashes or the server reboots.

PM2 Management Commands

pm2 list — View running processes

pm2 logs myapp — View application logs

pm2 restart myapp — Restart the app

pm2 stop myapp — Stop the app

pm2 delete myapp — Remove from PM2

pm2 monit — Real-time monitoring dashboard

Setting Up Nginx as Reverse Proxy

For production, put Nginx in front of Node.js:

1.Install Nginx (if not present):

sudo apt install nginx

2.Create config:

sudo nano /etc/nginx/sites-available/myapp

server {

listen 80;

server_name 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;

}

}

3.Enable and restart:

sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/

sudo nginx -t

sudo systemctl restart nginx

Installing SSL with Certbot

sudo apt install certbot python3-certbot-nginx

sudo certbot --nginx -d yourdomain.com

Certbot auto-configures Nginx and sets up auto-renewal.

Running Next.js or Nuxt.js

npm run build

pm2 start npm --name nextapp -- start

Monitoring

Use PM2 logs and monit for application health
Check system resources: htop, df -h, free -m
Set up PM2 alerts: pm2 install pm2-logrotate

Troubleshooting

Port already in use: Find and kill the process: lsof -i :3000 then kill -9 PID
npm install fails: Check disk space (df -h) and memory (free -m)
App won't start: Check logs: pm2 logs myapp --lines 100
502 Bad Gateway: Ensure your app is running on the configured port
Knowledge Base — NexusHost | NexusHost