环境要求
- nodejs(7.6+)
- mongodb(2.6+)
- git
安装
方式一. 可视化部署[推荐]
执行 yapi server 启动可视化部署程序,输入相应的配置和点击开始部署,就能完成整个网站的部署。部署完成之后,可按照提示信息,执行 node/{网站路径/server/app.js} 启动服务器。在浏览器打开指定url, 点击登录输入您刚才设置的管理员邮箱,默认密码(ymfe.org) 登录系统(默认密码可在个人中心修改)。npm install -g yapi-cli --registry https://registry.npm.taobao.orgyapi server
方式二. 命令行部署
如果 github 压缩文件无法下载,或需要部署到一些特殊的服务器,可尝试此方法mkdir yapicd yapigit clone https://github.com/YMFE/yapi.git vendors //或者下载 zip 包解压到 vendors 目录cp vendors/config_example.json ./config.json //复制完成后请修改相关配置cd vendorsnpm install --production --registry https://registry.npm.taobao.orgnpm run install-server //安装程序会初始化数据库索引和管理员账号,管理员账号名可在 config.json 配置node server/app.js //启动服务器后,请访问 127.0.0.1:{config.json配置的端口},初次运行会有个编译的过程,请耐心等候
安装后的目录结构如下:
|-- 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
Yapi-cli
[root@pe-tmct01 server]# yapi-cli --helpCommands:build 插件安装import 导入接口数据install 部署 YApi 项目,建议使用 yapi server 进行可视化部署ls 获取版本信息plugin 插件安装server 可视化部署 YApi 平台unplugin 插件卸载update 更新 YApi 平台版本Options:--help Show help [boolean]--version Show version number [boolean]
其他配置
配置邮箱
打开项目目录 config.json 文件,新增 mail 配置, 替换默认的邮箱配置
{"port": "*****","adminAccount": "********","db": {...},"mail": {"enable": true,"host": "smtp.163.com", //邮箱服务器"port": 465, //端口"from": "***@163.com", //发送人邮箱"auth": {"user": "***@163.com", //邮箱服务器账号"pass": "*****" //邮箱服务器密码}}}
如何申请STMP服务器账号和密码可以参考下面的教程:如何开通电子邮箱的SMTP功能
配置LDAP登录
打开项目目录 config.json 文件,添加如下字段:
{"port": "*****","adminAccount": "********","db": {...},"mail": {...},"ldapLogin": {"enable": true,"server": "ldap://l-ldapt1.com","baseDn": "CN=Admin,CN=Users,DC=test,DC=com","bindPassword": "password123","searchDn": "OU=UserContainer,DC=test,DC=com","searchStandard": "mail", // 自定义格式: "searchStandard": "&(objectClass=user)(cn=%s)""emailPostfix": "@163.com","emailKey": "mail","usernameKey": "name"}}
这里面的配置项含义如下:
enable表示是否配置 LDAP 登录,true(支持 LDAP登录 )/false(不支持LDAP登录);serverLDAP 服务器地址,前面需要加上 ldap:// 前缀,也可以是 ldaps:// 表示是通过 SSL 连接;baseDnLDAP 服务器的登录用户名,必须是从根结点到用户节点的全路径(非必须);bindPassword登录该 LDAP 服务器的密码(非必须);searchDn查询用户数据的路径,类似数据库中的一张表的地址,注意这里也必须是全路径;searchStandard查询条件,这里是 mail 表示查询用户信息是通过邮箱信息来查询的。注意,该字段信息与LDAP数据库存储数据的字段相对应,如果如果存储用户邮箱信息的字段是 email, 这里就需要修改成 email.(1.3.18+支持)自定义filter表达式,基本形式为:&(objectClass=user)(cn=%s), 其中%s会被username替换emailPostfix登陆邮箱后缀(非必须)emailKey: ldap数据库存放邮箱信息的字段(v1.3.21 新增 非必须)usernameKey: ldap数据库存放用户名信息的字段(v1.3.21 新增 非必须)禁止注册
在 config.json 添加closeRegister:true配置项,就可以禁止用户注册 yapi 平台,修改完成后,请重启 yapi 服务器。{"port": "*****","closeRegister":true}
版本通知
(v1.3.19+ 增加)在 config.json 添加"versionNotify": true配置项,就可以开启版本通知功能,默认为false,修改完成后,请重启 yapi 服务器。{"port": "******","adminAccount": "*****","versionNotify": true}
启动服务
nohup node /opt/tmp/test/node_modules/bin/my-yapi/vendors/server/app.js &配置监听
Yapi的服务不稳定,有时候服务会宕。使用forever保证服务的稳定性。
步骤:
1.安装forever
2.配置脚本
3.启动脚本
4.验证
安装 yum install -g forever
配置脚本
var spawn = require('child_process').spawn;free = spawn('node', ['/opt/tmp/test/node_modules/bin/my-yapi/vendors/server/app.js']);free.stdout.on('data', function (data) {console.log('standard output:\n' + data);});free.stderr.on('data', function (data) {console.log('standard error output:\n' + data);});free.on('exit', function (code, signal) {console.log('child process eixt ,exit:' + code);});
启动脚本**forever --minUptime 10000 --spinSleepTime 26000 start app.js**
验证
启动服务之后,在kill掉。过一会儿看服务是否会自动重启。
**
