Home > Article > Operation and Maintenance > Concise tutorial: How to use NGINX and PM2 to optimize resource management of VPS servers
Concise tutorial: How to use NGINX and PM2 to optimize resource management of VPS servers
Foreword:
In the modern Internet era, with the development of websites and applications As traffic increases, server resource management becomes critical. In order to improve the performance and reliability of the server, it is very necessary to reasonably configure and optimize server resources. This article will introduce how to use NGINX and PM2 to optimize the resource management of the VPS server, thereby improving the performance and reliability of the server.
1. Installation and configuration of NGINX
Installing NGINX
On Ubuntu, you can install NGINX through the following command:
$sudo apt-get update $sudo apt-get install nginx
nginx.conf
file stored in the /etc/nginx
directory. In this configuration file, you can configure the listening port, virtual host, etc. For details, please refer to the official documentation of NGINX. 2. Installation and configuration of PM2
Installing PM2
You can install PM2 globally through npm:
$sudo npm install -g pm2
Configuring PM2
Before using PM2 to manage applications, you need to create a startup script. Create a ecosystem.config.js
file in the root directory of the project, and copy the following content into the file:
module.exports = { apps: [ { name: "app", script: "app.js", instances: "max", autorestart: true, watch: true, ignore_watch: ["node_modules", "logs"], exec_mode: "cluster" } ] };
In the above configuration file, name
is The name of the application, script
is the path to the startup script, instances
is the number of processes, autorestart
is whether to automatically restart, watch
is Whether to automatically restart monitoring file changes, ignore_watch
is the directory to ignore monitoring, exec_mode
is the execution mode of the process.
3. Combined use of NGINX and PM2
Add the following code in the NGINX configuration file to forward the request to the local 3000 port:
server { listen 80; server_name your_domain.com; location / { proxy_pass http://localhost:3000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } }
Start the PM2 application
In the project In the root directory, run the following command to start the application managed by PM2:
$pm2 start ecosystem.config.js
Reload NGINX configuration
After modifying the NGINX configuration file, use the following command to reload the configuration File:
$sudo service nginx reload
Conclusion:
By properly configuring and optimizing NGINX and PM2, we can optimize the resource management of the VPS server and improve the performance and reliability of the server. This article introduces how to install and configure NGINX and PM2, and briefly introduces how to use them together to optimize the resource management of the VPS server. I hope this concise tutorial will help you optimize server resource management.
Code sample:
The following is a simple Express application startup script app.js:
const express = require("express"); const app = express(); app.get("/", (req, res) => { res.send("Hello, World!"); }); app.listen(3000, () => { console.log("Server is listening on port 3000"); });
Note: The above code is just an example, in fact you may need to customize it according to your own Make corresponding modifications and configurations to your projects and needs.
Reference link:
The above is the detailed content of Concise tutorial: How to use NGINX and PM2 to optimize resource management of VPS servers. For more information, please follow other related articles on the PHP Chinese website!