Hero Image

Install Node.js on RHEL 9

Install Node.js on RHEL 9

Step 1 – Install Node.js LTS

# Add NodeSource repository for Node.js 20 LTS
curl -fsSL https://rpm.nodesource.com/setup_20.x | bash -
dnf install -y nodejs

Step 2 – Verify

node --version
npm --version

Step 3 – Install a package manager (optional: pnpm)

npm install -g pnpm
pnpm --version

Step 4 – Run a simple HTTP server

mkdir -p /opt/myapp && cd /opt/myapp
cat > app.js <<'EOF'
const http = require('http');
const server = http.createServer((req, res) => {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello from Node.js!\n');
});
server.listen(3000, '127.0.0.1', () => {
  console.log('Server running at http://127.0.0.1:3000/');
});
EOF
node app.js &
curl http://127.0.0.1:3000

Step 5 – Run as a systemd service

# /etc/systemd/system/myapp.service
[Unit]
Description=My Node.js App
After=network.target

[Service]
User=nodejs
WorkingDirectory=/opt/myapp
ExecStart=/usr/bin/node app.js
Restart=on-failure
Environment=NODE_ENV=production PORT=3000

[Install]
WantedBy=multi-user.target
useradd -r -s /sbin/nologin nodejs
chown -R nodejs:nodejs /opt/myapp
systemctl enable --now myapp

Step 6 – Reverse proxy with Nginx

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;
}