Support Online
Skip to main content

Installing Node.js Production on Rocky Linux 9

What Will You Learn in This Guide?

In this guide, you will prepare the Node.js application for the production environment.
You will provide process management with PM2, reverse proxy and HTTPS access with Nginx.

🧠 Technical Summary

Subject: Node.js production installation on Rocky Linux 9
Problem: Stable and safe running of Node.js applications
Solution: PM2 + Nginx reverse proxy + HTTPS
Steps: Application → PM2 → Nginx → Publishing


Preliminary Preparations

The following should be ready before continuing:

  • A server with Rocky Linux 9 installed
  • A user with sudo authority
  • Server IP address directed to the domain
  • Nginx and Node.js must be installed

1. Creating a Simple Node.js Application

As an example, we will create a simple HTTP server.

sudo dnf install nano
  • This command installs a text editor.


nano hello.js
  • This command opens a new Node.js file.


const http = require('http');

const hostname = 'localhost';
const port = 3000;

const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World!\n');
});

server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
  • This code creates an HTTP server running on localhost.

Test the app:


node hello.js
  • This command starts the Node.js application.

2. Process Management with PM2

  1. PM2 runs Node.js applications in the background.

sudo npm install pm2@latest -g
  • This command installs PM2 globally.


pm2 start hello.js
  • This command starts the application with PM2.

To run automatically when the server restarts:


pm2 startup systemd
  • This command creates the systemd service.

Run the line in the command output and then:


pm2 save
  • This command saves the PM2 process list.

3. SELinux Compatible PM2 Service

  1. Rocky Linux requires additional settings due to SELinux.

sudo nano /etc/systemd/system/pm2-kullanici.service
  • This file contains PM2 service settings.


Environment=PM2_HOME=/home/kullanici/.pm2
PIDFile=/run/pm2.pid
Environment=PM2_PID_FILE_PATH=/run/pm2.pid
  • These settings ensure smooth operation of PM2.

Start the service:


sudo systemctl start pm2-kullanici
  • This command runs the PM2 service.

4. Nginx Reverse Proxy Configuration

  1. Nginx exposes the Node.js application to the outside world.

sudo nano /etc/nginx/conf.d/ornekdomain.conf
  • This file contains the domain configuration.


location / {
proxy_pass http://localhost: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;
}
  • This configuration directs traffic to the Node.js application.

Test the build:


sudo nginx -t
  • This command checks the Nginx configuration.


sudo systemctl restart nginx
  • This command restarts Nginx.

❓ Frequently Asked Questions (FAQ)

1. Why is PM2 needed? Even if the application crashes, it will automatically restart.

2. Why is Node.js listening to localhost? Direct external access is disabled for security.

3. Does it work without Nginx? Yes, but not recommended for production.

4. Can more than one application run on the same server? Yes, it is possible with different ports and paths.


Result

Now your Node.js application is ready for the production environment. You have established a stable structure with PM2 and a secure structure with Nginx.

You can easily implement this infrastructure on GenixNode.