1. Introduction to PM2

Process Manager (PM2) is a process management tool. It maintains a process list. You can use it to manage your Node processes, take charge of all running processes, and check the status of node processes.

1.1. Benefits of using PM2 managed Node applications

  1. Listen for file changes and restart the program automatically
  2. Performance Monitoring
  3. Load balancing
  4. The program crashes and restarts automatically
  5. The server automatically restarts when it restarts
  6. Automated deployment Project

2. Pm2 installation and use

2.1. Global Installation

npm install pm2 -g 
Copy the code

2.2 Common Commands

  • Start a Node program

        pm2 start start.js
    Copy the code

  • Start the process and specify the application name

        pm2 start app.js --name application1
    Copy the code
  • Cluster mode Startup

    // -i specifies the number of instances. // Max indicates that PM2 automatically detects the number of available cpus. You can specify the number by yourselfCopy the code

  • Adding Process Monitoring

    Pm2 start app.js --name start --watchCopy the code

  • List all processes

    Pm2 list pm2 ls //Copy the code
  • Deletes a process from the process list

    / / pm2 delete (appname) | id pm2 delete app/delete/specify the process name pm2 delete 0 / delete/specify the process idCopy the code
  • Delete all processes from the process list

    Pm2 delete all Copies the codeCopy the code
  • View details about a process

        pm2 describe app
    Copy the code

  • View the resource consumption of a process

        pm2 monit
    Copy the code

  • Restart the process

    Pm2 restart app // Restart the specified process pm2 restart all // Restart all processesCopy the code
  • Viewing Process Logs

    Pm2 logs app // View the logs of this process. Pm2 logs all // View the logs of all processesCopy the code
  • Set the PM2 to start automatically upon startup

Enable startup Settings for CentOS. Replace the last option for other systems (optional: ubuntu, CentOS, redhat, gentoo, systemd, Darwin, amazon).

    pm2 startup centos 
Copy the code

Then enter the command as prompted

And then save the Settings

pm2 save
Copy the code

3. Automatically deploy projects from pM2 profiles

Make sure pM2 is installed on the server, or if not, install it first

npm install pm2 -g
Copy the code

3.1 Create a deploy.yaml file in the root directory of the project

Yaml apps: -script:./start.js # import file name: 'app' # env: # environment variable COMMON_VARIABLE: true env_production: NODE_ENV: production deploy: # production script production: # lentoo # server username host: 192.168.2.166 # server IP address port: Ssh_options: StrictHostKeyChecking=no # SSH public key checking repo: https://github.com/**.git # remote repository address path: /home # pull the directory to a directory on the server pre-deploy: git fetch --all # execute the post-deploy: Yaml --env production # execute env: NODE_ENV: productionCopy the code

3.2 Configuring SSH Encryption Authentication for Git

  1. Generate rsa public and private keys on the server. Currently, this command is performed in centos7

  2. Git must be installed on the server. If it is not installed, install it first. If it is installed, skip it

    Yum -y install gitCopy the code
  3. To generate the secret key

        ssh-keygen -t rsa -C "[email protected]"
    Copy the code

    SSH contains two files, id_rsa and id_rsa.pub. The id_rsa.pub file contains the public key.

  4. Log in to GitHub, click on your profile picture in the upper right, select Settings, hit Add SSH Key, and copy the contents of id_rsa.pub into it.

3.3 Deploying the Project using PM2

Commit your local code to a remote Git repository before each deployment

  • The first deployment

    Yaml production setup Copies the code

After the deployment is complete, you can log in to the server to check whether the project has been pulled from git under the configured directory

  • Deploy again

    Yaml production update Copies the code

3.4 This deployment process also applies to front-end projects

For example, vue-CLI project, automatically deploy to the server, automatically execute the NPM run build command, generate dist directory, specified to nginx static file directory.

4, may encounter pits

  • When vscode and Powershell execute the deployment command in Windows, the deployment fails

    Yaml production setup Copies the code

Solution: Use the git command line to do it