Home  >  Article  >  Operation and Maintenance  >  How to configure a VPS server using NGINX and PM2

How to configure a VPS server using NGINX and PM2

WBOY
WBOYOriginal
2023-09-27 12:54:28877browse

How to configure a VPS server using NGINX and PM2

How to use NGINX and PM2 to configure the VPS server

In the process of building a web server, using NGINX and PM2 is a common configuration method. NGINX is a high-performance web server commonly used for reverse proxy and load balancing. PM2 is a process management tool that can run and manage Node.js applications on the server. This article will introduce how to configure a VPS server using NGINX and PM2, and provide specific code examples.

Step One: Install NGINX and PM2

First, log in to the VPS server and run the following command to install NGINX and PM2:

# 安装NGINX
sudo apt-get update
sudo apt-get install nginx

# 安装Node.js和PM2
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
sudo apt-get install -y nodejs
sudo npm install -g pm2

Step Two: Configure NGINX

The main purpose of configuring NGINX is to direct HTTP traffic to the correct port and application. Create an NGINX configuration file on the server:

sudo nano /etc/nginx/sites-available/default

In the file that opens, paste the following content into it:

server {
  listen 80;
  server_name your_domain.com;

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

In the above configuration, replace "your_domain.com" with your domain name or IP address of the server, and replace "localhost:3000" with the port your application is running on.

After saving and closing the file, restart the NGINX service:

sudo service nginx restart

Step 3: Configure PM2

Using PM2 to run and manage Node.js applications is very simple. In your application directory, use the following command to start the application:

pm2 start app.js --name my-app

The above command will start a process named "my-app" and set the application's entry file to "app.js ". You can make appropriate substitutions based on your application.

If your application needs to use environment variables, you can use the following command to specify the environment variable:

pm2 start app.js --name my-app --env production

If your application needs to specify the working directory, you can use the following command to start the application :

pm2 start app.js --name my-app --cwd /path/to/app

After starting an application using PM2, you can use the following command to view the currently running applications:

pm2 list

If you want to restart or stop the application, you can use the following command:

pm2 restart my-app
pm2 stop my-app

Finally, if you want your application to start automatically after the server restarts, you can run the following command:

pm2 startup

PM2 will generate a command and copy it to the terminal to run. This command will automatically start PM2 and your application when the server starts.

Summary

Through the above steps, you have successfully configured a VPS server using NGINX and PM2. NGINX will take care of directing HTTP traffic to the correct port and application, while PM2 will run and manage your Node.js application on the server. Using NGINX and PM2, a high-performance and stable web server can be achieved.

Hope this article is helpful to you!

The above is the detailed content of How to configure a VPS server using NGINX and PM2. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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