Linux centos-7系统,nodejs为node-v6.10.0-linux-x64
1.到nodejs官网(http://nodejs.cn/download/)下载压缩包(选择合适自己系统的版本),放到Linux的安装目录下,
2.用tar -xvf node-v6.10.0-linux-x64解压下载的nodejs压缩包
建议用mv node-v6.10.0-linux-x64 node移动重命名文件
3.设置nodejs全局变量,
ln -s /root/node/bin/node /sbin/node
ln -s /root/node/bin/npm /sbin/node
4.设置环境变量,在根目录下/etc/profile里面添加
export node_home = /root/node
export PATH = $node_home/bin:$PATH
5.在工作目录下创建hello.js文件,在里面添加
var http = require('http');
http.createServer(function(req, res){
res.writeHead(200,{'Content-Type': 'text/plain'});
res.end('hello world');
}).listen(3030);
console.log("http server is listening at port 3030.");
7.用node运行hello.js文件,
node hello.js
然后在浏览器输入IP地址加端口号输出内容了