search
HomeOperation and MaintenanceNginxHow to configure a VPS server using NGINX and PM2

How to configure a VPS server using NGINX and PM2

Sep 27, 2023 pm 12:54 PM
nginx configurationpm configurationvps server configuration

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
NGINX vs. Apache: Performance, Scalability, and EfficiencyNGINX vs. Apache: Performance, Scalability, and EfficiencyApr 19, 2025 am 12:05 AM

NGINX and Apache are both powerful web servers, each with unique advantages and disadvantages in terms of performance, scalability and efficiency. 1) NGINX performs well when handling static content and reverse proxying, suitable for high concurrency scenarios. 2) Apache performs better when processing dynamic content and is suitable for projects that require rich module support. The selection of a server should be decided based on project requirements and scenarios.

The Ultimate Showdown: NGINX vs. ApacheThe Ultimate Showdown: NGINX vs. ApacheApr 18, 2025 am 12:02 AM

NGINX is suitable for handling high concurrent requests, while Apache is suitable for scenarios where complex configurations and functional extensions are required. 1.NGINX adopts an event-driven, non-blocking architecture, and is suitable for high concurrency environments. 2. Apache adopts process or thread model to provide a rich module ecosystem that is suitable for complex configuration needs.

NGINX in Action: Examples and Real-World ApplicationsNGINX in Action: Examples and Real-World ApplicationsApr 17, 2025 am 12:18 AM

NGINX can be used to improve website performance, security, and scalability. 1) As a reverse proxy and load balancer, NGINX can optimize back-end services and share traffic. 2) Through event-driven and asynchronous architecture, NGINX efficiently handles high concurrent connections. 3) Configuration files allow flexible definition of rules, such as static file service and load balancing. 4) Optimization suggestions include enabling Gzip compression, using cache and tuning the worker process.

NGINX Unit: Supporting Different Programming LanguagesNGINX Unit: Supporting Different Programming LanguagesApr 16, 2025 am 12:15 AM

NGINXUnit supports multiple programming languages ​​and is implemented through modular design. 1. Loading language module: Load the corresponding module according to the configuration file. 2. Application startup: Execute application code when the calling language runs. 3. Request processing: forward the request to the application instance. 4. Response return: Return the processed response to the client.

Choosing Between NGINX and Apache: The Right Fit for Your NeedsChoosing Between NGINX and Apache: The Right Fit for Your NeedsApr 15, 2025 am 12:04 AM

NGINX and Apache have their own advantages and disadvantages and are suitable for different scenarios. 1.NGINX is suitable for high concurrency and low resource consumption scenarios. 2. Apache is suitable for scenarios where complex configurations and rich modules are required. By comparing their core features, performance differences, and best practices, you can help you choose the server software that best suits your needs.

How to start nginxHow to start nginxApr 14, 2025 pm 01:06 PM

Question: How to start Nginx? Answer: Install Nginx Startup Nginx Verification Nginx Is Nginx Started Explore other startup options Automatically start Nginx

How to check whether nginx is startedHow to check whether nginx is startedApr 14, 2025 pm 01:03 PM

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

How to close nginxHow to close nginxApr 14, 2025 pm 01:00 PM

To shut down the Nginx service, follow these steps: Determine the installation type: Red Hat/CentOS (systemctl status nginx) or Debian/Ubuntu (service nginx status) Stop the service: Red Hat/CentOS (systemctl stop nginx) or Debian/Ubuntu (service nginx stop) Disable automatic startup (optional): Red Hat/CentOS (systemctl disabled nginx) or Debian/Ubuntu (syst

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)