


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

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr


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

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

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1
Easy-to-use and free code editor

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Chinese version
Chinese version, very easy to use

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