Home >Operation and Maintenance >Nginx >Teach you how to use NGINX and PM2 to deploy Node.js applications on VPS servers

Teach you how to use NGINX and PM2 to deploy Node.js applications on VPS servers

王林
王林Original
2023-09-26 17:54:411297browse

Teach you how to use NGINX and PM2 to deploy Node.js applications on VPS servers

Teach you how to use NGINX and PM2 to deploy Node.js applications on VPS servers

Today I will introduce to you how to use NGINX and PM2 to deploy on VPS servers Node.js application. Node.js is a very popular back-end development framework, while NGINX is a high-performance reverse proxy server and PM2 is a powerful process manager. By using these three tools together, we can achieve efficient and stable server deployment.

  1. Make sure you have Node.js and NPM installed. If it is not installed, you can install it with the following command:
sudo apt-get update
sudo apt-get install nodejs
sudo apt-get install npm
  1. Install PM2 Process Manager. PM2 can help us manage the process of Node.js applications and automatically restart them when the application crashes. Use the following command to install:
sudo npm install -g pm2
  1. Create a simple Node.js application. Create a new folder on your VPS and create a file named app.js inside it. Write your application code in app.js. For example, here is a simple Express application code:
var express = require('express');
var app = express();

app.get('/', function(req, res) {
  res.send('Hello World!');
});

app.listen(3000, function() {
  console.log('App listening on port 3000!');
});
  1. Launch the application using PM2. In the terminal, navigate to the application directory and run the following command:
pm2 start app.js

Now your application has been launched through PM2 and will automatically restart on failure.

  1. Configure NGINX reverse proxy. We want to configure NGINX to listen on port 80, forwarding incoming requests to our Node.js application. Open the NGINX configuration file, the path is generally /etc/nginx/sites-enabled/default, and configure it as follows:
server {
  listen 80;
  server_name your-domain.com;

  location / {
    proxy_set_header   X-Real-IP $remote_addr;
    proxy_set_header   Host      $http_host;
    proxy_pass         http://127.0.0.1:3000;
  }
}

In this configuration, change your Replace -domain.com with your domain name. Then, replace 127.0.0.1:3000 with the address and port your application is running on.

  1. Save and close the configuration file. Then, reload the NGINX configuration to take effect:
sudo service nginx reload

NGINX will now send incoming requests to our Node.js application through the reverse proxy.

Congratulations! You have successfully deployed a Node.js application on a VPS server using NGINX and PM2. Now, you can access your domain name and should be able to see what your application is running.

The above is a brief guide to using NGINX and PM2 to deploy Node.js applications on VPS servers. I hope it will be helpful to you.

The above is the detailed content of Teach you how to use NGINX and PM2 to deploy Node.js applications on VPS servers. 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