RAP2开发环境部署

所需环境

1、mysql

mysql linux安装步骤:https://blog.csdn.net/qq_31854907/article/details/85070544
2、redis

redis linux安装步骤:https://blog.csdn.net/qq_31854907/article/details/85107885
3、nginx安装

ngix linux安装步骤:https://blog.csdn.net/qq_31854907/article/details/85099271
4、npm/nodejs环境

node linux安装步骤:https://blog.csdn.net/micarlxm/article/details/81091284

下载的版本是:wget https://nodejs.org/dist/latest-v8.x/node-v8.15.1-linux-x64.tar.gz
5、rap2-delos端安装

使用git下载安装源码:git clone https://github.com/thx/rap2-delos.git

RAP2开发环境部署

修改配置文件

目录:rap2-delos/src/config
文件:config.dev.ts;config.local.ts;config.prod.ts 请把三个文件都修改  请看下面注释
修改:config.dev.ts文件中db对象中username,password参数与本地或者开发环境的数据库信息匹配

上面三个 配置文件 config.*.tx 都要修改 主要是针对 mysql 做修改

import { IConfigOptions } from "../types";

let config: IConfigOptions = {
  version: '2.3',
  serve: {
    port: 8080,
  },
  keys: ['some secret hurr'],
  session: {
    key: 'rap2:sess',
  },
  db: {
    dialect: 'mysql',
    host: 'localhost',
    port: 3306,
    username: 'root',
    password: '123456',
    database: 'db_rap2_delos_app',
    pool: {
      max: 5,
      min: 0,
      idle: 10000,
    },
    logging: false,
  },
  redis: {
    host: '127.0.0.1',
    port: 6379
  }
}

export default config

三个文件都按照上面的文件进行配置

自己根据上面定义的数据名字创建 我的是 RAP2_DELOS_APP

CREATE DATABASE IF NOT EXISTS db_rap2_delos_app
DEFAULT CHARSET utf8
COLLATE utf8_general_ci;

3、安装依赖包

进入项目根目录
# cd rap2-delos
安装项目所需依赖
# npm install
全局安装PM2   这是用来启动服务端代码的
# npm install -g pm2
安装 TypeScript 编译包
# npm install typescript -g

4、初始化数据库

执行编译  
# npm run build
如果上面编译报错  rimraf: command not found  则执行 npm install rimraf --save-dev -g  其他错误根据具体去处理

初始化数据库  如果不执行上面的编译 直接执行初始化数据库会报错
# npm run create-db
如果上面报错 sh: cross-env: command not found  则先执行 npm install --save-dev cross-env -g

5、代码规范检查

# npm run check
如果上面报错 sh: tslint: command not found  则先执行 npm install -g tslint typescript

启动项目 下面三种不同的启动方式

开发模式 启动开发模式的服务器 监视并在发生代码变更时自动重启 (第一次运行比较慢,请耐心等待)
# npm run dev

生产模式 启动生产模式服务器
# npm start

没报错代表启动了起来   如果启动失败,请确认node 已经是10版本以上,如果不是 请先切换版本  nvm use v10.15.0

使用pm2指令进行生产模式启动
pm2 start dispatch.js --name=rap-server-delos-prod

RAP2开发环境部署

6、常见问题

1、执行npm run create-db命令,提示Unable to connect to the database:{ SequelizeAccessDeniedError: Access denied for user 'root'@'localhost' (using password:NO)}

    原因:未修改rap2-delos/src/config目录下数据库配置文件,或者是与文件中的数据库信息与之连接的数据库信息不匹配
解决方法:修改config.dev.ts文件数据库配置信息

    如果修改正确无误后,执行npm run create-db依旧出错,那么查看该项目中是否已经存在dist目录,如果有,请按照如上修改对应的数据库配置信息

2、执行npm run dev命令,提示Error: listen EADDRINUSE :::8080
    原因:8080 端口被占用
    解决方法:杀掉占用 8080 端口的应用


3、执行npm install 命令,提示hiredis 编译无法通过
    原因:无权限操作rap2-delos/node_modules/hiredis路径
    解决方法:sudo npm install
    如果提示sudo: npm: command not found,请参考 *-npm,*-node

4、执行npm run dev可以正常启动,npm start命令无法正常启动服务
    原因:请使用pm2 logs查看日志具体定位
    
5、示例:由于 Redis 的安全模式,不能正常使用

    ReplyError: Ready check failed: DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 

    1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 
    2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 
    3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 
    4) Setup a bind address or an authentication password. 
    NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.
解决方法: 使用--protected-mode no方式启动
--------------------- 
作者:清悟 
来源:CSDN 
原文:https://blog.csdn.net/qq_16142851/article/details/85198925 
版权声明:本文为博主原创文章,转载请附上博文链接!

7、客户端dolores环境搭建

1、获取源代码

git clone https://github.com/thx/rap2-dolores.git
执行不成功 多执行几次,或者直接在浏览器打开 https://github.com/thx/rap2-dolores.git 去下载压缩包回来

2、配置文件

目录:rap2-dolores/src/config
文件:config.dev.ts; 其中 dev,表示开发环境,其他同理
修改:config.dev.ts文件,serve地址是 服务端 rap2-delos 部署成功后的地址,默认:'http://localhost:8080'   可以不做修改

最终修改如下 配置文件如下 请注意 ip 那里 不能使用 127.0.0.1 请使用服务器ip (可以尝试使用localhost试试)
module.exports = {
serve: ‘http://ip:8080’,
keys: [‘some secret hurr’],
session: {
key: ‘koa:sess’
}
}
3、安装项目依赖包

# npm install  【如果报下面错误】

错误

gyp ERR! configure error 
gyp ERR! stack Error: EACCES: permission denied, mkdir '/data/wwwroot/rap2-dolores/node_modules/node-sass/.node-gyp'

错误解决

1、在项目根目录创建.npmrc文件,复制下面代码到该文件
phantomjs_cdnurl=http://cnpmjs.org/downloads
sass_binary_site=https://npm.taobao.org/mirrors/node-sass/
registry=https://registry.npm.taobao.org

2、删除之前安装node-sass包
# npm uninstall node-sass
3、重新安装
# npm install -g node-sass

如果上面还是不成功还是报错 
ERR! configure error 
gyp ERR! stack Error: EACCES: permission denied, mkdir '/data/wwwroot/rap2-dolores/node_modules/node-sass/.node-gyp'

安装淘宝 npm
# npm install -g cnpm --registry=https://registry.npm.taobao.org
使用cnpm 安装node-sass
# cnpm install -g node-sass

执行成功后 再执行npm install

4、编译启动项目

01、开发模式 自动监视改变后重新编译
# npm run dev
备注:测试用例
# npm run test

02、生产模式 编译 React 生产包
# npm run build

5、配置服务器 我使用的是nginx

使用 nginx 服务器路由到编译产出的 build 文件夹作为静态服务器即可
 配置如下   8181 是我的前端页面端口
 server {
    listen 8181;
    server_name _;
    
    root /data/wwwroot/rap2-dolores/build;
    index index.html index.htm;
 
    location / {
        try_files $uri /index.html;
    }
 
    error_page 500 502 503 504 /500.html;
}

重新加载配置文件
# systemctl reload nginx

访问服务 ip:8181  成功

如果不成功 一直有圈圈在转 请查看 rap2-dolores/src/config/config.dev.ts 文件 ip 必须是服务器的ip 不能是127.0.0.1

下面配置可以不使用
module.exports = {
  serve: 'http://ip:8080',
  keys: ['some secret hurr'],
  session: {
    key: 'koa:sess'
  }
}

配置完成后 重新运行 npm run build  即可