AI development environment on Ubuntu
[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).
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.
Verify it worked:
v20.11.0
10.2.4
Step 2: Create a Dummy App
Let's create a quick Express server to test our setup.
Paste this code into the file (Ctrl+O to save, Ctrl+X to exit):
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:
│ 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.
sudo env PATH.... You must copy and run that command manually to finish the setup.
3. Save the List:
Step 4: Nginx Reverse Proxy
Your app is on port 3000. Your users are on port 80. Nginx bridges the gap.
Paste the config below (replace your_domain.com with your real domain):
Enable and Restart:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Step 5: Go Secure (HTTPS)
Finally, lock it down with a free SSL certificate.
Your Node.js app is now live, auto-healing, and secure.