Use PM2 to deploy nodejs projects

Use PM2 to deploy nodejs projects.

If it is started directly through the node app, if an error is reported, the entire operation may be stopped directly, and the supervisor feels that it is only used as a development environment. Find pm2 online. It seems that the most common online deployment of nodejs projects are forever and pm2.
Use occasions:

  • supervisor is for the development environment.
  • Forever manages multiple sites, each site has a small amount of traffic and does not require monitoring.
  • The pm2 website has a large number of visits and requires a complete monitoring interface.

Main features of PM2:

  • Built-in load balancing (using the Node cluster cluster module)
  • Background process
  • 0 seconds downtime and reloading, I understand that it means that no downtime is required during maintenance and upgrades.
  • Startup scripts with Ubuntu and CentOS
  • Stop unstable processes (avoid infinite loops)
  • console detection
  • Provide HTTP API
  • Remote control and real-time interface API (Nodejs module, allows interaction with PM2 process manager)

Install

npm install -g pm2

usage

$ npm install -g pm2Command line global installation pm2
$ pm2 start app.jsstart app project
$ pm2 listLists all process information managed by pm2, and also shows how many times a process will be started because of unhandled exceptions.

 

Screenshot 2017-02-24 11.57.30.png


$ pm2 monitMonitor the CPU and memory usage of each node process

 

 

Screenshot 2017-02-24 11.58.46.png


$ pm2 logsshow all process logs
$ pm2 stop allstop all processes
$ pm2 restart allrestart all processes
$ pm2 reload allfor 0 seconds stop reload process (for NETWORKED processes)
$ pm2 stop 0stop specified process
$ pm2 restart 0restart specified process
$ pm2 startupspawn init script keep process alive
$ pm2 webrun robust computer API endpoint ( http://localhost:9615 )
$ pm2 delete 0kills the specified process
$ pm2 delete allkills all processes

 

Different ways to run processes:
$ pm2 start app.js -i maxstart max number of processes based on the number of available CPUs
$ pm2 start app.js -i 3start 3 processes
$ pm2 start app.js -xstart app.js in fork mode instead of cluster
$ pm2 start app.js -x -- -a 23start app.js in fork mode and pass the parameter (-a 23)
$ pm2 start app.js --name serveroneto start a process and put it Named serverone
$ pm2 stop serveroneto stop the serverone process
$ pm2 start app.jsonstart the process, set options
$ pm2 start app.js -i max -- -a 23in app.json pass parameters to app.js after --
$ pm2 start app.js -i max -e err.log -o out.logstart and generate a configuration file

Configure pm2 startup file

Add a processes.json in the project root directory: the
content is as follows:

{
  "apps": [
    {
      "name": "mywork",
      "cwd": "/srv/node-app/current",
      "script": "bin/www",
      "log_date_format": "YYYY-MM-DD HH:mm Z",
      "error_file": "/var/log/node-app/node-app.stderr.log",
      "out_file": "log/node-app.stdout.log",
      "pid_file": "pids/node-geo-api.pid",
      "instances": 6,
      "min_uptime": "200s",
      "max_restarts": 10,
      "max_memory_restart": "1M",
      "cron_restart": "1 0 * * *",
      "watch": false,
      "merge_logs": true,
      "exec_interpreter": "node",
      "exec_mode": "fork",
      "autorestart": false,
      "vizion": false
    }
  ]
}

illustrate:

  • apps: json structure, apps is an array, each array member corresponds to an application running in pm2
  • name: application name
  • cwd: the directory where the application is located
  • script: The script path of the application
  • log_date_format:
  • error_file: Custom application's error log file
  • out_file: custom application log file
  • pid_file: The pid file of the custom application
  • instances:
  • min_uptime: The minimum running time, the setting here is 60s, that is, if the application exits within 60s, pm2 will consider the program to exit abnormally, and trigger a restart at this time. Max_restarts sets the number of
  • max_restarts: Set the number of times the application exits and restarts abnormally, the default is 15 times (counting from 0)
  • cron_restart: start regularly to solve problems that can be solved by restarting
  • watch: Whether to enable monitoring mode, the default is false. If set to true, pm2 will automatically reload when the application changes. Here you can also set the files you want to monitor.
  • merge_logs:
  • exec_interpreter: The script type of the application, the shell used here, the default is nodejs
  • exec_mode: Application startup mode, here is cluster_mode (cluster), the default is fork
  • autorestart: Enable/disable auto restart on app crash or exit
  • vizion: enable/disable vizion features (version control)

can pm2 start processes.jsonbe activated by .
You can also write commands in package.json. as follows:

Screenshot 2017-02-24 12.14.07.png

Start by npm run pm2.

 



Author: passerbyli
Link: https://www.jianshu.com/p/d2a640b8661c
Source : Jianshu The
copyright belongs to the author. For commercial reprints, please contact the author for authorization, and for non-commercial reprints, please indicate the source.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324995544&siteId=291194637