环境: macOS Catalina + node.js(npm) + mongodb + nginx
参考:https://hellosean1025.github.io/yapi/devops/index.html

安装依赖

# mongodb
brew tap mongodb/brew
brew install mongodb-community@4.2
 
# 查看服务状态
brew services list
 
# 启动 mongodb
brew services start mongodb-community
 
# nginx
brew install nginx
 
# 启动 nginx
sudo brew services start nginx
 
# node.js 直接从官网下载 dmg 包安装

安装 & 运行

第一种办法:

# 安装
npm install -g yapi-cli
 
# 直接运行
yapi server
 
# 放到后台运行
nohup yapi server > /tmp/yapi.log &

安装简单,运行迅速,但是没找到修改端口的办法。由于我本地的 9090 端口已经被占用,导致无法运行。

第二种办法:

mkdir yapi
cd yapi
git clone https://github.com/YMFE/yapi.git vendors
 
# 配置
cp vendors/config_example.json ./config.json
vim config.json # 参见后面的说明,我把端口号改成了 3333
 
# 安装依赖
cd vendors
npm install --production --registry https://registry.npm.taobao.org
 
# 运行
npm run install-server
node server/app.js
 
# 运行成功效果:
MCDTONY:vendors cn-tonylu$ npm run install-server
 
> yapi-vendor@1.9.2 install-server /Users/cn-tonylu/workspace/examples/yapi/vendors
>  node server/install.js
 
log: mongodb load success...
初始化管理员账号成功,账号名:"admin@admin.com",密码:"ymfe.org"
MCDTONY:vendors cn-tonylu$ node server/app.js
log: -------------------------------------swaggerSyncUtils constructor-----------------------------------------------
log: 服务已启动,请打开下面链接访问:
http://127.0.0.1:3333/
log: mongodb load success...

安装后的目录结构如下:

|-- config.json
|-- init.lock
|-- log
`-- vendors
    |-- CHANGELOG.md
    |-- LICENSE
    |-- README.md
    |-- client
    |-- common
    |-- config_example.json
    |-- doc
    |-- exts
    |-- nodemon.json
    |-- npm-debug.log
    |-- package.json
    |-- plugin.json
    |-- server
    |-- static
    |-- test
    |-- webpack.alias.js
    |-- yapi-base-flow.jpg
    |-- ydocfile.js
    `-- ykit.config.js

配置文件参考

{
  "port": "3000",
  "adminAccount": "admin@admin.com",
  "timeout":120000,
  "db": {
    "servername": "127.0.0.1",
    "DATABASE": "yapi",
    "port": 27017,
    "user": "test1",
    "pass": "test1",
    "authSource": ""
  },
  "mail": {
    "enable": true,
    "host": "smtp.163.com",
    "port": 465,
    "from": "***@163.com",
    "auth": {
      "user": "***@163.com",
      "pass": "*****"
    }
  }
}

Nginx 代理转发

首先配置本地的 hosts:

# 在 /etc/hosts 文件里新增一条记录:
127.0.0.1  yapi.tony.com

配置 Nginx:

cd /usr/local/etc/nginx/servers/
vim yapi
 
# 内容如下:
server {
    listen 80;
    server_name yapi.tony.com;
    location / {
        proxy_pass http://127.0.0.1:3333;
    } 
}
 
# 最后重启或重新加载 nginx 配置:
sudo brew services restart nginx

即可通过域名访问: http://yapi.tony.com/

标签: nginx, yapi