Getting Started with PM2

Source: Internet
Author: User
Getting started with PM2 Description

PM2 is a node process management tool that includes a complete set of feature installations , including daemons, monitoring, logging, etc.

NPM Install Pm2-g
Command

First, you have a node file that you can start

Const HTTP = require (' http ');

HTTP
. Createserver (function (req, res) {
    res.writehead ({' Content-type ': ' Text/plain '});
    Res.end (' is Test vi ';
})
. Listen (8081);

Console.log (' Server runing on http://47.xx.139.xx:8081 ');
1. Start
# Simple Start by default will generate a app called apps run on the corresponding port 8081
pm2 start app.js

# while listening for file changes, automatic restart after a change (--watch)
pm2 start App.js--wat CH

# Load Balancing Start multiple instances (-i num) num can be a specified number or Max indicates that the
pm2 start app.js-i max--watch 

# Custom application name (-n XXX) is determined according to the current number of machine cores 
  PM2 start app.js-i max--watch-n app-2 # Open App Name for app-2 application

# define standard output log path and error log output path
pm2 start App.js-o./ Logs/out.log-e./logs/error.log
2. Stop
# AppName and AppId can see through the PM2 list
pm2 stop appname|appid

# Stop all applications
PM2 stop all
3. reboot
4. Delete
# After the stop can be restarted through the restart, delete cannot restart 
pm2 Delete appname|appid
5. View Current process status
PM2 List
6. View process Details
PM2 Show Appname|appid
7. View Log
PM2 logs Appname|appid
8. Access to the performance panel to view CPU memory, etc. occupancy
PM2 Monit
Configuring Process Files
# root directory Write configuration file Module.exports = {apps: [//apps can be configured with multiple applications {name: ' Expres S-demo ',//Application name script: ' App.js ',//Startup script CWD: './',//current work path watch:true,//monitor changes in the eye Record Exec_mode: ' cluster ',//Cluster mode instances: ' Max ',//number of instances Max_memory_restart: ' 40M ',/ /If you exceed the specified memory, reboot ignore_watch: [//Exclude ' node_modules ' from the Monitor directory, ' logs ']
            , Error_file: './logs/app-err.log ',//error log path Out_filel: './logs/app-out.log ',//normal log path ENV: {//default environment variable node_env: ' Production '//Environment variable}, Env_dev: {//the command line can specify the current environment variable to be
            Dev node_env: ' Development '}, Env_prod: {node_env: ' production ' }],//deploy: {} deployment Configuration} 
# command line start
pm2 start ecosystem.config.js--env Dev

More Configuration website View PM2 implementation node online deployment

Step: Log on to the server at the local command line through the local PM2 tool, and then notify the server to deploy code clone to the server from the Git warehouse to the appropriate folder, and wait for further action 1. Local environment configuration ensure that git, node, PM2 are installed locally To report a local NODEJS application (This example is a express service) has a third party Git platform managed code address: Git@gitee.com:mjzhang/node-website.git 2. The server generates an SSH public key and Join Git platform

# server
# sequentially executes
ssh-keygen-t rsa-b 4096-c "youxiang@163.com" all the way to the

eval "$ (ssh-agent-s)"

Ssh-add ~/.ssh /id_rsa

Cat ~/.ssh/id_rsa.pub # Display public key

# Copy public key to third party git platform
3. Nodejs Project root directory write PM2 configuration file ecosystem.config.js
Local module.exports = {apps: [{name: ' Express-demo ',//Application name script: ' App.js ',
            Start script CWD: './',//current work path watch:true,//Monitor changes directory Exec_mode: ' cluster ',//cluster mode Instances: ' Max ',//number of instances Max_memory_restart: ' 40M ',///If more than specified memory is restarted ignore_watch: [//From Supervisor  The control catalogue excludes ' node_modules ', ' Logs ', Error_file: './logs/app-err.log ', Error log path Out_filel: './logs/app-out.log ',//Normal log path env: {//default environment variable node_env: '
            Production '//Environment variable}, Env_dev: {//Command line can specify current environment variable for dev node_env: ' Development '  }, Env_prod: {node_env: ' Production '}}], deploy: {// Deployment Configuration prod: {//corresponding environment name User: ' MJZ ', host: [' 47.94.139.47 '],//server IP port to deploy
      : ' 39999 ',//port number      Path: '/www/website ',//cloned to the server path repo: ' Git@gitee.com:mjzhang/node-website.git ',//warehouse address Ref: ' Origin/master ',//Code Warehouse Branch ssh_options: ' Stricthostkeychecking=no ',//Cancel key checksum ' Post-deplo Y ': ' npm install && pm2 startorrestart ecosystem.config.js--env prod ' env: {node_env : ' Production '}}}
4. Submit project to Git platform
# local
git Add.
git commit-m ' xxx '
git push Origin master
5. Create a folder in the server to receive project files
# server
# Create/www/website
cd/www
sudo chmod 777 website # Modify folder permissions are read-writable, or the next step will report an error-No permissions modified
6. Local Run command implementation deployment

After a successful deployment,/www/website three folders current folder, currently running folder source source code shared log file

The ' Post-deploy ' in the configuration file ecosystem.config.js: ' npm install && pm2 startorrestart ecosystem.config.js--env ' can make After the code is cloned to the server, it runs 7. Routine maintenance process change code submitted to git version library git Add. && git commit-m ' xxx ' && git push origin master PM2 republish PM2 deploy ecosystem.config.js PROD server Live view log pm 2 Monit

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.