Teach you how to deploy multiple applications on a VPS server and manage them using NGINX and PM2

WBOY
Release: 2023-09-26 18:01:09
Original
1774 people have browsed it

Teach you how to deploy multiple applications on a VPS server and manage them using NGINX and PM2

Teach you how to deploy multiple applications on a VPS server and manage them using NGINX and PM2

Overview:
In modern Internet application development, use VPS Server-based deployment of applications is a common way. This article will teach you how to deploy multiple applications on a VPS server and use NGINX as a reverse proxy server and PM2 as an application process management tool. We'll demonstrate each step with specific code examples.

Step 1: Prepare VPS server
First, you need to have a VPS server and have administrator rights. You can use SSH to log in to the server for subsequent operations.

Step 2: Install NGINX and PM2
Install NGINX and PM2 on the VPS server. Depending on the operating system you are using, the installation method will be different. The following is a sample command to install on an Ubuntu system:

  1. Install NGINX:

sudo apt-get update
sudo apt-get install nginx

  1. Install PM2:

sudo apt-get install nodejs npm
sudo npm install pm2 -g

Step 3: Create the application
on the VPS server Create multiple applications, each application can be an independent folder. Here is the structure of a sample app:

myapp/
|-- index.js
|-- package.json

In each app’s folder, create A file named index.js and write the application code in it. The package.json file is used to describe relevant information and dependencies of your application. The following is the code for a sample index.js file:

const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain') ;
res.end('Hello, World!
');
});

server.listen(port, hostname, () => {
console. log(Server running at http://${hostname}:${port}/);
});

Step 4: Start the application
in each In the application folder, use PM2 to start the application. Here is a sample command:

cd /path/to/myapp
pm2 start index.js --name myapp

Repeat the above steps to start a process for each application.

Step 5: Configure NGINX
Add the reverse proxy configuration in the NGINX configuration file and forward the request to the corresponding application. Here is the code for the example configuration:

server {
listen 80;
server_name example.com;

location /app1 {

proxy_pass http://127.0.0.1:3000;
Copy after login

}

location /app2 {

proxy_pass http://127.0.0.1:3001;
Copy after login

}

# ... Configuration of other applications

# Configuration of static resources
location /static {

alias /path/to/static/files;
Copy after login

}
}

Modify example.com in the above configuration file to your domain name, and modify the proxy address of each application according to the actual situation.

Step 6: Restart NGINX
Restart NGINX for the configuration to take effect. On the Ubuntu system, you can use the following command to restart NGINX:

sudo service nginx restart

At this point, you have successfully deployed multiple applications on the VPS server and used NGINX as the reverse The proxy server forwards the request to the appropriate application. By using PM2, you can also easily manage application processes.

Summary:
This article introduces how to deploy multiple applications on a VPS server and manage them using NGINX and PM2. By following the above steps, you can easily deploy multiple applications to your VPS server and effectively manage and operate these applications. Hope this article is helpful to you!

The above is the detailed content of Teach you how to deploy multiple applications on a VPS server and manage them using NGINX and PM2. 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