PM2 介紹 啓動Nodejs 框架Express項目

文章主要來自豆瓣上一位同學的文章:PM2 介紹,中間可能有部分修改!

介紹

PM2 是一個帶有負載均衡功能的 Node 應用的進程管理器。

當你要把你的獨立代碼利用全部的服務器上的所有 CPU,並保證進程永遠都活着,0 秒的重載, PM2 是完美的。它非常適合 IaaS 結構,但不要把它用於 PaaS 方案(隨後將開發 Paas 的解決方案)。

備註:

  • SaaS、PaaS 和 IaaS 是雲服務模式
  • SaaS 軟件即服務,例如 Google 的 Gmail 郵箱服務,面向應用型用戶
  • PaaS 平臺即服務,例如 Google 的 GAE,面向開發型用戶
  • IaaS 基礎架構即服務,例如亞馬遜的 AWS,IaaS 對於不知道新推出的應用程序/網站會有多成功的創業公司來說非常有用

請參考

雲服務模式:SaaS、PaaS 和 IaaS,哪一種適合你?

主要特性

  • 內建負載均衡(使用 Node cluster 集羣模塊)
  • 後臺運行
  • 0 秒停機重載,我理解大概意思是維護升級的時候不需要停機.
  • 具有 Ubuntu 和 CentOS 的啓動腳本
  • 停止不穩定的進程(避免無限循環)
  • 控制檯檢測
  • 提供 HTTP API
  • 遠程控制和實時的接口 API ( Nodejs 模塊,允許和 PM2 進程管理器交互 )

測試過 Nodejs v0.11/v0.10/v0.8 版本,兼容 CoffeeScript,基於 Linux 和 MacOS。

安裝

npm install -g pm2

用法

$ npm install pm2 -g     # 命令行安裝 pm2 
$ pm2 start app.js -i 4  # 後臺運行pm2,啓動4個app.js 
                         # 也可以把'max' 參數傳遞給 start
                         # 正確的進程數目依賴於Cpu的核心數目
$ pm2 start app.js --name my-api # 命名進程
$ pm2 list               # 顯示所有進程狀態
$ pm2 monit              # 監視所有進程
$ pm2 logs               # 顯示所有進程日誌
$ pm2 stop all           # 停止所有進程
$ pm2 restart all        # 重啓所有進程
$ pm2 reload all         # 0 秒停機重載進程 (用於 NETWORKED 進程)
$ pm2 stop 0             # 停止指定的進程
$ pm2 restart 0          # 重啓指定的進程
$ pm2 startup            # 產生 init 腳本 保持進程活着
$ pm2 web                # 運行健壯的 computer API endpoint (http://localhost:9615)
$ pm2 delete 0           # 殺死指定的進程
$ pm2 delete all         # 殺死全部進程

運行進程的不同方式

$ pm2 start app.js -i max    # 根據有效CPU數目啓動最大進程數目
$ pm2 start app.js -i 3      # 啓動3個進程
$ pm2 start app.js -x        #用fork模式啓動 app.js 而不是使用 cluster
$ pm2 start app.js -x -- -a 23   # 用fork模式啓動 app.js 並且傳遞參數 (-a 23)
$ pm2 start app.js --name serverone  # 啓動一個進程並把它命名爲 serverone
$ pm2 stop serverone       # 停止 serverone 進程
$ pm2 start app.json        # 啓動進程, 在 app.json裏設置選項
$ pm2 start app.js -i max -- -a 23                   #在--之後給 app.js 傳遞參數
$ pm2 start app.js -i max -e err.log -o out.log  # 啓動 並 生成一個配置文件
你也可以執行用其他語言編寫的app  ( fork 模式):
$ pm2 start my-bash-script.sh    -x --interpreter bash
$ pm2 start my-python-script.py -x --interpreter python

0 秒停機重載:這項功能允許你重新載入代碼而不用失去請求連接。

注意:

  • 僅能用於 web 應用
  • 運行於 Node 0.11.x 版本
  • 運行於 cluster 模式(默認模式)
$ pm2 reload all

CoffeeScript:

$ pm2 start my_app.coffee  #這就是全部

PM2 準備好爲產品級服務了嗎?

只需在你的服務器上測試

$ git clone https://github.com/Unitech/pm2.git
$ cd pm2
$ npm install  # 或者 npm install --dev ,如果devDependencies 沒有安裝
$ npm test

pm2 list:列出由 PM2 管理的所有進程信息,還會顯示一個進程會被啓動多少次,因爲沒處理的異常。

pm2 monit:監視每個 node 進程的 CPU 和內存的使用情況。

啓動項目

進入項目目錄,執行以下命令

pm2 start app.js

會出現以下錯誤信息,該如何解決呢?

PM2        | 2017-10-26 10:45:21: Starting execution sequence in -fork mode- for app name:app id:0
PM2        | 2017-10-26 10:45:21: App name:app id:0 online
PM2        | 2017-10-26 10:45:21: App [app] with id [0] and pid [17365], exited with code [0] via signal [SIGINT]
PM2        | 2017-10-26 10:45:21: Starting execution sequence in -fork mode- for app name:app id:0
PM2        | 2017-10-26 10:45:21: App name:app id:0 online
PM2        | 2017-10-26 10:45:22: App [app] with id [0] and pid [17375], exited with code [0] via signal [SIGINT]
PM2        | 2017-10-26 10:45:22: Starting execution sequence in -fork mode- for app name:app id:0
PM2        | 2017-10-26 10:45:22: App name:app id:0 online
PM2        | 2017-10-26 10:45:22: App [app] with id [0] and pid [17385], exited with code [0] via signal [SIGINT]
PM2        | 2017-10-26 10:45:22: Starting execution sequence in -fork mode- for app name:app id:0
PM2        | 2017-10-26 10:45:22: App name:app id:0 online
PM2        | 2017-10-26 10:45:23: App [app] with id [0] and pid [17395], exited with code [0] via signal [SIGINT]
PM2        | 2017-10-26 10:45:23: Script /home/nodeWork/regalcoin/app.js had too many unstable restarts (16). Stopped. "errored"
PM2        | 2017-10-26 10:50:51: Starting execution sequence in -fork mode- for app name:www id:1
PM2        | 2017-10-26 10:50:51: App name:www id:1 online


在當前項目目錄,執行

pm2 start ./bin/www

 

成功運行起來

 

啓動項目

pm2 start www(App name)

停止項目

pm2 stop www

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章