centOS下安装nodejs小结以及守护进程使用与结束

/*  

*  备注:错误步骤:1~4,直接进行步骤5即可

*/

1. 首先需要下载node安装包,可以在https://nodejs.org/dist/中查找最新的安装包地址,使用

wget https://nodejs.org/dist/v4.5.0/node-v4.5.0.tar.gz

提示

ERROR: certificate common name `*.nodejs.org' doesn't match requested host name `nodejs.org'.
To connect to nodejs.org insecurely,use `--no-check-certificate'.

是因为该网址所使用的协议是https协议,因此加上了--no-check-certificate参数

wget --no-check-certificate https://nodejs.org/dist/v4.5.0/node-v4.5.0.tar.gz

成功下载

2. 嗯,上述方法需要安装 gcc 与 gcc-c++,因此使用

yum -y groupinstall "Development Tools"

对上面两者进行了安装

3. 将安装包进行解压,使用

tar -xf node-v4.5.0.tar.gz

然后发现当前文件夹下多出了一个解压后的文件夹

4. 接下来进入该目录,使用make对其进行编译,此时出错,显示的错误内容为:

In file included from ../deps/v8/src/v8.h:29,from ../deps/v8/src/accessors.cc:5:
../deps/v8/include/v8.h: In constructor 'v8::MaybeLocal<T>::MaybeLocal()':
../deps/v8/include/v8.h:353: error: 'nullptr' was not declared in this scope
../deps/v8/include/v8.h: In member function 'bool v8::MaybeLocal<T>::IsEmpty() const':
../deps/v8/include/v8.h:360: error: 'nullptr' was not declared in this scope
../deps/v8/include/v8.h: In member function 'bool v8::MaybeLocal<T>::ToLocal(v8::Local<S>*) const':
../deps/v8/include/v8.h:364: error: 'nullptr' was not declared in this scope
../deps/v8/include/v8.h: In member function 'bool v8::WeakCallbackInfo<T>::IsFirstPass() const':
../deps/v8/include/v8.h:430: error: 'nullptr' was not declared in this scope
../deps/v8/include/v8.h: At global scope:
../deps/v8/include/v8.h:469: error: expected unqualified-id before 'using'
../deps/v8/include/v8.h: In constructor 'v8::Global<T>::Global()':
../deps/v8/include/v8.h:790: error: 'nullptr' was not declared in this sc

经检查,应该是gcc与gcc-c++版本问题,所以对其进行更新也许可以解决。

5. 之后并没有继续上述方法。而是查到了使用

sudo yum install -y nodejs

可以成功安装nodejs,使用版本测试查看版本

node -v

显示为

v0.10.48

表示成功安装

在node安装成功之后,写一个常用的http模块程序:server.js

1 var http = require('http');
2 var server = http.createServer(function(req,res){
3     res.writeHeader(200,{
4         'Content-Type': 'text/plain; charset=utf-8'
5     });
6     res.end('Hello World');
7 });
8 server.listen(8888);    // 监听8888端口,其他如apache服务器默认为80端口

在使用

node server.js

之后,发现可以访问云服务器ip地址的8888端口,界面输出Hello World字样。

但是有个问题,如果将所使用的shell关闭了以后,该页面就访问不到了,因此查找解决方法,发现需要“守护进程”

在查找过程中,发现阮一峰大神的这篇博客中有提到这一点:可以使用

node sever.js &

在后面加一个&使该进程变为后台任务,是最简单的方法,但是当server.js修改的时候需要重新启动server.js,并不能自动监测该文件的变化,因此可以看博客中的几个方法。

其中,使用linux中的 nohup方法加指令来使命令后台运行,较为简单。

另外,如果需要结束后台任务,需要kill命令

// 首选需要查找运行在8888端口上的进程id
sudo lsof -i:8888

// 然后使用这个命令杀死进程
sudo kill -9 24841

参考:http://www.cnblogs.com/likaopu/p/6553326.html

相关文章

linux下开机自启: 在/etc/init.d目录下新建文件elasticsear...
1、因为在centos7中/etc/rc.d/rc.local的权限被降低了,所以...
最简单的查看方法可以使用ls -ll、ls-lh命令进行查看,当使用...
ASP.NET Core应用程序发布linux在shell中运行是正常的。可一...
设置时区(CentOS 7) 先执行命令timedatectl status|grep &...
vim&#160;/etc/sysconfig/network-scripts/ifcfg-eth0 B...