Home > Web Front-end > JS Tutorial > body text

Teach you step by step how to install and configure pm2 to realize automatic deployment of node projects

青灯夜游
Release: 2021-12-29 19:16:18
forward
10461 people have browsed it

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!

Teach you step by step how to install and configure pm2 to realize automatic deployment of node projects

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

2.2. Common commands

  • Start a node program

        pm2 start start.js
    Copy after login

Teach you step by step how to install and configure pm2 to realize automatic deployment of node projects

  • Start the process and specify the application program name

        pm2 start app.js --name application1
    Copy after login
  • Cluster mode startup

        // -i 表示 number-instances 实例数量
        // max 表示 PM2将自动检测可用CPU的数量 可以自己指定数量
        pm2 start start.js -i max
    Copy after login

Teach you step by step how to install and configure pm2 to realize automatic deployment of node projects

  • Add process monitoring

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

Teach you step by step how to install and configure pm2 to realize automatic deployment of node projects

  • List All processes

        pm2 list
        pm2 ls // 简写
    Copy after login
  • Delete processes from the process list

        // pm2 delete [appname] | id
        pm2 delete app  // 指定进程名删除
        pm2 delete 0    // 指定进程id删除
    Copy after login
  • Delete all processes from the process list

        pm2 delete all
    Copy after login
  • View the specific situation of a process

        pm2 describe app
    Copy after login

Teach you step by step how to install and configure pm2 to realize automatic deployment of node projects

  • ##View the resource consumption of a process

        pm2 monit
    Copy after login

Teach you step by step how to install and configure pm2 to realize automatic deployment of node projects

  • Restart the process

        pm2 restart app // 重启指定名称的进程
        pm2 restart all // 重启所有进程
    Copy after login

  • View the process log

        pm2 logs app    // 查看该名称进程的日志
        pm2 logs all    // 查看所有进程的日志
    Copy after login

  • Set pm2 to start automatically at boot

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

    pm2 startup centos
Copy after login

Then follow the prompts to enter the commands required

Finally save the settings

pm2 save
Copy after login

3. Automatically deploy the project through the pm2 configuration file

Make sure pm2 is installed on the server. If not installed, install it first

npm install pm2 -g
Copy after login
Copy after login

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

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

  • Generate the secret key

        ssh-keygen -t rsa -C "xxx@xxx.com"
    Copy after login

    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.

Teach you step by step how to install and configure pm2 to realize automatic deployment of node projects

##3.3 Use pm2 to deploy the project

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

    First deployment
  • pm2 deploy deploy.yaml production setup
    Copy after login
    Copy after login
  • 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
    Copy after login
3.4 This deployment process is also applicable to front-end projects

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

    Teach you step by step how to install and configure pm2 to realize automatic deployment of node projectsSolution: Use the git command line to execute

    For more node-related knowledge, please visit:

    nodejs tutorial

    ! !

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

Related labels:
source:juejin.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template