Here’s a step-by-step guide to installing Node.js on a VPS server (suitable for Ubuntu-based systems):
How to Install Node.js on a VPS Server
Step 1: Connect to Your VPS
Use SSH to log in to your server:
ssh your-username@your-server-ip
Replace
your-usernameandyour-server-ipwith your actual SSH credentials.
Step 2: Update Your System
Before installing anything, update the package index:
sudo apt update && sudo apt upgrade -y
Step 3: Install Node.js (Recommended Method via NodeSource)
-
Add the NodeSource repository:
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
You can replace
18.xwith the version you want (e.g.,20.xfor the latest LTS).
-
Install Node.js:
sudo apt install -y nodejs
-
Verify Installation:
node -v
npm -v
Step 4: (Optional) Install Build Tools
Some Node packages need build tools. Install them with:
sudo apt install -y build-essential
Step 5: Run a Simple Node App (Test)
-
Create a file:
nano app.js
-
Paste this code:
const http = require('http');
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.end('Hello from your VPS!');
});
server.listen(port, () => {
console.log(`Server running at http://localhost:${port}/`);
});
-
Start the app:
node app.js
-
Visit
http://your-server-ip:3000in your browser.
Bonus: Keep App Running in Background
Use PM2, a process manager:
sudo npm install -g pm2
pm2 start app.js
pm2 startup
pm2 save
Need Help?
If you're using a managed VPS (e.g., from Sun Servers), reach out to your hosting provider for assistance setting up or maintaining your Node.js environment.
Let me know if you want the guide tailored for CentOS, Debian, or with Docker instead.