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

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访问。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容