环境

  1. 配置node.js的环境,选择下面的环境直接重新安装。

node 环境部署遇到的坑 - 图1

安全组

什么是安全组:

安全组是一种虚拟防火墙,具备有状态的数据包过滤功能,用于设置云服务器负载均衡、数据库等实例的网络访问控制,是重要的网络安全隔离手段。

您可以通过配置安全组规则,允许或禁止安全组内的实例对公网或私网的访问:

  • 安全组是一个逻辑上的分组,您可以将同一地域内具有相同网络安全隔离需求的基础网络云服务器或弹性网卡实例加到同一个安全组内。
  • 您可以通过安全组策略对实例的出入流量进行安全过滤,实例可以是基础网络云服务器或弹性网卡实例 。
  • 您可以随时修改安全组的规则。新规则立即生效。

安全组的配置

node 环境部署遇到的坑 - 图2

安全组不是很会配置,所以全部放开了。如果有多个实例,将安全组和实例关联

遇到的问题
  • 使用ping 可以ping通服务器
    node 环境部署遇到的坑 - 图3
  • 使用telnet 端口的是不可以,具体什么原因不知道,但是xshell和xfp可以上传文件

node 环境部署遇到的坑 - 图4

  • 可能原因:
    • 公司局域网的防火墙
    • 服务器防火墙没有关闭
  • 关闭服务器防火墙
    • 查看系统是centos7还是centos6: 输入 cat /etc/redhat-release,即可显示系统版本。node 环境部署遇到的坑 - 图5
    • Centos 6.x版本 iptables
      • 查看防火墙状态:

[root@centos6 ~]# service iptables status
iptables: Firewall is not running. 说明防火墙没有开启。

  1. - 开启防火墙:

[root@centos6 ~]# service iptables start

  1. - 关闭防火墙:

[root@centos6 ~]# service iptables stop

  • Centos 7.x版本 firewall
    • 停止firewall
      systemctl stop firewalld.service
    • 禁止firewall开机启动
      systemctl disable firewalld.service
    • 查看默认防火墙状态(关闭后显示notrunning,开启后显示running)
      firewall-cmd —state

nginx

nginx的安装

  1. 使用yurm安装nginx
    1. $ sudo yum -y install nginx # 安装 nginx
    2. $ sudo yum remove nginx # 卸载 nginx

    nginx的配置

  • nginx -t 查看nginx的地址
    node 环境部署遇到的坑 - 图6
  1. # 全局块
  2. ...
  3. # events
  4. events {
  5. ...
  6. }
  7. # http
  8. http
  9. {
  10. # http全局块
  11. ...
  12. # 虚拟主机server
  13. server
  14. {
  15. # server全局块
  16. ...
  17. # location
  18. location [PATTERN]
  19. {
  20. ...
  21. }
  22. location [PATTERN]
  23. {
  24. ...
  25. }
  26. }
  27. server
  28. {
  29. ...
  30. }
  31. # http全局块
  32. ...
  33. }
  • 配置详解
  1. ########### 每个指令必须有分号结束。#################
  2. #user administrator administrators; #配置用户或者组,默认为nobody nobody
  3. #worker_processes 2; #允许生成的进程数,默认为1
  4. #pid /nginx/pid/nginx.pid; #指定nginx进程运行文件存放地址
  5. error_log log/error.log debug; #制定日志路径,级别。这个设置可以放入全局块,http块,server块,级别以此为:debug|info|notice|warn|error|crit|alert|emerg
  6. events {
  7. accept_mutex on; #设置网路连接序列化,防止惊群现象发生,默认为on
  8. multi_accept on; #设置一个进程是否同时接受多个网络连接,默认为off
  9. #use epoll; #事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
  10. worker_connections 1024; #最大连接数,默认为512
  11. }
  12. http {
  13. include mime.types; #文件扩展名与文件类型映射表
  14. default_type application/octet-stream; #默认文件类型,默认为text/plain
  15. #access_log off; #取消服务日志
  16. log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #自定义格式
  17. access_log log/access.log myFormat; #combined为日志格式的默认值
  18. sendfile on; #允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。
  19. sendfile_max_chunk 100k; #每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。
  20. keepalive_timeout 65; #连接超时时间,默认为75s,可以在httpserverlocation块。
  21. # 定义常量
  22. upstream mysvr {
  23. server 127.0.0.1:7878;
  24. server 192.168.10.121:3333 backup; #热备
  25. }
  26. error_page 404 https://www.baidu.com; #错误页
  27. #定义某个负载均衡服务器
  28. #root 是指定项目的根目录,适用与serverlocation。可以指定多个,如果locaiton没有指定,会往其外层的serverhttp中寻找继承。
  29. server {
  30. keepalive_requests 120; #单连接请求上限次数。
  31. listen 4545; #监听端口,服务器默认接口是8080不会可以不需要修改
  32. server_name 127.0.0.1; #监听地址
  33. root /data/gezhinode; # 指定项目的根目录,配置你项目的更目录(默认是这个)
  34. location ~*^.+$ { #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。
  35. #root path; #根目录(自己可以设置)
  36. #index vv.txt; #设置默认页
  37. proxy_pass http://mysvr; #请求转向mysvr 定义的服务器列表
  38. deny 127.0.0.1; #拒绝的ip
  39. allow 172.18.5.54; #允许的ip
  40. }
  41. }
  42. }

nginx内置的全局变量

  1. $args :这个变量等于请求行中的参数,同$query_string
  2. $content_length 请求头中的Content-length字段。
  3. $content_type 请求头中的Content-Type字段。
  4. $document_root 当前请求在root指令中指定的值。
  5. $host 请求主机头字段,否则为服务器名称。
  6. $http_user_agent 客户端agent信息
  7. $http_cookie 客户端cookie信息
  8. $limit_rate 这个变量可以限制连接速率。
  9. $request_method 客户端请求的动作,通常为GETPOST
  10. $remote_addr 客户端的IP地址。
  11. $remote_port 客户端的端口。
  12. $remote_user 已经经过Auth Basic Module验证的用户名。
  13. $request_filename 当前请求的文件路径,由rootalias指令与URI请求生成。
  14. $scheme HTTP方法(如httphttps)。
  15. $server_protocol 请求使用的协议,通常是HTTP/1.0HTTP/1.1
  16. $server_addr 服务器地址,在完成一次系统调用后可以确定这个值。
  17. $server_name 服务器名称。
  18. $server_port 请求到达服务器的端口号。
  19. $request_uri 包含请求参数的原始URI,不包含主机名,如:”/foo/bar.php?arg=baz”。
  20. $uri 不带请求参数的当前URI$uri不包含主机名,如”/foo/bar.html”。
  21. $document_uri $uri相同。

nginx中路径配置的规则

在nginx中配置proxy_pass代理转发时,如果在proxy_pass后面的ur加 /,表示绝对路径;没有 / 表示相对路径,表示吧匹配的路径部分给代理走了。
假如下面四种情况分别用 http://192.168.1.1/proxy/test.html访问。

  1. {
  2. // 第一种
  3. location /proxy/
  4. {
  5. pxoxy_pass 127.0.0.1/;
  6. // 代理到url:127.0.0.1/test.html
  7. };
  8. // 第二种(相对于第一种,少了url 后面少了 / )
  9. location /proxy/
  10. {
  11. proxy_pass 127.0.0.1;
  12. // 代理到url:127.0.0.1/proxy/test.html
  13. };
  14. // 第三种
  15. location /proxy/
  16. {
  17. proxy_pass 127.0.0.1/aaa/;
  18. // 代理到url:127.0.0.1/aaa/test.html
  19. };
  20. // 第四种
  21. location /proxy/
  22. {
  23. proxy_pass 127.0.0.1/aaa
  24. // 代理到url:127.0.0.1/proxy/aaatest.html
  25. };
  26. }

xfp:用来上传文件的,好像可以git直接上传不会。

Xshell: 用来控制服务器的

升级node

  • 因为使用了egg框架来做后台,但是安装的node的版本太低。
  • 升级node后发现有两个node
    • 修改vim ~/.bash_profile 文件加
    • export N_PREFIX=/data/home/server/nodejs #node实际安装位置
    • export PATH=$N_PREFIX/bin:$PATH // 退出保存就可以

引用连接 : https://blog.csdn.net/qq_16339527/article/details/73008708

使用PM2管理

pm2的作用

  1. 监听文件变化,自动重启程序
  2. 支持性能监控
  3. 负载均衡
  4. 程序崩溃自动重启
  5. 服务器重新启动时自动重新启动
  6. 自动化部署项目

安装

npm install -g pm2

配置

在工程目录输入:$ pm2 ecosystem

  1. {
  2. "apps": {
  3. "name": "pm2-test-server",
  4. "script": "app.js",
  5. "watch": true, // 监听文件改变,自动重启服务
  6. "ignore_watch": [ // 忽略监听目录
  7. "node_modules",
  8. "logs"
  9. ],
  10. "instances": 4, // 多进程
  11. "error_file": "logs/error.log", // 错误日志
  12. "out_file": "logs/out.log", // 日志
  13. "log_date_format": "YYYY-MM-DD HH-mm-ss" // 日志时间格式
  14. }
  15. }

PM2常用命令

  • pm2 list 查看
    node 环境部署遇到的坑 - 图7
  • pm2 start … 开始
    node 环境部署遇到的坑 - 图8
  • pm2 restart / 重启
  • pm2 stop / 停止
    node 环境部署遇到的坑 - 图9
  • pm2 delete / 删除
  • pm2 info / 基本信息
  • pm2 log / 日志
  • pm2 monit / 查看cup和内存

显示面板解析

node 环境部署遇到的坑 - 图10

字段 解释
app name / id 进程的标识,可以对他们进行别的操作,比如stop,delete
mode 进程模式,cluster或fork。cluster有多个进程,而fork只有一个。
status 进程是否在线
restart 重启次数
uptime 运行时间
cpu cpu占用率
mem 内存占用大小

egg项目需要配置

ecosystem.config.js 配置

作用: 可以同时管理多个node服务器,不用一个个手动启动node

  1. module.exports = {
  2. apps : [{
  3. name:'gezhi', // 服务器名字
  4. script: '/data/gezhi/node/app.js', // 启动文件的地址,egg app.js express 是 bin/www
  5. watch: '.' // 监听文件的变化
  6. },
  7. // 第二个项目
  8. {
  9. name:'gezhi2',
  10. script:'/data/gezhinode/app.js',
  11. watch:'.'
  12. }
  13. ],
  14. // 没有用到不知道
  15. deploy : {
  16. production : {
  17. user : 'SSH_USERNAME',
  18. host : 'SSH_HOSTMACHINE',
  19. ref : 'origin/master',
  20. repo : 'GIT_REPOSITORY',
  21. path : 'DESTINATION_PATH',
  22. 'pre-deploy-local': '',
  23. 'post-deploy' : 'npm install && pm2 reload ecosystem.config.js --env production',
  24. 'pre-setup': ''
  25. }
  26. }
  27. };

查看启动的端口

使用 netstat -nelp 来查看哪些端口被使用:
image.png我的node 服务器使用了7100端口

curl的使用

  1. 用法: 用来URL规则在命令行下工作的文件传输工具,是一个很强大的HTTP命令行工具。可以用来检测服务是不是启动,端口是不是可以访问。
  2. 语法# curl [option ] url
  3. 基本用法 # curl 127.0.0.1可以用来检测本机的8080端口是不是开启,用来检测nginx是不是有效

image.png

访问: ip+端口 直接访问

启动成功后看到端口,ip是服务器公网ip