Applies to: LifeinCloud Cloud VPS • Ubuntu/Debian • CentOS/RHEL/AlmaLinux

Node.js is a JavaScript runtime for building web apps, APIs, and automation scripts. This guide is designed for beginners and covers installation, creating a simple app, and keeping it running in production.

Step 1 — Install Node.js

We recommend installing the LTS (Long-Term Support) version for stability.

Ubuntu/Debian:
apt update && apt upgrade -y
curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
apt install -y nodejs
CentOS/RHEL/AlmaLinux:
dnf update -y
curl -fsSL https://rpm.nodesource.com/setup_20.x | bash -
dnf install -y nodejs

Step 2 — Verify Installation

node -v
npm -v

If you see version numbers, Node.js and npm were installed successfully.

Step 3 — Create a Hello World App

  1. Create a new file:
    nano hello.js
  2. Paste this code:
    const http = require('http');
    const server = http.createServer((req, res) => {
      res.end('Hello from Node.js on LifeinCloud VPS!');
    });
    server.listen(3000, () => console.log('Server running on http://YOUR_SERVER_IP:3000'));
  3. Run it:
    node hello.js

Visit http://YOUR_SERVER_IP:3000 in your browser — you should see your Hello World message.

Step 4 — Keep Your App Running with PM2

Without a process manager, your app stops when you close SSH. PM2 solves this.

npm install -g pm2
pm2 start hello.js --name myapp
pm2 status
pm2 logs myapp

Ensure PM2 restarts apps after reboot:

pm2 startup systemd
pm2 save

Step 5 — (Optional) Use Nginx as a Reverse Proxy

If you want your app accessible on port 80 (standard HTTP) instead of port 3000:

  1. Install Nginx:
    apt install -y nginx   # Ubuntu/Debian
    dnf install -y nginx   # CentOS/AlmaLinux
  2. Create a config file, e.g. /etc/nginx/sites-available/nodeapp:
    server {
      listen 80;
      server_name YOUR_DOMAIN_OR_IP;
    
      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_cache_bypass $http_upgrade;
      }
    }
  3. Enable and restart Nginx:
    ln -s /etc/nginx/sites-available/nodeapp /etc/nginx/sites-enabled/
    nginx -t
    systemctl restart nginx

Now your app is accessible at http://YOUR_DOMAIN_OR_IP.

Tips:
  • Use the LTS version of Node.js for stability.
  • Always run production apps with a process manager like PM2.
  • Restrict your firewall to only open necessary ports (80/443 for web, 22 for SSH).
  • For SSL, add Let’s Encrypt to Nginx or use a LifeinCloud control panel SSL option.
Esta resposta lhe foi útil? 1 Usuários acharam útil (1 Votos)