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

Sep 26, 2023 pm 05:54 PM
nodejs nginx pm

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
Copy after login
  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
Copy after login
  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!');
});
Copy after login
  1. Launch the application using PM2. In the terminal, navigate to the application directory and run the following command:
pm2 start app.js
Copy after login

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;
  }
}
Copy after login

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
Copy after login

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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to allow external network access to tomcat server How to allow external network access to tomcat server Apr 21, 2024 am 07:22 AM

To allow the Tomcat server to access the external network, you need to: modify the Tomcat configuration file to allow external connections. Add a firewall rule to allow access to the Tomcat server port. Create a DNS record pointing the domain name to the Tomcat server public IP. Optional: Use a reverse proxy to improve security and performance. Optional: Set up HTTPS for increased security.

How to connect nodejs to mysql database How to connect nodejs to mysql database Apr 21, 2024 am 06:13 AM

To connect to a MySQL database, you need to follow these steps: Install the mysql2 driver. Use mysql2.createConnection() to create a connection object that contains the host address, port, username, password, and database name. Use connection.query() to perform queries. Finally use connection.end() to end the connection.

Is there a big difference between nodejs and java? Is there a big difference between nodejs and java? Apr 21, 2024 am 06:12 AM

The main differences between Node.js and Java are design and features: Event-driven vs. thread-driven: Node.js is event-driven and Java is thread-driven. Single-threaded vs. multi-threaded: Node.js uses a single-threaded event loop, and Java uses a multi-threaded architecture. Runtime environment: Node.js runs on the V8 JavaScript engine, while Java runs on the JVM. Syntax: Node.js uses JavaScript syntax, while Java uses Java syntax. Purpose: Node.js is suitable for I/O-intensive tasks, while Java is suitable for large enterprise applications.

How to generate URL from html file How to generate URL from html file Apr 21, 2024 pm 12:57 PM

Converting an HTML file to a URL requires a web server, which involves the following steps: Obtain a web server. Set up a web server. Upload HTML file. Create a domain name. Route the request.

What is the relationship between nodejs and npm? What is the relationship between nodejs and npm? Apr 21, 2024 am 06:09 AM

Node.js is a JavaScript runtime environment and npm is its package manager. The two work together to enable developers to write server-side programs in JavaScript, use third-party modules, and easily manage modules.

How to connect nodejs to mycat How to connect nodejs to mycat Apr 21, 2024 am 06:16 AM

Steps to connect MyCAT in Node.js: Install the mycat-ts dependency. Create a connection pool, specify the host, port, username, password and database. Use the query method to execute SQL queries. Use the close method to close the connection pool.

How to deploy and maintain a website using PHP How to deploy and maintain a website using PHP May 03, 2024 am 08:54 AM

To successfully deploy and maintain a PHP website, you need to perform the following steps: Select a web server (such as Apache or Nginx) Install PHP Create a database and connect PHP Upload code to the server Set up domain name and DNS Monitoring website maintenance steps include updating PHP and web servers, and backing up the website , monitor error logs and update content.

How to use Fail2Ban to protect your server from brute force attacks How to use Fail2Ban to protect your server from brute force attacks Apr 27, 2024 am 08:34 AM

An important task for Linux administrators is to protect the server from illegal attacks or access. By default, Linux systems come with well-configured firewalls, such as iptables, Uncomplicated Firewall (UFW), ConfigServerSecurityFirewall (CSF), etc., which can prevent a variety of attacks. Any machine connected to the Internet is a potential target for malicious attacks. There is a tool called Fail2Ban that can be used to mitigate illegal access on the server. What is Fail2Ban? Fail2Ban[1] is an intrusion prevention software that protects servers from brute force attacks. It is written in Python programming language

See all articles