[Image of Nginx reverse proxy architecture]

Running node index.js is fine for your laptop. On a server, it is a disaster waiting to happen.

If your app crashes, it stays down. If the server reboots, your app stays dead. To host Node.js professionally on Joy Services, you need PM2 (to keep the app alive) and Nginx (to handle the traffic).

Prerequisites: A Joy Services VPS (Ubuntu 24.04), a domain name, and basic Nginx installed.

Step 1: Install Node.js (The Right Way)

Do not use apt install nodejs. It installs ancient versions. We will use NVM (Node Version Manager) to get the latest Long Term Support (LTS) version.

Bash
root@joy:~# curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash source ~/.bashrc nvm install --lts

Verify it worked:

Bash
root@joy:~# node -v && npm -v
Expected Output
v20.11.0 10.2.4

Step 2: Create a Dummy App

Let's create a quick Express server to test our setup.

Bash
root@joy:~# mkdir -p ~/my-node-app && cd ~/my-node-app npm init -y && npm install express nano index.js

Paste this code into the file (Ctrl+O to save, Ctrl+X to exit):

JavaScript
const express = require('express'); const app = express(); const PORT = 3000; app.get('/', (req, res) => { res.send('Hello from Joy Services! Node.js is running successfully.'); }); app.listen(PORT, () => { console.log(`App running on port ${PORT}`); });

Step 3: Setup PM2 (The Process Manager)

PM2 acts as a bodyguard for your app. If the app crashes, PM2 restarts it instantly.

1. Install and Start:

Bash
root@joy:~# npm install pm2@latest -g pm2 start index.js --name "my-app"
Expected Output
│ App name │ id │ mode │ pid │ status │ restart │ uptime │ ├──────────┼────┼──────┼──────┼────────┼─────────┼────────┤ │ my-app │ 0 │ fork │ 1234 │ online │ 0 │ 0s │

2. Make it Reboot-Proof:
This is the step everyone forgets. We need to tell the server to launch PM2 when it turns on.

root@joy:~# pm2 startup
Action Required: The command above will print a new command in the terminal starting with sudo env PATH.... You must copy and run that command manually to finish the setup.

3. Save the List:

root@joy:~# pm2 save

Step 4: Nginx Reverse Proxy

Your app is on port 3000. Your users are on port 80. Nginx bridges the gap.

Bash
root@joy:~# sudo nano /etc/nginx/sites-available/my-node-app

Paste the config below (replace your_domain.com with your real domain):

Nginx Config
server { listen 80; server_name your_domain.com www.your_domain.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; } }

Enable and Restart:

root@joy:~# sudo ln -s /etc/nginx/sites-available/my-node-app /etc/nginx/sites-enabled/ sudo nginx -t
Expected Output
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
root@joy:~# sudo systemctl restart nginx

Step 5: Go Secure (HTTPS)

Finally, lock it down with a free SSL certificate.

Bash
root@joy:~# sudo certbot --nginx -d your_domain.com -d www.your_domain.com

Your Node.js app is now live, auto-healing, and secure.