美文网首页全栈最后一公里——nodejs项目线上服务器部署发布
4、搭建node生产环境——搭建服务器的nodejs环境

4、搭建node生产环境——搭建服务器的nodejs环境

作者: 伯纳乌的追风少年 | 来源:发表于2017-08-29 18:03 被阅读0次

1、安装更新

sudo apt-get update

2、安装相关的apt包文件

sudo apt-get install vim openssl build-essential libssl-dev wget curl git

3、安装nvm
先进入nvm的github主页:https://github.com/creationix/nvm找到脚本

wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | bash

安装成功后应该重新开一个终端重新登录,才能使用已经安装的nvm

4、使用nvm安装nodejs

nvm install v6.9.5

5、设置系统默认node版本

nvm alias default v6.9.5

6、指定使用taobao的镜像来下载npm源

npm --registry=https://registry.npm.taobao.org install -g npm

7、增加系统文件监控数目

echo fs.inotify.max_user_watches=524288|sudo tee -a /etc/sysctl.conf && sudo sysctl -p

8、安装cnpm

npm --registry=https://registry.npm.taobao.org install -g cnpm

9、安装相关软件

npm i pm2 webpack gulp grunt-cli -g

10、编写node启动程序:

vim app.js

const http= require('http')
http.createServer(function(req,res){
  res.writeHead(200,{"Content-Type":"text/plain"})
  res.end("created by xiaoke")
}).listen(8081)
console.log('server running on http://47.52.28.218:8081')

11、修改iptables的配置文件并重载:

sudo vim /etc/iptables.up.rules

#加一行
-A INPUT -p tcp --dport 8081 -j ACCEPT

重载iptables

sudo iptables-restore < /etc/iptables.up.rules

若此时提示:“-bash: /etc/iptables.up.rules: Permission denied”,则可能是权限问题,尝试“su -”切换到root用户再执行

如果此时还不行的话,需要添加一个阿里云的安全规则:

12、浏览器中此时可以通过输入http://47.52.28.218:8081访问。

相关文章

网友评论

    本文标题:4、搭建node生产环境——搭建服务器的nodejs环境

    本文链接:https://www.haomeiwen.com/subject/dgwodxtx.html