当前位置: 首页 > 后端技术 > Node.js

最全阿里云节点部署

时间:2023-04-03 22:51:20 Node.js

部署环境服务器环境:阿里云服务器CentOS7.464位,本地环境:Windows1064位连接工具:mobaxterm远程连接下载mobaxterm后,下一步就是完成安装.打开软件>session>sshhost填写ip地址,用户名填写root(阿里云默认用户名是root)。点击确定,输入密码(注意输入密码时软件没有反应),回车。如果登录不成功,您可以自行排查原因。部署node方法一(使用源码编译安装,先使用wget命令下载Node包)wgethttps://nodejs.org/dist/v6.9.5/node-v6.9.5-linux-x64.tar.xz解压文件tarxvfnode-v6.9.5-linux-x64.tar.xz创建软连接,主要是为了命令全局生效ln-s/root/node-v6.9.5-linux-x64/bin/node/usr/local/bin/nodeln-s/root/node-v6.9.5-linux-x64/bin/npm/usr/local/bin/npm查看是否成功node-vnpm-v方法二(使用NVM下载)下载NVM(这是一个node版本管理工具)yuminstallgitgitclonehttps://github.com/cnpm/nvm.git~/.nvm&&cd~/.nvm&&gitcheckout`gitdescribe--abbrev=0--tags`//here为了确保,不要忘记激活NVMecho".~/.nvm/nvm.sh">>/etc/profilesource/etc/profile列出了Node.js的所有版本nvmlist-remoteinstallandsetupnodenvminstallv6.9.5//安装nodenvminstallv7.4.0//安装nodenvmls//查看当前可用版本nvmusenodev7.4.0//切换版本nvmaliasdefaultv7.4.0//安装node后使用任意方法设置默认版本您可以通过创建一个新的项目文件example.js来测试项目。cd~touchexample.js用vim编辑器打开工程文件example.js。yuminstallvimvimexample.js输入“i”进入编辑模式,将以下项目文件内容粘贴到文件中。使用“Esc”键退出编辑模式,输入“:wq”,回车,保存文件内容退出(这里的0.0.0.0相当于windows中的127.0.0.1)consthttp=require('http');consthostname='0.0.0.0';constport=3000;constserver=http.createServer((req,res)=>{res.statusCode=200;res.setHeader('Content-Type','text/plain');res.end('HelloWorld\\n');});server.listen(port,hostname,()=>{console.log(`服务器运行于http://${hostname}:${port}/`);});运行项目node~/example.js//也可以使用以下代码后台运行node~/example.js&//后台运行netstat-tpln//查看端口运行状态在浏览器中打开http://IP:3000如果看到HelloWorld,恭喜部署成功。这里有几点需要注意。如果在后台运行,这个端口会被占用。如何关闭netstat-tpln//查看端口pidkill-9pid如果打开浏览器无奈无法访问,运行你的节点后查看是否出现Serverrunningat...,如果出现可能是你的服务器问题。这个时候可能是你的安全组配置有问题。可以根据官网修改入口。再次执行上述步骤。继续运行之前的所有任务,你可能还有一个疑问,如何让node进程保持存活,全局安装cnpm,可以大大提高下载速度npminstall-gcnpm--registry=https://registry。npm。taobao.orginstallpm2cnpminstall-gpm2pm2usepm2startexample.js//启动服务pm2list//查看启动的应用pm2showexample.js//查看详细信息pm2logs//查看当前信息pm2stopexample.js//停止示例pm2deleteexample.js//删除示例