Centos Node安装记录

系统版本为Centos6.9

方式一:从EPEL库安装Node.js

可用node版本较低

1
2
3
4
5
6
7
8
# 安装
yum install epel-release
yum install nodejs
yum install npm
# 查看
node --version
npm --version

方式二(推荐):下载源代码,编译安装

可使用最新版本node

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 下载
cd /usr/local/src
wget https://nodejs.org/dist/v11.5.0/node-v11.5.0.tar.gz
# 解压
tar zxvf node-v11.5.0.tar.gz
cd node-v11.5.0
# 配置
./configure
# 编译
make install
# 查看
node --version
npm --version

备注:执行上述./config若提示需安装gcc或gcc版本过低,则使用devtoolset安装/升级gcc版本

1
2
3
4
5
6
7
8
9
10
11
# 安装源
yum -y install centos-release-scl-rh centos-release-scl
yum check-update
# 安装devtoolset
yum search devtoolset
yum -y install devtoolset-7-gcc.x86_64
yum install -y devtoolset-7-gcc-c++.x86_64
# 设为默认启用
source /opt/rh/devtoolset-7/enable

不推荐使用如下方式安装gcc, 因为如下方式安装的gcc版本一般较低

1
2
3
# 安装编辑器
yum install -y gcc
yum install -y gcc-c++

也不推荐通过如下方式,手动升级gcc,因为gcc下载编译前,需依次下载编译依赖库gmp、mpfr、mpc、isl,较繁琐,且实际安装会有各种问题。对系统层面有未知影响的升级还是尽量避免。

1
2
3
4
5
6
7
8
9
10
# 下载编译依赖库
......
# 下载gcc
cd /usr/local/src
wget http://ftp.gnu.org/gnu/gcc/gcc-8.2.0/gcc-8.2.0.tar.gz
tar zxvf gcc-8.2.0.tar.gz
# 编译
......

测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
vim hello.js;
# 内容如下
var http = require('http');
http.createServer(function(request, response) {
response.writeHead(200, {'Content-Type':'text/plain'});
response.end('Hello World\n');
}).listen(8081);
console.log('Node Server started');
# 运行
node hello.js
# 测试
curl http://127.0.0.1:8081