node專案部署伺服器(pm2)
主要應用工具 pm2部署伺服器
pm2的優點
- 內建負載均衡(使用Node cluster 叢集模組)
- 後臺執行
- 0秒停機過載,我理解大概意思是維護升級的時候不需要停機.
- 具有Ubuntu和CentOS 的啟動指令碼
- 停止不穩定的程序(避免無限迴圈)
- 控制檯檢測
- 提供 HTTP API
- 遠端控制和實時的介面API ( Nodejs 模組,允許和PM2程序管理器互動 )
安裝pm2
sudo npm install -g pm2
啟動專案
cd 專案檔案目錄
pm2 start bin/www #啟動專案
┌──────────┬────┬──────┬─────┬────────┬───────────┬────────┬─────────────┬──────────┐ │ App name │ id │ mode │ PID │ status │ restarted │ uptime │ memory │ watching │ ├──────────┼────┼──────┼─────┼────────┼───────────┼────────┼─────────────┼──────────┤ │ app │ 0 │ fork │ 308 │ online │ 0 │ 0s │ 21.879 MB │ disabled │ └──────────┴────┴──────┴─────┴────────┴───────────┴────────┴─────────────┴──────────┘ Use `pm2 info <id|name>` to get more details about an app
檢視所有pm2專案
pm2 list
┌──────────┬────┬──────┬─────┬────────┬───────────┬────────┬─────────────┬──────────┐ │ App name │ id │ mode │ PID │ status │ restarted │ uptime │ memory │ watching │ ├──────────┼────┼──────┼─────┼────────┼───────────┼────────┼─────────────┼──────────┤ │ app │ 0 │ fork │ 984 │ online │ 1 │ 3s │ 64.141 MB │ disabled │ └──────────┴────┴──────┴─────┴────────┴───────────┴────────┴─────────────┴──────────┘ Use `pm2 info <id|name>` to get more details about an app
停止pm2某個專案
pm2 stop appname
檢視啟動程式的詳細資訊:
pm2 describe appname
在瀏覽器端顯示執行的專案資訊
pm2 web
常用命令
$ 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 (
$ 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 啟動 並 生成一個配置檔案