[关闭]
@yanglt7 2019-01-02T06:05:37.000000Z 字数 3451 阅读 670

部署 nodejs 服务至 vCenter 集群并实现端口转发及隐藏端口

网管


虚拟机安装

由于集群需内网登录,为了方便起见,先在本机安装虚拟机测试功能,之后再部署至集群。

参考文章:
在 VMware Workstation Pro 上安装虚拟机 CentOS 7.X
CentOS 7.x 系统安装后的基本配置及调优

安装 nodejs

安装 nodejs 至 /Software/

  1. cd /Software/
  2. wget https://nodejs.org/dist/v9.9.0/node-v9.9.0-linux-x64.tar.xz
  3. tar -xvf node-v9.9.0-linux-x64.tar.xz
  4. mv node-v9.9.0-linux-x64 nodejs
  5. ln -s /Software/nodejs/bin/node /usr/local/bin/
  6. ln -s /Software/nodejs/bin/npm /usr/local/bin/

将项目文件放至 /Data/HopeLibrarySystem-master/ 目录下
此处输入图片的描述

若该项目文件之前在 Windows 下运行过 npm install 命令,需将 node_modules 文件夹删除。
进入项目文件所在目录,cd /Data/HopeLibrarySystem-master/
运行 npm install,安装依赖包。

安装 mysql

顺便记录上传本机文件至虚拟机的方法:
法一:
filezalla 客户端,选择 SFTP 协议
ip 用户名 密码 端口
此处输入图片的描述
root 用户登录及端口设置可在 /etc/ssh/sshd_config 文件中修改,具体参见参考文章 CentOS 7.x 系统安装后的基本配置及调优

法二:
sftp
打开命令行,直接输入 sftp -o port=2333 ylt@192.168.2.156

通过 filezalla 将 mysql 安装包放至 /Software/ 目录下

  1. groupadd mysql
  2. useradd -s /sbin/nologin -g mysql -M mysql
  3. tar -xvf mysql-5.5.62-linux-glibc2.12-x86_64.tar.gz
  4. mv mysql-5.5.62-linux-glibc2.12-x86_64 mysql
  5. ll support-files/*.cnf
  6. /bin/cp support-files/my-small.cnf /etc/my.cnf
  7. mkdir /Data/mysql_data
  8. /Software/mysql/scripts/mysql_install_db --basedir=/Software/mysql --datadir=/Software/mysql/data --user=mysql
  9. cp support-files/mysql.server /etc/init.d/mysqld
  10. chmod +x /etc/init.d/mysqld
  11. sed -i 's#/usr/local/mysql#/Software/mysql#g' /Software/mysql/bin/mysqld_safe /etc/init.d/mysqld
  12. /etc/init.d/mysqld start
  13. netstat -lntup|grep mysql
  14. chkconfig --add mysqld
  15. chkconfig mysqld on
  16. echo 'export PATH=/Software/mysql/bin:$PATH' >>/etc/profile
  17. source /etc/profile
  18. echo $PATH
  19. mysqladmin -u root password '***'

具体查看参考文章 MySQL 的安装与配置

导入数据库文件

source /Data/HopeLibrarySystem-master/hopeWechat.sql

数据库文件可能乱码解决:

  1. vim /etc/locale.conf,改中文编码
  2. LANG="zh_CN.UTF-8"
  3. vim /etc/my.cnf ,设置mysql服务器端的编码
  4. character_set_server= utf8

将本机 vmx 上传到集群

先将 vmx 文件转换成 ovf:

  1. cd C:\Program Files (x86)\VMware\VMware Workstation\OVFTool
  2. ovftool D:\HopeLibrarySystem\HopeLibrarySystem.vmx D:\HopeLibrarySystem.ovf

VCenter 部署 ovf 模板即可。

端口转发

先设置网络,将网关设置成端口转发机的内网IP

  1. cd /etc/sysconfig/network-scripts/
  2. cp ifcfg-lo ifcfg-lo.bak
  3. mv ifcfg-lo ifcfg-ens33
  4. vim ifcfg-ens33
  5. DEVICE=ens33
  6. IPADDR=10.99.1.6
  7. GATEWAY=端口转发机的内网IP
  8. BOOTPROTO=static
  9. NETMASK=255.255.255.0
  10. TYPE=Ethernet
  11. NM_CONTROLLED=yes
  12. IPV6INIT=no
  13. #NETWORK=127.0.0.0
  14. # If you're having problems with gated making 127.0.0.0/8 a martian,
  15. # you can change this to something else (255.255.255.255, for example)
  16. #BROADCAST=127.255.255.255
  17. ONBOOT=yes
  18. #NAME=loopback
  19. PROXY_METHOD=none
  20. BROWSER_ONLY=no
  21. PREFIX=24
  22. DEFROUTE=yes
  23. IPV4_FAILURE_FATAL=no
  24. NAME="System ens33"
  25. UUID=c96bc909-188e-ec64-3a96-6a90982b08ad
  26. ZONE=trusted

在端口转发机添加防火墙规则:

  1. vim /etc/sysconfig/iptables
  2. iptables -t nat -A PREROUTING -d 公网IP/32 -p tcp -m tcp --dport 3000 -j DNAT --to-destination 10.99.1.6:3000
  3. service iptables save
  4. service iptables reload

IP+端口隐藏

新建一台 Linux 主机,配置 Nginx,实现基于 location 的转发。

Nginx 安装参考文章 Nginx 安装与虚拟主机配置

修改端口转发机的防火墙规则:

  1. iptables -t nat -A PREROUTING -d 外网IP/32 -p tcp -m tcp --dport 80 -j DNAT --to-destination Nginx主机IP:80

修改 Nginx 配置文件:

  1. #user nobody;
  2. #2018.12.22
  3. worker_processes 1;
  4. events {
  5. worker_connections 1024;
  6. }
  7. http {
  8. include mime.types;
  9. default_type application/octet-stream;
  10. sendfile on;
  11. keepalive_timeout 65;
  12. log_format main '$remote_addr - $remote_user [$time_local] '
  13. : '"$request" $status $body_bytes_sent '
  14. : '"$http_referer" "$http_user_agent" "$http_x_forwarded_for"';
  15. server {
  16. listen 80;
  17. access_log logs/access.log;
  18. server_name 202.116.83.77;
  19. location / {
  20. proxy_pass http://10.99.1.13;
  21. }
  22. location /hopelibrary/ {
  23. rewrite ^/hopelibrary/(.*)$ /$1 break;
  24. proxy_pass http://10.99.1.6:3000/;
  25. }
  26. }
  27. }

使用 pm2 使 nodejs 后台永久运行

  1. npm install -g pm2
  2. pm2 start bin/www --name hopelibrary
  3. pm2 save
  4. pm2 startup

参考文章 PM2来部署nodejs服务器永久开启

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注