1, Usage scenario
When deploying and publishing the node.js project, SSH is often used to connect multiple servers respectively. For each server, the latest git pull code should be executed to compile and start the application. The operation is trivial and easy to forget some of them. pm2 can solve this problem. It can be completed automatically with only one command. It can also monitor and view the operation of node.js application in real time.
2, Solution
Step 1: server environment configuration
- Install node.js: No, look here!
- Install PM2: npm install pm2@latest -g or yarn global add pm2
- Install git: No, look here
- Configure SSH so that the server can run git clone without secret, No, look here!
Step 2: develop machine environment configuration
- Install node.js: No, look here!
- Install PM2: npm install pm2@latest -g or yarn global add pm2
- Configure SSH, log in to the server without secret, No, look here!
Step 3: node.js project operation step configuration
Open the terminal, switch to the root directory of node.js project, execute pm2 ecosystem, and generate the configuration file as follows:
// ecosystem.config.js module.exports = { apps: [{ script: 'index.js', watch: '.', env_production: { NODE_ENV: "production" }, env_development: { NODE_ENV: "development" } }], // Deployment Configuration deploy: { production: { user: 'SSH_USERNAME', host: 'SSH_HOSTMACHINE', ref: 'origin/master', repo: 'GIT_REPOSITORY', path: 'DESTINATION_PATH', 'pre-deploy-local': '', 'post-deploy': 'npm install && pm2 reload ecosystem.config.js --env production', 'pre-setup': '' } } }
- Modify the above configuration file to correct the server and git related information;
Execute the following command in the directory of ecosystem.config.js to initialize the node.js project to the server
pm2 deploy production setup // pm2 connects to the server and notifies it of initialization such as git clone code.
Start or update the node.js project:
pm2 deploy production --force; // Let the server git pull code, compile and start the application // --Function of force: if the local code of the server is changed, give up the change and update the project with the latest code of git warehouse.
3, Detailed explanation of deploy command
1. Command format
pm2 deploy <configuration_file> <environment> <command>
2,configuration_file
If the configuration file name is ecosystem.config.js or pm2.config.js, the above command does not need to write < configuration_ file>.
pm2 deploy production setup // This command is executed by default by looking for the ecosystem.config.js or pm2.config.js configuration file
3,environment
Set env in configuration file_ Production and env_development, you can call it when executing the command and pass the environment variable to the node.js project: pm2 deploy production --force;
// ecosystem.config.js module.exports = { apps: [{ script: 'index.js', watch: '.', env_production: { NODE_ENV: "production" }, env_development: { NODE_ENV: "development" } }], }
4,command
setup run remote setup commands update update deploy to the latest release revert[n] revert to[n]th last deployment or 1 curr[ent] output current release commit prev[ious] output previous release commit exec | run < cmd > execute the given < cmd > list list previous deploy commits [ref] deploy to[ref], the "ref" setting, or latest tag
4, Other important matters
1. exec allows all servers to execute commands once
pm2 deploy production exec "pm2 reload all"
2. Rollback to previous deployment version
pm2 deploy production revert 1
3. Several time points of deployment
"pre-setup" : "stay setup A command or script that runs before execution", "post-setup" : "In code clone Command or script to execute after completion", "pre-deploy" : "pm2 startOrRestart ecosystem.json --env production", "post-deploy" : "pm2 startOrRestart ecosystem.json --env production", "pre-deploy-local" : "echo 'This is a local executed command'"
4. To operate multiple servers, you only need to modify the host
"host" : ["212.83.163.1", "212.83.163.2", "212.83.163.3"],
5, Error reporting
1. Error message
npm: command not found post-deploy hook failed Deploy failed
2. Solution
During pm2 deployment, the above error occurs, but npm -v is normal. The reason is the pm2 configuration file. Follow the steps below to solve it.
# 1. Open terminal $ cd ~ $ nano .bashrc # 2. Comment out the following #If not running interactively, don 't do anything case $ - in * i * );; *) return;; esac # 3. Update environment variables $ source .bashrc