Node安装

!为了解决安装npm慢的问题,可指定国内镜像,如阿里的:

npm config set registry http://registry.npm.taobao.org

后续就很快了。

一、相关文档:

安装:
http://www.jianshu.com/p/43525232b03b

PM2实用入门指南:
http://www.cnblogs.com/chyingp/p/pm2-documentation.html

 

二、安装

1、安装:centos 6.5 x64为例,
node.js下载地址https://nodejs.org/en/download/stable/

 

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

 

3、待解压完成,建议把解压后的目录改为node,或者其他有意义的名字,不然node升级太快在原有基础上升级但是文件目录显示的版本名又不匹配

mv node-v5.7.1-linux-x64 node

 

4、配置node全局运行:vi /etc/profile
export NODE_HOME=/mnt/nodejs/node-v6.9.1-linux-x64
export NODE_PATH=$NODE_HOME/lib/node_modules
export PATH=$NODE_HOME/bin:$PATH
export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL

生效:source /etc/profile
重启一下试试看,node,npm都可以全局启用。

node -v

npm -v

 

5、安装express:
npm install -g express

然后还需要在vi /etc/profile添加:
export NODE_PATH=$NODE_HOME/lib/node_modules (前面已添加)
生效:source /etc/profile

 

6、npm install -g stomp-client     node 访问activeq

7、npm install -g stompjs         node 访问activeq

[root@localhost /]#
之前基本的环境已经搭建完成,但是光有这个还不足够。

 

三、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
全文完。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值