Web Front-end
JS Tutorial
Teach you step by step how to install and configure pm2 to realize automatic deployment of node projectsTeach you step by step how to install and configure pm2 to realize automatic deployment of node projects
How does
pm2 automatically deploy nodejs projects? The following article will introduce to you how to install and configure pm2, and implement automatic deployment of node projects. I hope it will be helpful to you!

1. Introduction to pm2
pm2 (process manager) is a process management tool that maintains a process list and can be used to manage your node process is responsible for all running processes and checks the status of the node process. It also supports performance monitoring, load balancing and other functions.
1.1. Benefits of using node programs managed by pm2
Monitor file changes and automatically restart the program
-
Support performance monitoring
Load balancing
Automatic restart after program crash
Server restart Automatically restart at startup
Automated deployment project
2. PM2 installation and use
2.1.Global Installation
npm install pm2 -g
2.2. Common commands
-
Start a node program
pm2 start start.js

-
Start the process and specify the application program name
pm2 start app.js --name application1
-
Cluster mode startup
// -i 表示 number-instances 实例数量 // max 表示 PM2将自动检测可用CPU的数量 可以自己指定数量 pm2 start start.js -i max

-
Add process monitoring
// 在文件改变的时候会重新启动程序 pm2 start app.js --name start --watch

-
List All processes
pm2 list pm2 ls // 简写 -
Delete processes from the process list
// pm2 delete [appname] | id pm2 delete app // 指定进程名删除 pm2 delete 0 // 指定进程id删除 -
Delete all processes from the process list
pm2 delete all
-
View the specific situation of a process
pm2 describe app

- ##View the resource consumption of a process
pm2 monit

- Restart the process
pm2 restart app // 重启指定名称的进程 pm2 restart all // 重启所有进程 - View the process log
pm2 logs app // 查看该名称进程的日志 pm2 logs all // 查看所有进程的日志 - Set pm2 to start automatically at boot
pm2 startup centosThen follow the prompts to enter the commands requiredFinally save the settings
pm2 save3. Automatically deploy the project through the pm2 configuration fileMake sure pm2 is installed on the server. If not installed, install it first
npm install pm2 -g
3.1 Create a new deploy.yaml file in the project root directory# deploy.yaml
apps:
- script: ./start.js # 入口文件
name: 'app' # 程序名称
env: # 环境变量
COMMON_VARIABLE: true
env_production:
NODE_ENV: production
deploy: # 部署脚本
production: # 生产环境
user: lentoo # 服务器的用户名
host: 192.168.2.166 # 服务器的ip地址
port: 22 # ssh端口
ref: origin/master # 要拉取的git分支
ssh_options: StrictHostKeyChecking=no # SSH 公钥检查
repo: https://github.com/**.git # 远程仓库地址
path: /home # 拉取到服务器某个目录下
pre-deploy: git fetch --all # 部署前执行
post-deploy: npm install && pm2 reload deploy.yaml --env production # 部署后执行
env:
NODE_ENV: production
3.2 Configure git SSH password-free authentication
- Generate rsa public key and private key in the server, currently it is performed under
centos7
- The prerequisite server must install git. If it is not installed, install git first. If it is already installed, skip it.
yum –y install git
- Generate the secret key
ssh-keygen -t rsa -C "xxx@xxx.com"
In ~/.ssh There are two files, id_rsa and id_rsa.pub, in the directory. The id_rsa.pub file stores the public key. - Log in to GitHub, click the avatar on the upper right, select settings, click Add SSH key, and copy the contents of id_rsa.pub into it.

Submit the local code to the remote before each deployment git warehouse
- First deployment
-
pm2 deploy deploy.yaml production setup
After the deployment is completed, you can log in to the server to check whether the project has been pulled from git in the configured directory
- Deploy again
-
pm2 deploy deploy.yaml production update
such as vue- The cli project is automatically deployed to the server, and the npm run build command is automatically executed. The generated dist directory is assigned to the static file directory of nginx.
4. Possible pitfalls
- Deployment failure occurs when the command line program of vscode under the window system and powershell execute the deployment command
-
pm2 deploy deploy.yaml production setup
Solution: Use the git command line to execute
For more node-related knowledge, please visit:
nodejs tutorialThe above is the detailed content of Teach you step by step how to install and configure pm2 to realize automatic deployment of node projects. For more information, please follow other related articles on the PHP Chinese website!
Python vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AMPython and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.
From C/C to JavaScript: How It All WorksApr 14, 2025 am 12:05 AMThe shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.
JavaScript Engines: Comparing ImplementationsApr 13, 2025 am 12:05 AMDifferent JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.
Beyond the Browser: JavaScript in the Real WorldApr 12, 2025 am 12:06 AMJavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.
Building a Multi-Tenant SaaS Application with Next.js (Backend Integration)Apr 11, 2025 am 08:23 AMI built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing
How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration)Apr 11, 2025 am 08:22 AMThis article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base
JavaScript: Exploring the Versatility of a Web LanguageApr 11, 2025 am 12:01 AMJavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.
The Evolution of JavaScript: Current Trends and Future ProspectsApr 10, 2025 am 09:33 AMThe latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.


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

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

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

Atom editor mac version download
The most popular open source editor

Dreamweaver CS6
Visual web development tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function






