How to Install Node.js on a VPS
Install and configure Node.js on your GoZen Host VPS. Covers NVM, PM2 process management, and Nginx reverse proxy setup.
Node.js lets you run JavaScript on your server. It’s used for web applications (Express, Next.js, Nuxt), APIs, real-time apps, and build tools. Here’s how to install it on your GoZen Host VPS.
Method 1: NVM (Recommended)
NVM (Node Version Manager) lets you install and switch between multiple Node.js versions easily.
Install NVM
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
Reload your shell:
source ~/.bashrc
Install Node.js
# Install the latest LTS version (recommended for production)
nvm install --lts
# Or install a specific version
nvm install 20
# Verify
node --version
npm --version
Switch Between Versions
# List installed versions
nvm ls
# Switch to a version
nvm use 20
# Set a default version
nvm alias default 20
Method 2: System Package Manager
If you prefer a system-wide installation:
Ubuntu/Debian
# Add NodeSource repository (Node.js 20 LTS)
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
# Install
sudo apt install -y nodejs
# Verify
node --version
npm --version
AlmaLinux/Rocky
curl -fsSL https://rpm.nodesource.com/setup_20.x | sudo bash -
sudo dnf install -y nodejs
Running a Node.js App
Simple Test
Create a test app:
mkdir ~/myapp && cd ~/myapp
nano app.js
const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello from GoZen Host VPS!\n');
});
server.listen(3000, () => {
console.log('Server running on port 3000');
});
node app.js
Visit http://your-server-ip:3000 to see it working. Press Ctrl+C to stop.
PM2: Keep Your App Running
Node.js apps stop when you close your SSH session. PM2 keeps them running in the background and auto-restarts them if they crash.
Install PM2
npm install -g pm2
Start Your App
cd ~/myapp
pm2 start app.js --name "myapp"
PM2 Commands
# List running apps
pm2 list
# View logs
pm2 logs myapp
# Restart
pm2 restart myapp
# Stop
pm2 stop myapp
# Delete from PM2
pm2 delete myapp
# Monitor resources
pm2 monit
Auto-Start on Reboot
pm2 startup
# Follow the instructions it prints
pm2 save
Nginx Reverse Proxy
Your Node.js app runs on port 3000 (or similar), but visitors expect port 80/443. Use Nginx as a reverse proxy:
Install Nginx
sudo apt install -y nginx
Configure the Proxy
sudo nano /etc/nginx/sites-available/yourdomain.com
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache_bypass $http_upgrade;
}
}
# Enable the site
sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/
# Test config
sudo nginx -t
# Reload
sudo systemctl reload nginx
Add SSL with Let’s Encrypt
sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
Certbot automatically configures HTTPS and sets up auto-renewal.
Environment Variables
Don’t hardcode secrets in your code. Use environment variables:
With PM2
Create an ecosystem.config.js:
module.exports = {
apps: [{
name: 'myapp',
script: 'app.js',
env: {
NODE_ENV: 'production',
PORT: 3000,
DB_HOST: 'localhost',
DB_PASSWORD: 'your-db-password'
}
}]
};
Start with: pm2 start ecosystem.config.js
With a .env File
Use the dotenv package:
npm install dotenv
Create .env:
PORT=3000
DB_HOST=localhost
DB_PASSWORD=your-db-password
Never commit
.envfiles to Git. Add.envto your.gitignore.
Firewall
Allow only the ports you need:
# Allow SSH, HTTP, HTTPS
sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'
sudo ufw enable
# Don't expose your Node.js port directly
# Nginx handles public traffic on 80/443
Related Articles
Last updated 19 Apr 2026, 23:46 +0300.