Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to install and configure pm2 to automate deployment of node projects

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to install and configure pm2 to automate the deployment of node projects. I hope you will get something after reading this article. Let's discuss it together.

1. Introduction to pm2

Pm2 (process manager) is a process management tool that maintains a list of processes that you can use to manage your node processes, responsible for all running processes, and view the status of node processes. It also supports performance monitoring, load balancing and other functions.

1.1. benefits of using node programs managed by pm2

Listen for file changes and restart the program automatically

Support performance monitorin

Load balancing

Program crashes automatically restarts

Automatically restart when the server is restarted

Automated deployment project

2. Installation and use of pm2 2.1.Global installation of npm install pm2-g2.2, common commands

Start a node program

Pm2 start start.js

Start the process and specify the name of the application

Pm2 start app.js-name application1

Cluster mode startup

/ /-I indicates the number of number-instances instances / / max means that PM2 will automatically detect the number of available CPU. You can specify the number of pm2 start start.js-I max yourself.

Add process monitoring

/ / restart the program pm2 start app.js-- name start-- watch when the file changes

List all processes

Pm2 list pm2 ls / / abbreviation

Remove a process from the process list

/ / pm2 delete [appname] | id pm2 delete app / / specified process name deletion pm2 delete 0 / / specified process id deletion

Delete all processes in the process list

Pm2 delete all

View the details of a process

Pm2 describe app

View the resource consumption of the process

Pm2 monit

Restart the process

Pm2 restart app / / restart the specified process pm2 restart all / / restart all processes

View the process log

Pm2 logs app / / View the logs of processes with that name pm2 logs all / / View the logs of all processes

Set pm2 to boot automatically

Enable startup settings, here is the CentOS system, other systems replace the last option (optional: ubuntu, centos, redhat, gentoo, systemd, darwin, amazon)

Pm2 startup centos

Then enter according to the command that you need to enter.

Finally save the settings

Pm2 save3, automatically deploy the project through the pm2 configuration file

Make sure that pm2 is installed on the server, and install it first if not installed

Npm install pm2-g3.1 create a new deploy.yaml file # deploy.yamlapps:-script:. / start.js # entry file name: 'app' # Program name env: # environment variable COMMON_VARIABLE: true env_production: NODE_ENV: productiondeploy: # deployment script production: # production environment user: lentoo # user name of the server host: 192.168.2.166 # ip address of the server port: 22 # ssh port ref: origin/master # git branch to be pulled ssh_options: StrictHostKeyChecking=no # SSH public key check repo: Https://github.com/**.git # remote warehouse address path: / home # pull to a directory of the server pre-deploy: git fetch-- all # execute post-deploy: npm install & & pm2 reload deploy.yaml-- env production # perform ssh secret-free authentication of env: NODE_ENV: production3.2 configuration git after deployment

Generate rsa public and private keys in the server, currently under centos7

If you want to install git on the server, install git first if it is not installed, and skip those that have already been installed.

Yum-y install git

Generate secret key

Ssh-keygen-t rsa-C "xxx@xxx.com"

There are two files, id_rsa and id_rsa.pub, in the ~ / .ssh directory, and the public key key is stored in the id_rsa.pub file.

Log in to GitHub, click on the avatar at the top right, select settings, click Add SSH key, and copy the contents of id_rsa.pub into it.

3. 3 deploy the project using pm2

Submit the local code to the remote git repository before each deployment

First deployment

Pm2 deploy deploy.yaml production setup

After the deployment is completed, you can log in to the server to see if the project has been pulled from the git under the configured directory.

Deploy again

Pm2 deploy deploy.yaml production update3.4 the deployment process is also applicable to the front-end project

Projects such as vue-cli are automatically deployed to the server, the npm run build command is automatically executed, and the generated dist directory is specified to the static file directory of nginx.

4. The pit you may encounter

Deployment failure occurs when the command line program of vscode under window system and powershell executes the command of deployment.

Pm2 deploy deploy.yaml production setup

Solution: use the git command line to execute

After reading this article, I believe you have some understanding of "how to install and configure pm2 to automate the deployment of node projects". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report