node.js&pm2搭建node生產環境

centos 6.5 x64為例,
node.js下載地址https://nodejs.org/en/download/stable/

下載截圖

建議采用穩定編譯過的版本,source code稍麻煩,編譯過的直接可用,安裝超級簡單,紅色的是centos X64可用地址。
https://nodejs.org/dist/v5.7.1/node-v5.7.1-linux-x64.tar.xz

1、下載安裝包

[root@localhost software]# wget http://nodejs.org/dist/v5.7.1/node-v5.7.1-linux-x64.tar.xz
--2016-03-06 14:17:04--  http://nodejs.org/dist/v5.7.1/node-v5.7.1-linux-x64.tar.xz
Resolving nodejs.org... 104.20.22.46, 104.20.23.46
Connecting to nodejs.org|104.20.22.46|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 8374600 (8.0M) [application/x-xz]
Saving to: “node-v5.7.1-linux-x64.tar.xz”

100%[=====================================================================================================================================================>] 8,374,600   41.5K/s   in 2m 22s  

2016-03-06 14:19:37 (57.7 KB/s) - “node-v5.7.1-linux-x64.tar.xz” saved [8374600/8374600]


原始https鏈接容易下不下來,看網絡情況,可以去掉s后下載快些。

2、解壓文件tar.xz

因為是tar.xz結尾的文件,要xz一下,再tar一下。

[root@localhost software]# xz -d node-v5.7.1-linux-x64.tar.xz 
[root@localhost software]# tar -xvf node-v5.7.1-linux-x64.tar 
ode-v5.7.1-linux-x64/
node-v5.7.1-linux-x64/bin/
node-v5.7.1-linux-x64/bin/npm
node-v5.7.1-linux-x64/bin/node
node-v5.7.1-linux-x64/share/
node-v5.7.1-linux-x64/share/man/
node-v5.7.1-linux-x64/share/man/man1/
node-v5.7.1-linux-x64/share/man/man1/node.1
node-v5.7.1-linux-x64/share/systemtap/
node-v5.7.1-linux-x64/share/systemtap/tapset/
node-v5.7.1-linux-x64/share/systemtap/tapset/node.stp
node-v5.7.1-linux-x64/share/doc/
node-v5.7.1-linux-x64/share/doc/node/
node-v5.7.1-linux-x64/share/doc/node/gdbinit
node-v5.7.1-linux-x64/LICENSE
node-v5.7.1-linux-x64/include/
node-v5.7.1-linux-x64/include/node/
node-v5.7.1-linux-x64/include/node/zconf.h
node-v5.7.1-linux-x64/include/node/uv-sunos.h

[root@localhost software]# cd node
[root@localhost node]# cd bin
[root@localhost bin]# ./node -v
v5.7.1

等待解壓完成,建議把解壓后的目錄改為node,或者其他有意義的名字,不然node升級太快在原有基礎上升級但是文件目錄顯示的版本名又不匹配。

3、配置node全局運行

export NODE_HOME=/file/software/node
export PATH=$NODE_HOME/bin:$PATH 
#找到下邊這一行添加進去,記得你自己的路徑搞對了。
export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL

重啟一下試試看,node,npm都可以全局啟用。

推薦上面的全局配置方式,當然配置軟鏈接也可以全局命令行,這個不推薦。


[root@localhost bin]# ln -s /file/software/node/bin/node /usr/local/bin/node
[root@localhost bin]# ln -s /file/software/node/bin/npm  /usr/local/bin/npm
[root@localhost bin]# cd /
[root@localhost /]# dir
bin  boot  cgroup  dev  etc  file  home  lib  lib64  lost+found  media  mnt  opt  proc  root  sbin  selinux  srv  sys  tmp  usr  var
[root@localhost /]# npm -v
3.6.0
[root@localhost /]# 

之前基本的環境已經搭建完成,但是光有這個還不足夠。

4、pm2保駕護航

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
開始下載安裝文件
  │ └── minimist@0.0.8 
  ├── moment@2.11.2 
  ├─┬ nssocket@0.6.0 
  │ └── lazy@1.0.11 
  ├── pidusage@1.0.1 
  ├─┬ pm2-axon@2.0.9 
  │ ├── amp@0.3.1 
  │ ├── amp-message@0.1.2 
  │ ├── configurable@0.0.1 
  │ └── escape-regexp@0.0.1 
  ├─┬ pm2-axon-rpc@0.3.6 
  │ ├─┬ commander@1.0.5 
  │ │ └── keypress@0.1.0 
  │ └── json-stringify-safe@5.0.1 
  ├─┬ pm2-deploy@0.2.1 
  │ ├── async@1.4.2 
  │ └── tv4@1.0.18 
  ├─┬ pm2-multimeter@0.1.2 
  │ └── charm@0.1.2 
  ├── pmx@0.6.1 
  ├── semver@5.1.0 
  ├── shelljs@0.6.0 
  ├─┬ source-map-support@0.4.0 
  │ └─┬ source-map@0.1.32 
  │   └── amdefine@1.0.0 
  └─┬ vizion@0.2.12 
    └── async@0.9.0 

runTopLevelLifecycles     ▌ ╢████████████████████████████████████████████████████████████████████████████████
npm WARN optional Skipping failed optional dependency /pm2/chokidar/fsevents:
runTopLevelLifecycles     ? ╢████████████████████████████████████████████████████████████████████████████████
npm WARN notsup Not compatible with your operating system or architecture: fsevents@1.0.8
runTopLevelLifecycles     ? ╢████████████████████████████████████████████████████████████████████████████████

成功。

5、測試個helloworld

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 <id|name>` to get more details about an app

[root@localhost www]#pm2 monit

是不是很神奇,接下來把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是注冊后官網給的。

系統提供監控的key

系統監控

6、補充問題,如何開放端口

[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 

全文完。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容