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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Nginx error page configuration, beautify website fault prompts. During the operation of the website, it is inevitable to encounter server errors or other faults. These problems will cause users to be unable to access the website normally. In order to improve user experience and website image, we can configure Nginx error pages to beautify website failure prompts. This article will introduce how to customize the error page through Nginx's error page configuration function, and provide code examples as a reference. 1. Modify the Nginx configuration file. First, we need to open the Nginx configuration.

How to implement Nginx's cross-domain resource sharing (CORS) configuration requires specific code examples. With the popularity of front-end and back-end separation development, cross-domain resource sharing (CORS) issues have become a common challenge. In web development, due to the browser's same-origin policy restrictions, client-side JavaScript code can only request resources with the same domain name, protocol, and port as the page where it is located. However, in actual development, we often need to request resources from different domain names or different subdomains. At this time, you need to use CO

Nginx access control configuration to restrict access to specified users. In a web server, access control is an important security measure used to restrict access rights to specific users or IP addresses. As a high-performance web server, Nginx also provides powerful access control functions. This article will introduce how to use Nginx configuration to limit the access permissions of specified users, and provide code examples for reference. First, we need to prepare a basic Nginx configuration file. Assume we already have a website with a configuration file path of

PHP is a very popular programming language, especially suitable for web development. As a PHP developer, when dealing with some configuration files, you often need to use arrays for management. In this article, we will explore how to use PHP arrays like Nginx configuration files for configuration management. Nginx's configuration file is a very common configuration method that can be edited using text and is very readable. The Nginx configuration file uses a method similar to a PHP array to represent configuration information.

nginx configuration is the main configuration file, virtual host configuration, HTTP request processing, reverse proxy, load balancing, static file processing, HTTP compression, SSL/TLS support, virtual host configuration and log files.

How to use NGINX and PM2 to configure a 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 1: Install NGINX and PM2

How Nginx implements access control configuration based on the domain name of the request source requires specific code examples. Nginx is a high-performance web server software. It can not only serve as a static file server, but can also implement flexible access control through configuration. This article will introduce how to implement access control configuration based on the request source domain name through Nginx, and provide specific code examples. The Nginx configuration file is usually located in /etc/nginx/nginx.conf. We can add relevant configurations to this file.

Nginx restricts access to IP segment configuration and improves website security. In today's Internet era, website security is one of the important concerns of any enterprise or individual. There are endless hackers and cybercriminals launching malicious attacks, so it is crucial to protect your website from malicious requests and illegal access. As a high-performance web server and reverse proxy server, Nginx provides powerful security features, one of which is to restrict access to IP segments. This article will introduce how to use Nginx configuration to restrict access to IP segments and improve the website’s
