VMware虚拟机Linux部署nodejs环境并测试运行

业余选手第一次Linux下配置NodeJS

1.nodejs官网下载对应node版本DOUNLOAD
在这里插入图片描述
2.利用Xshell上传到linux的node存放目录
先在Xshell中进入要上传的目录,用lrzsz命令rz上传到指定位置

3.将上传的node安装包解压
用tar -xvf 文件名,命令解压安装包,这里解压完成最好将解压出来的文件夹重新命名一下,mv old_dir_name new_dir_name

4.配置nodejs全局变量
ln -s /home/node/bin/node /sbin/node
ln -s /home/node/bin/npm /sbin/npm
在这里插入图片描述
5.配置环境变量
在根目录下/etc/profile里面添加

export node_home = /root/node
export PATH = $node_home/bin:$PATH

在这里插入图片描述
6.测试
先运行node -v查看node是否配置成功
在这里插入图片描述
在linux的主目录home创建www工作目录,创建node_test/hello.js
创建一个简单的nodejs服务器,然后通过主机浏览器进行访问
hello.js代码:

var http = require('http');
http.createServer(function(req, res){
  res.writeHead(200,{'Content-Type': 'text/plain'});
  res.end('hello world');
}).listen(3000);
console.log("http server is listening at port 3000.");

在这里插入图片描述
接下来用node运行hello.js启动node服务器
node hello.js
在这里插入图片描述
利用主机的浏览器去访问linux IP+PORT

注意:

到这里有的人用主机和虚拟机相互ping都能ping通,但是主机就是访问不了,可以尝试这关闭掉虚拟机linux的防火墙
CentOS7关闭防火墙代码:

systemctl stop firewalld.service     #停止firewall
systemctl disable firewalld.service  #禁止firewall开机启动

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_38652871/article/details/87378671