内网部署YApi
官网地址:https://hellosean1025.github.io/yapi/devops/index.html
环境要求
- nodejs(7.6+)
- mongodb(2.6+),安装看这篇文章:https://www.cnblogs.com/sanduzxcvbnm/p/15868006.html
安装,命令行部署
mkdir yapi
cd yapi
git clone https://github.com/YMFE/yapi.git vendors //或者下载 zip 包解压到 vendors 目录(clone 整个仓库大概 140+ M,可以通过 `git clone --depth=1 https://github.com/YMFE/yapi.git vendors` 命令减少,大概 10+ M)
cp vendors/config_example.json ./config.json //复制完成后请修改相关配置,主要是修改连接mongodb的库名,访问用户名和密码
cd vendors
npm install --production --registry https://registry.npm.taobao.org
npm run install-server //安装程序会初始化数据库索引和管理员账号,管理员账号名可在 config.json 配置
# 提示
# 初始化管理员账号成功,账号名:"admin@admin.com",密码:"ymfe.org"
node server/app.js //启动服务器后,请访问 127.0.0.1:{config.json配置的端口},初次运行会有个编译的过程,请耐心等候,后面使用pm2进行服务管理,这一步如果运行的话需要kill掉
在执行这一步(npm install --production --registry https://registry.npm.taobao.org)时报错如下:
npm ERR! code ETARGET
npm ERR! notarget No matching version found for dify@1.0.5.
npm ERR! notarget In most cases you or one of your dependencies are requesting
npm ERR! notarget a package version that doesn't exist.
npm ERR! notarget
npm ERR! notarget It was specified as a dependency of 'vendors'
npm ERR! notarget
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2022-02-07T07_44_03_504Z-debug.log
解决办法:
rm -rf node_modules
npm cache clean --force
rm package-lock.json
服务管理
推荐使用 pm2 管理 node 服务器启动,停止
npm install pm2 -g //安装pm2
cd {项目目录} # cd yapi
pm2 start "vendors/server/app.js" --name yapi //pm2管理yapi服务
pm2 info yapi //查看服务信息
pm2 stop yapi //停止服务
pm2 restart yapi //重启服务
设置开机自启动
pm2 startup
pm2 save
运行pm2 startup
在/etc/systemd/system/
目录下生成pm2-root的启动脚本,且自动将pm2-root设为服务。
# pm2 startup
[PM2] Init System found: systemd
Platform systemd
Template
[Unit]
Description=PM2 process manager
Documentation=https://pm2.keymetrics.io/
After=network.target
[Service]
Type=forking
User=root
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
Environment=PATH=/usr/lib64/qt-3.3/bin:/usr/local/node_14.0.0/bin:/usr/local/apache-maven-3.6.3/bin:/usr/local/jdk1.8.0_231/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/etc/alternatives/java_sdk_11/bin:/server/rabbitmq/bin:/server/rabbitmq/rabbitmq_server-3.7.8/sbin:/root/bin:/usr/local/mysql/bin:/usr/local/mysql/lib:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
Environment=PM2_HOME=/root/.pm2
PIDFile=/root/.pm2/pm2.pid
Restart=on-failure
ExecStart=/usr/local/node_14.0.0/lib/node_modules/pm2/bin/pm2 resurrect
ExecReload=/usr/local/node_14.0.0/lib/node_modules/pm2/bin/pm2 reload all
ExecStop=/usr/local/node_14.0.0/lib/node_modules/pm2/bin/pm2 kill
[Install]
WantedBy=multi-user.target
Target path
/etc/systemd/system/pm2-root.service
Command list
[ 'systemctl enable pm2-root' ]
[PM2] Writing init configuration in /etc/systemd/system/pm2-root.service
[PM2] Making script booting at startup...
[PM2] [-] Executing: systemctl enable pm2-root...
Created symlink from /etc/systemd/system/multi-user.target.wants/pm2-root.service to /etc/systemd/system/pm2-root.service.
[PM2] [v] Command successfully executed.
+---------------------------------------+
[PM2] Freeze a process list on reboot via:
$ pm2 save
[PM2] Remove init script via:
$ pm2 unstartup systemd
运行pm2 save
会将当前pm2所运行的应用保存在/root/.pm2/dump.pm2
下,当开机重启时,运行pm2-root服务脚本,并且到/root/.pm2/dump.pm2
下读取应用并启动。
# pm2 save
[PM2] Saving current process list...
>>>> In-memory PM2 is out-of-date, do:
>>>> $ pm2 update
In memory PM2 version: 5.1.2
Local PM2 version: 5.2.0
[PM2] Successfully saved in /root/.pm2/dump.pm2
升级
cd {项目目录} # cd yapi
yapi ls //查看版本号列表
yapi update //更新到最新版本
yapi update -v {Version} //更新到指定版本
测试
访问后新建一个项目,使用测试swagger.json: https://petstore.swagger.io/v2/swagger.json
安装其他插件 ==> 不建议安装
安装其他插件后,原来页面上少了swagger同步的信息
其他插件