Nodejs Deployment Mode-PM2

Source: Internet
Author: User
Tags xquery

Tag: deb run href chart script top pts code

At present, there are many pain points in the development of Nodejs, one of which is that after modifying the code we need to restart the service to see the effect. In this way, the killing process, restart, kill process, restart very big head. Programmers are the most hated species of repetitive work, and the students who have known it before May know it forever . forevercan help us solve the above problem, through the changes in the resources to change the automatic restart after the changes. Development phase We use node file.js to start another because of the Nodejs single-threaded, any exception will cause the entire service interruption, which for the production of the service for a long time the program is not possible, forever can help us restart after the exception, to ensure that the service has been online, I think this is the origin of its name it. But what I want to say is forever not enough "high!" Big! On ”。 Next I'm going to introduce a big enough artifact pm2 .

Brief introduction

pm2= P (rocess) M (Anager) 2, is a process management tool that can be used in a production environment Nodejs, and it has a load balancer built in. It not only guarantees that the service will not be interrupted all the time, but also provides 0 seconds of reload function, as well as a series of other process management and monitoring functions. And it's very easy to use. Below I will share my use of the process, Nodejs application is an express 4.x based application, the name is Wolverine .

Installation

Environment Checklist:

    • Windows7 x64
    • Node v5.0.0
    • NPM 3.3.6

Global Installation pm2

Install Pm2-g

Update

Update
Start

The previous start-up Wolverine is scripts implemented using Package.json, which can be started just by executing npm run start it and configured as follows:

"Scripts": {    "node./bin/www",    "node Debug./bin/www"  }, 

Using pm2 we can be configured at start pm2 ./bin/www , the command back to support the addition of parameters to implement watch, cluster multi-process mode and other functions. I don't really like a bunch of commands, so I used the configuration file.

In Wolverine the root directory, I created a processes.json configuration file, the contents of the configuration file are as follows, and the comments are clearly written.

{"Apps": [{"Name":"Wolverine",Name"Script"://program inbound  "CWD": //root  "watch": [ "Bin",  "public ", " routes ", " views "],//the directory to be monitored Span class= "hljs-string" > "Error_file":  "./logs/app-err.log", //error output log  "Out_file": //log " Log_date_format ":  "Yyyy-mm-dd hh:mm Z" //date format}]}       

Later, I added a piece of Package.json in the

"PM2 Start Processes.json"

Just enter the following command directly on startup:

Run PM2

See the following interface, it started successfully, and then we can turn off the window, the service will not stop, is not much taller.

Management and Monitoring

Starting a successful interface will show the app name and ID, which are important values. Of course, both values can be configured in the Processes.json configuration file.

Open the command line, under any path, enter

$ PM2 List

You can see the graphical interface at the start-up, allowing us to view all Nodejs services managed by PM2.

Input, the following command mate ID or name to view the details of a process

0

Content related to restart times, run time, script path, parameters, log path, run mode, and more

Input

$ PM2 Monit

Stop, restart, etc commands

Stop [app-name|  ID]  #停止某一个进程, you can use app-name or delete [app-name|  Delete all #删除并停止所有进程    

You can further see the CPU and memory usage of each service.

Log monitoring

If you have been using it tail -f log_file.log log_error.log to view the logs, you may be in love with this feature below.

$ PM2 logs$ PM2 logs [App-name]

We can see the logs of all processes in real time, or just one. We can even use the JSON format to view the logs.

--json
Web API

If you want to monitor not only the processes that are managed by PM2, but also the machines that the process runs on, you can use the following API

$ PM2 Web

PM2 will start a process called Pm2-http-interface to provide Web services. You open the browser input http://127.0.0.1:9615, is not seen by the results of stunning.

PM2 provides a Web API that outputs a lot of information through JSON. The approximate structure can be seen:

Come up with your imagination, we can develop an application to invoke this API, you can develop a graphical interface of the monitoring software ...

Nodejs Deployment Mode-PM2

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.