一、日志
1、pm2 的log怎么查看?(安裝pm2后默認日志的路徑為~/.pm2/),可以通過pm2 show (name)來查看某個進程下的日志地址
2、修改日志的輸出路徑,通過寫一個程序啟動的配置文件apps.yml,在里面可以設置日志的輸出地址,輸出文件,已經輸出的格式。例如:
apps:
- script : ./app.js
name : 'loginServer'
instances: 2
cwd: /var/service/ACServer/LoginServer
max_memory_restart: 1G // 內存超過1G之后自動重啟
exec_mode: cluster
#log_date_format: "YYY-MM-DD HH:mZ" // 配置日志的輸出格式
#error_file: "/var/data/loginLogs/pm2logs/loginErr.log" 配置out日志的輸出文件名
#out_file: "/var/data/loginLogs/pm2logs/loginOut.log"
3、清楚(pm2 flush)與重載日志(pm2 reloadLogs
)
二、開機自啟 nodejs 服務器
啟動想開機啟動的項目 pm2 start app.js
保存 pm2 save
開機設置 pm2 startup centos
chmod +x /etc/init.d/pm2-init.sh
chkconfig –add pm2-init.sh
三、pm2-logrotate進行日志分割
使用pm2-logrotate進行日志管理使得我們的node服務的log以及pm2的log能夠得到控制,防止log過多導致把磁盤刷爆。pm2-logrotate配置看官網,需要注意的是設置retain參數,來限制日志文件數
pm2 set pm2-logrotate:retain 10
四、pm2 monit命令監控你的內存和CPU
五、異常問題
今天在跑程序的時候pm2 突然報如下錯誤:
C:\server\MobileServer>pm2 start apps.yml [PM2][WARN] Applications MobileAPI, MobileMapReduce not running, starting... C:\Users\Administrator\AppData\Roaming\npm\node_modules\pm2\lib\API.js:1031 intOut(conf.PREFIX_MSG + 'App [%s] launched (%d instances)', data[0].pm2_env.n ^ TypeError: Cannot read property 'pm2_env' of undefined at C:\Users\Administrator\AppData\Roaming\npm\node_modules\pm2\lib\API.js:1031:86 at C:\Users\Administrator\AppData\Roaming\npm\node_modules\pm2\node_modules\pm2-axon-rpc\lib\client.js:45:10 at Parser.<anonymous> (C:\Users\Administrator\AppData\Roaming\npm\node_modules\pm2\node_modules\pm2-axon\lib\sockets\req.js:67:8) at Parser.emit (events.js:107:17) at Parser._write (C:\Users\Administrator\AppData\Roaming\npm\node_modules\pm2\node_modules\pm2-axon\node_modules\amp\lib\stream.js:91:16) at doWrite (_stream_writable.js:301:12) at writeOrBuffer (_stream_writable.js:288:5) at Parser.Writable.write (_stream_writable.js:217:11) at Socket.ondata (_stream_readable.js:540:20) at Socket.emit (events.js:107:17)
按照報錯信息應該是:pm2 環境設置問題。
通過pm2 在github上的issue找到了解決方案,地址如下:
https://github.com/Unitech/pm2/issues/2154
只需要運行下:pm2 update
然后重啟服務就可以了。
六、log4js中關於pm2的設置
在pm2zhongoing使用log4js需要對log4js的配置文件進行設置,這里主要參見官方文檔:https://log4js-node.github.io/log4js-node/api.html
具體demo配置如:
const logConfig = appenders: {
console: { type: 'console' }, file: { type: 'file', filename: './logs/application.log', maxLogSize: 104800, backups: 100 }, date_file: { type: 'dateFile', filename: "./logs/date", alwaysIncludePattern: true, pattern: "-yyyy-MM-dd.log" } }, categories: { default: {appenders: ['console'], level: 'info'}, errorLog: {appenders: ['console', 'file', 'date_file'], level: 'error'} },
// 關於pm2的設置 pm2: true,
disableClustering: true }; module.exports = logConfig;
前不久在測試服上開啟一個進程的時候沒加disableClustering參數,后來開啟多進程之后發現只有第一個進程能看到正常的log4js的log,而其他進程則沒有,回來閱讀api,設置disableClustering:true。這樣
每個進程的log就出現了。
七、是否需要nginx
nginx可以做的事情主要有兩個:
1、反向代理,實現簡單的負載均衡: 如果有多台服務器或者一台服務器多個端口,可以考慮用nginx。
2、靜態資源緩存:把一些靜態資源(如靜態頁面,js等資源文件)放到nginx里,可以極大的提高服務的性能。
參考:https://github.com/jawil/blog/issues/7/