3、node之pm2管理node生產環境

pm2保駕護航

pm2 是一個帶有負載均衡功能的 node 應用進程管理工具

forever已經out了,嚴重推薦pm2方式運行nodejs,這是最好的,沒有之一。
內建負載均衡(使用 Node cluster 集羣模塊)
後臺運行0 秒停機重載,我理解大概意思是維護升級的時候不需要停機.
具有 Ubuntu 和 CentOS 的啓動腳本
停止不穩定的進程(避免無限循環)
控制檯檢測
提供 HTTP API
遠程控制和實時的接口 API ( Nodejs 模塊,允許和 PM2 進程管理器交互 )
pm2官網http://pm2.keymetrics.io/

[root@localhost /]# npm install -g pm2
開始下載安裝文件 │ └── [email protected] ├── [email protected] ├─┬ [email protected] │ └── [email protected] ├── [email protected] ├─┬ [email protected] │ ├── [email protected] │ ├── [email protected] │ ├── [email protected] │ └── [email protected] ├─┬ [email protected] │ ├─┬ [email protected] │ │ └── [email protected] │ └── [email protected] ├─┬ [email protected] │ ├── [email protected] │ └── [email protected] ├─┬ [email protected] │ └── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├─┬ [email protected] │ └─┬ [email protected] │ └── [email protected] └─┬ [email protected] └── [email protected] runTopLevelLifecycles ▌ ╢████████████████████████████████████████████████████████████████████████████████ npm WARN optional Skipping failed optional dependency /pm2/chokidar/fsevents: runTopLevelLifecycles ? ╢████████████████████████████████████████████████████████████████████████████████ npm WARN notsup Not compatible with your operating system or architecture: [email protected] runTopLevelLifecycles ? ╢████████████████████████████████████████████████████████████████████████████████

成功。

測試個helloworldQQ245197944

可以在網頁目錄裏編寫服務端代碼helloworld.js

var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, { 'Content-Type': 'text/plain' }); res.end('Hello World\n'); }).listen(1337, "127.0.0.1"); console.log('Server running at http://127.0.0.1:1337/');

pm2啓動試試看

[root@localhost www]# pm2 start helloworld.js --name 'helloworld'
[PM2] Spawning PM2 daemon [PM2] PM2 Successfully daemonized [PM2] Starting helloworld.js in fork_mode (1 instance) [PM2] Done. ┌────────────┬────┬──────┬──────┬────────┬─────────┬────────┬─────────────┬──────────┐ │ App name │ id │ mode │ pid │ status │ restart │ uptime │ memory │ watching │ ├────────────┼────┼──────┼──────┼────────┼─────────┼────────┼─────────────┼──────────┤ │ helloworld │ 0 │ fork │ 2251 │ online │ 0 │ 0s │ 14.715 MB │ disabled │ └────────────┴────┴──────┴──────┴────────┴─────────┴────────┴─────────────┴──────────┘ Use `pm2 show ` to get more details about an app [root@localhost www]#pm2 monit

1240


是不是很神奇,接下來把pm2加入系統啓動中。

[root@localhost www]# pm2 startup centos
[PM2] Generating system init script in /etc/init.d/pm2-init.sh [PM2] Making script booting at startup... [PM2] /var/lock/subsys/pm2-init.sh lockfile has been added [PM2] -centos- Using the command: su -c "chmod +x /etc/init.d/pm2-init.sh; chkconfig --add pm2-init.sh" [PM2] Done. [root@localhost www]# pm2 save
[PM2] Dumping processes 要保存一下!

還有更厲害。
先去pm2官網上註冊個賬號,收費的更好些,我們看下free版本。

[root@localhost ~]# pm2 link key1 ke2 [62server]
[Keymetrics.io] Using (Public key: yklukcus7ugg7u6) (Private key: fhgynshuxtahahd) [Keymetrics.io] [Agent created] Agent ACTIVE - Web Access: https://app.keymetrics.io/

key1和key2是註冊後官網給的。QQ245197944

1240

系統提供監控的key

1240

系統監控

補充問題,如何開放端口

[root@localhost ~]# /sbin/iptables -I INPUT -p tcp --dport 1337 -j ACCEPT [root@localhost ~]# /etc/rc.d/init.d/iptables save iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ] [root@localhost ~]# /etc/init.d/iptables status Table: filter Chain INPUT (policy ACCEPT) num target prot opt source destination 1 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:1337
此文爲轉載。



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