Home > Operation and Maintenance > Nginx > 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

WBOY
Release: 2023-09-26 11:01:10
Original
867 people have browsed it

简明教程: 如何使用NGINX和PM2优化VPS服务器的资源管理

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

  1. Installing NGINX
    On Ubuntu, you can install NGINX through the following command:

    $sudo apt-get update
    $sudo apt-get install nginx
    Copy after login
  2. Configuring NGINX
    By default, the NGINX configuration file is the 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

  1. Installing PM2
    You can install PM2 globally through npm:

    $sudo npm install -g pm2
    Copy after login
  2. 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"
     }
      ]
    };
    Copy after login

    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

  1. Configuring the reverse proxy
    You can optimize the load balancing and traffic of requests by configuring the reverse proxy manage. We can configure a reverse proxy in the NGINX configuration file to forward requests to applications managed by 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;
  }
}
Copy after login
  1. 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
    Copy after login
  2. Reload NGINX configuration
    After modifying the NGINX configuration file, use the following command to reload the configuration File:

    $sudo service nginx reload
    Copy after login

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");
});
Copy after login

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:

  • NGINX official documentation: https://nginx.org/en/docs/
  • PM2 official documentation: https://pm2. keymetrics.io/docs/

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template