Rocky Linux 9 is a popular choice for hosting Node.js applications due to its stability and compatibility with a wide range of server-side and networking applications. In this article, we will guide you through the process of setting up a production-ready Node.js environment on a Rocky Linux 9 server. By the end of this tutorial, you will have a secure, scalable, and efficient setup for running your Node.js applications in a production environment.
Prerequisites
Before we begin, make sure you have the following prerequisites in place:
- A Rocky Linux 9 server with a non-root user and sudo privileges.
- An active firewall configured on your server.
- A domain name pointed at your server’s public IP address (we’ll use example.com throughout this tutorial).
- Nginx installed and configured with SSL using Let’s Encrypt certificates.
- Node.js installed on your server.
If you haven’t completed these prerequisites, please refer to the appropriate tutorials linked above.
Step 1: Creating a Node.js Application
To get started, let’s create a simple “Hello World” application that will serve as a starting point for your Node.js projects. Open your favorite text editor and create a file called hello.js
. Insert the following code into the 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}/`);
});
Save the file and exit the editor. This code sets up a basic HTTP server that listens on the specified address (localhost
) and port (3000
). It responds with a “Hello World!” message to any incoming HTTP requests.
To test your application, run the following command in your terminal:
node hello.js
You should see the following output:
Server running at http://localhost:3000/
Now, open another terminal session on your server and use curl
to send an HTTP request to your application:
curl http://localhost:3000
If everything is set up correctly, you should see the following output:
Hello World!
Congratulations! Your Node.js application is up and running.
Step 2: Installing PM2
PM2 is a process manager for Node.js applications that allows you to manage and monitor your applications in a production environment. To install PM2, run the following command:
sudo npm install pm2@latest -g
The -g
option installs PM2 globally, making it available system-wide.
Once PM2 is installed, you can start your application by running the following command:
pm2 start hello.js
PM2 will automatically add your application to its process list and start it in the background. You can view the status of your application by running:
pm2 list
To ensure that your application starts automatically on server boot, run the following command:
pm2 startup systemd
Copy and run the command provided in the output. This will generate and configure a startup script for PM2.
Finally, let’s make a small adjustment to the generated systemd service file to make it compatible with Rocky Linux’s SELinux security system. Open the service file using your text editor:
sudo nano /etc/systemd/system/pm2-sammy.service
In the [Service]
block of the file, replace the PIDFile
setting with /run/pm2.pid
and add the Environment
line as shown below:
[Unit]
Description=PM2 process manager
Documentation=https://pm2.keymetrics.io/
After=network.target
[Service]
Type=forking
User=sammy
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
Environment=PATH=/home/sammy/.local/bin:/home/sammy/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/bin:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
Environment=PM2_HOME=/home/sammy/.pm2
PIDFile=/run/pm2.pid
Restart=on-failure
Environment=PM2_PID_FILE_PATH=/run/pm2.pid
ExecStart=/usr/local/lib/node_modules/pm2/bin/pm2 resurrect
ExecReload=/usr/local/lib/node_modules/pm2/bin/pm2 reload all
ExecStop=/usr/local/lib/node_modules/pm2/bin/pm2 kill
[Install]
Save the file and exit the editor.
Start the PM2 service by running:
sudo systemctl start pm2-sammy
Check the status of the service to ensure that it’s running without any issues:
sudo systemctl status pm2-sammy
You should see the status as “active” if everything is configured correctly.
Step 3: Setting Up Nginx as a Reverse Proxy Server
Now that your Node.js application is running, let’s set up Nginx as a reverse proxy server to handle incoming requests and forward them to your application.
Open your Nginx configuration file for editing:
sudo nano /etc/nginx/conf.d/your_domain.conf
Replace the contents of the location /
block with the following configuration:
[Unit] Description=PM2 process manager Documentation=https://pm2.keymetrics.io/ After=network.target [Service] Type=forking User=sammy LimitNOFILE=infinity LimitNPROC=infinity LimitCORE=infinity Environment=PATH=/home/sammy/.local/bin:/home/sammy/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/bin:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin Environment=PM2_HOME=/home/sammy/.pm2 PIDFile=/run/pm2.pid Restart=on-failure Environment=PM2_PID_FILE_PATH=/run/pm2.pid ExecStart=/usr/local/lib/node_modules/pm2/bin/pm2 resurrect ExecReload=/usr/local/lib/node_modules/pm2/bin/pm2 reload all ExecStop=/usr/local/lib/node_modules/pm2/bin/pm2 kill [Install]
This configuration tells Nginx to forward all requests to your Node.js application running on localhost
at port 3000
.
Save the file and exit the editor.
Check the syntax of your Nginx configuration to ensure there are no errors:
sudo nginx -t
If the syntax is correct, restart Nginx for the changes to take effect:
sudo systemctl restart nginx
Now, if you access your server’s URL (either the public IP address or domain name), you should be able to see your Node.js application running behind the Nginx reverse proxy.
Conclusion
Congratulations! You have successfully set up a production-ready Node.js environment on a Rocky Linux 9 server. By following the steps in this tutorial, you have created a secure and scalable setup for running your Node.js applications in a production environment.
To further enhance your Node.js application’s performance and security, consider using Shape.host’s Linux SSD VPS hosting services. Shape.host offers reliable and efficient cloud hosting solutions, providing businesses with the power and flexibility they need to succeed in today’s digital landscape.
With Shape.host’s Linux SSD VPS hosting, you can enjoy lightning-fast page load times, robust security measures, and expert technical support, ensuring that your Node.js applications run smoothly and securely. Visit Shape.host today to learn more about their hosting services and take your Node.js applications to the next level.