keyfil/教程/单点/20250103-YApi部署指南.md
liuxiaohua 3bfea7cda3
Some checks failed
Publish to Confluence / confluence (push) Failing after 1m15s
[2025-05-07] 添加端口转发文档
2025-05-07 11:33:02 +08:00

6.2 KiB
Raw Blame History

YApi 部署指南

Docker 安装

项目准备

拉取项目

mkdir yapi

cd yapi

git clone https://xxxx.xxxx.xx/yapi.git vendors

cp vendors/config_cp.json ./config.json

准备配置文件 yapi/config.json

项目结构

- yapi
--- vendors
----- package.json
----- config_cp.json
----- server
----- Dockerfile
--- config.json

Dockerfile

FROM node:12-alpine3.15
ENV TZ="Asia/Shanghai"
#-使用阿里云镜像
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
WORKDIR /yapi/vendors
COPY .. /yapi/vendors/

RUN mv /yapi/vendors/config_cp.json /yapi/config.json

RUN npm config set registry https://registry.npmmirror.com

#-构建 yapi
RUN apk add --no-cache wget python2 make g++ && cd /yapi/vendors && npm set strict-ssl false && npm install --production --registry https://registry.npmmirror.com

#-插件安装 
RUN npm install -g ykit --registry https://registry.npmmirror.com

RUN npm install yapi-plugin-kc-oidc --registry https://registry.npmmirror.com

RUN npm rebuild node-sass --registry https://registry.npmmirror.com

RUN npm run build-client

EXPOSE 3000
ENTRYPOINT ["node"]

构建镜像 yapi/vendors

docker build -t qifu-base-yapi:1.0.0 .

启动 YApi

初始化数据库(可选,这边已有,不需要再执行)

  • 由于之前已经有 YApi 了,所以这一步不需要
  • config.json 在 yapi
  • 指令跟着实际情况改改
docker run -d --rm \
  --name yapi-init \
  --link mongodb:mongo \
  --net=yapi \
  -v $PWD/config.json:/yapi/config.json \
  qifu-base-yapi:1.0.0 \
  server/install.js
#-初始化管理员账号在上面的 config.json 配置中 hexiaohei1024@gmail.com初始密码是 ymfe.org可以登录后进入个人中心修改

启动 YApi

  • 指令跟着实际情况改改
docker run -d \
   --name yapi \
   --link mongodb:mongo \
   --restart always \
   --net=yapi \
   -p 3000:3000 \
   -v $PWD/config.json:/yapi/config.json \
   qifu-base-yapi:1.0.0 \
   server/app.js

宿主机安装

Nodejs 安装 版本12

  • 下载 Nodejs下载地址
  • 解压
  • 配置环境变量 /etc/profile
  • 使环境变量生效 source /etc/profile

Python 安装

可能遇到的问题

YAPI 安装启动

#-配置淘宝镜像
npm config set registry https://registry.npmmirror.com
#-安装windows构建工具, windows 才需要
##-npm install --g --production windows-build-tools

#-构建 yapi
mkdir yapi
#-进入文件夹
cd yapi
#-或者下载 zip 包解压到 vendors 目录clone 整个仓库大概 140+ M可以通过 `git clone --depth=1 https://github.com/YMFE/yapi.git vendors` 命令减少,大概 10+ M
git clone https://github.com/YMFE/yapi.git vendors 
#-复制完成后请修改相关配置
cp vendors/config_example.json ./config.json 
cd vendors

npm install --production --registry https://registry.npmmirror.com
#-npm install --production --unsafe-perm=true --allow-root --registry https://registry.npmmirror.com
#-安装程序会初始化数据库索引和管理员账号,管理员账号名可在 config.json 配置
npm run install-server 
#-启动服务器后,请访问 127.0.0.1:{config.json配置的端口},初次运行会有个编译的过程,请耐心等候
node server/app.js 

YAPI OIDC 配置

可以参考:https://www.npmjs.com/package/yapi-plugin-kc-oidc?activeTab=readme

  • config.json 添加配置 plugins 节点

  • 配置文件

  • 执行编译

#-2.0 进入工作目录
cd yapi/vendors

#-2.1 安装所有依赖包
##-安装过程中可能出现node-sass报错替换到新的版本就可解决比如我将"node-sass": "^4.9.0" 替换成了 "node-sass": "^4.14.0"
##-sed -i s/'"node-sass": "^4.9.0"'/'"node-sass": "^4.14.0"'/ package.json
npm install --registry https://registry.npmmirror.com

#-2.2 安装ykit工具
npm install -g ykit --registry https://registry.npmmirror.com

#-2.3 安装yapi-plugin-kc-oidc
npm install yapi-plugin-kc-oidc --registry https://registry.npmmirror.com
##-或者不用npm安装自己去git clone代码
##-cd node_modules && git clone --depth 1 https://gitee.com/joshu/yapi-plugin-kc-oidc.git

#-2.4 开始编译
##-cd yapi/vendors
##-linux
npm run build-client

##-windows
###-set NODE_ENV=production
###-ykit pack -m

配置文件

{
  "port": "3000",
  "adminAccount": "admin@admin.com",
  "timeout": 120000,
  "db": {
    "servername": "127.0.0.1",
    "DATABASE": "yapi",
    "port": 27017,
    "user": "test",
    "pass": "test"
  },
  "mail": {
    "enable": true,
    "host": "smtp.163.com",
    "port": 465,
    "from": "***@163.com",
    "auth": {
      "user": "***@163.com",
      "pass": "*****"
    }
  },
  "plugins": [
    {
      "name": "kc-oidc",
      "options": {
        "authUrl": "",
        "tokenUrl": "",
        "userUrl": "",
        "callbackUrl": "${host}/api/user/login_by_token",
        "clientId": "",
        "clientSecret": "",
        "scope": "openid email profile phone ",
        "userKey": "preferred_username",
        "emailPostfix": "@xxx.com"
      }
    }
  ]
}
  • mongodb 没有开启鉴权则删除 userpass 字段

参考