Nodejs build a production environment under Linux - and the use of nodejs Process Manager pm2

nodejs installation: https://www.cnblogs.com/loaderman/p/11596661.html

nodejs Process Manager pm2 use

PM2 is a very good Node process management tool, it has a rich set of features: the ability to take full advantage of multi-core CPU and is capable of load balancing, application can help after a crash, the specified time (cluster model) and exceeded the maximum memory restrictions in the case realization automatic restart.

PM2 is an open source-based Nodejs process manager, including daemons, monitoring, complete set of features log.

PM2 main features :

1 , built-in load balancing (using Node cluster cluster module)
2 , running in the background
3 , 0 Miao stop overloaded, I understand the general idea is to maintain the upgrade when no downtime . 4 , with Ubuntu and CentOS startup scripts 5 , stops unstable process (to avoid infinite loop)
6 , the console detects

PM2 of common commands :
1 , PM2 installation

npm install pm2 -g # command-line installation pm2

2 , run pm2 program and specify the name

pm2 start app.js --name www_aaa_com
Start app.js PM2 -i 3 --name www_bbb_com 3 start three processes (comes with load balancing)

 

3 , showing the status of all processes

pm2 list

4 , showing the status of all processes

pm2 logs

 

5, a process of displaying the log

 

Log pm2 logs www_aaa_com

 

6 , close restart all processes

pm2 stop all # stop all processes
pm2 restart all # restart all processes
All # reload PM2 0 Miao overload shutdown process (for NETWORKED process)

7 , turn off the restart of the specified process

STOP 0 PM2 # stops the specified process
restart PM2 0 # restart the specified process
pm2 stop www_aaa_com
pm2 restart www_aaa_com

8 , kill the process

the Delete 0 M2    # kill the specified process
pm2 delete all # kill all processes
pm2 delete www_aaa_com # kill the process specified name

10 , shows the corresponding process / general information application

pm2 show www_aaa_com

 Test demonstration Description:

To the next home, and create www_aaa_com www_bbb_com folders are copied into the project code

With cnpm (gzip compression support) command-line tool instead of the default npm:

Elevation install g cnpm --registry = https: // registry.npm.taobao.org

Then cnpm install depend on the installation environment

www_aaa_com app.js Code:

// pulling-koa Mo块

Var Koa = Require ( 'koa' );

Router var = the require ( 'KOA-Router') ();   / * incorporated instantiating recommended route ** * /

// instantiate 
var App = new new Koa ();

router.get('/',async (ctx)=>{

    the console.log ( 'AAAA execution' );
    ctx.body = "This is www_aaa_com home" ;

})

router.get('/news',async (ctx)=>{
    ctx.body = "This is www_aaa_com news list page" ;

})

app.use (router.routes ());    / * start routing * /
app.use(router.allowedMethods());
app.listen(3001);

www_bbb_com in app.js Code:

// pulling-koa Mo块

Var Koa = Require ( 'koa' );

Router var = the require ( 'KOA-Router') ();   / * incorporated instantiating recommended route ** * /

// instantiate 
var App = new new Koa ();

router.get('/',async (ctx)=>{

    the console.log ( 'BBB execution' );
    ctx.body = "This is www_bbb_com home 111111111111" ;

})

router.get('/news',async (ctx)=>{
    ctx.body = "This is www_bbb_com news list page" ;

})

app.use (router.routes ());    / * start routing * /
app.use(router.allowedMethods());
app.listen(3002);

Turn off the firewall

systemctl stop firewalld 

In the corresponding directory, respectively, to start the project with pm2

pm2 start app.js --name www_aaa_com   
pm2 start app.js --name www_bbb_com

 

 

Guess you like

Origin www.cnblogs.com/loaderman/p/11654348.html
Recommended