官方安装教程: 内网部署
1. 安装nodejs
安装NodeJS
[root@bogon my-yapi]# yum install -y nodejs
验证安装
[root@bogon my-yapi]# node -v
v10.24.0
[root@bogon my-yapi]# npm -v
6.14.11
2. 安装和配置MonGoDB4.4
配置MonGODB的YUM源
[root@bogon yum.repos.d]# vi /etc/yum.repos.d/mongodb-org-4.4.repo
[mongodb-org-4.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.4.asc
安装MonGoDB
[root@bogon yum.repos.d]# yum install -y mongodb-org
查看安装路径
[root@bogon yum.repos.d]# rpm -ql mongodb-org-server
/etc/mongod.conf
/run/mongodb
/usr/bin/mongod
/usr/lib/systemd/system/mongod.service
/usr/share/doc/mongodb-org-server-4.0.28
/usr/share/doc/mongodb-org-server-4.0.28/LICENSE-Community.txt
/usr/share/doc/mongodb-org-server-4.0.28/MPL-2
/usr/share/doc/mongodb-org-server-4.0.28/README
/usr/share/doc/mongodb-org-server-4.0.28/THIRD-PARTY-NOTICES
/usr/share/man/man1/mongod.1.gz
/var/lib/mongo
/var/log/mongodb
/var/log/mongodb/mongod.log
配置MongoDB开机启动
[root@bogon yum.repos.d]# systemctl enable mongod
[root@bogon yum.repos.d]# systemctl start mongod
[root@bogon yum.repos.d]# systemctl status mongod
3.禁用selinux
[root@bogon yum.repos.d]# vi /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
4. 安装yapi
安装Yapi
[root@bogon yum.repos.d]# npm install -g yapi-cli --registry https://registry.npm.taobao.org
初始化 YApi ,执行yapi server命令,按提示操作
[root@bogon bin]# ./yapi server
在浏览器打开 http://0.0.0.0:9090 访问。非本地服务器,请将 0.0.0.0 替换成指定的域名或ip
从浏览器打开后,出现下面的部署页面
点击开始部署后,出现
“Error: getaddrinfo ENOTFOUND yapi.demo.qunar.com yapi.demo.qunar.com:80”错误
网上搜索了很多帖子,解决方法均不适用,但知道问题原因在于yapi.demo.qunar.com这个网站已经不再维护。
手动将URL换成http://registry.npm.taobao.org/yapi-vendor/download/yapi-vendor-1.10.2.tgz后解决这个问题,方法如下:
[root@bogon src]# cd [node安装路径]
[root@bogon src]#vi lib/node_modules/yapi-cli/src/utils.js
将url = 'http://yapi.demo.qunar.com/publicapi/archive/' + version;
换成url = 'http://registry.npm.taobao.org/yapi-vendor/download/yapi-vendor-1.10.2.tgz'
部署成功!
部署过程中出现
“gyp
WARN EACCES user "root" does not have permission to access the dev dir "/root/.node-gyp/10.9.0"
gyp
WARN EACCES attempting to reinstall using temporary dev dir "/usr/local/yapi/vendors/node_modules/dtrace-provider/.node-gyp"
gyp
WARN install got an error, rolling back install
gyp WARN
install got an error, rolling back install
gyp
ERR! configure error
gyp ERR! stack Error: EACCES: permission denied, mkdir '/usr/local/yapi/vendors/node_modules/dtrace-provider/.node-gyp'
gyp
ERR! System Linux 3.10.0-1160.62.1.el7.x86_64
gyp
ERR! command "/opt/node-v10.9.0-linux-x64/bin/node" "/opt/node-v10.9.0-linux-x64/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /usr/local/yapi/vendors/node_modules/dtrace-provider
gyp ERR! node -v v10.9.0
gyp ERR! node-gyp -v v3.7.0
gyp ERR! not ok
”错误,但似乎不影响使用。
访问http://http://172.31.115.73:3000/后成功显示yapi界面
5. 安装pm2设置后台启动
[root@bogon src]#npm install -g pm2
使用PM2后台启动Yapi服务
[root@bogon src]#pm2 restart /usr/local/my-yapi/vendors/server/app.js
6. 运行一段时间后,服务器异常断电,PM2重启YAPI就失败了,尝试使用
# node /usr/local/my-yapi/vendors/server/app.js &
后成功登录。
但是日志中显示错误
[root@bogon my-yapi]# node vendors/server/app.js
log: -------------------------------------swaggerSyncUtils constructor-----------------------------------------------
log: 服务已启动,请打开下面链接访问:
http://127.0.0.1:3000/
log: mongodb load success...
log: 定时器触发, syncJsonUrl:http://192.168.15.161:8089/v2/api-docs,合并模式:merge
log: 定时器触发, syncJsonUrl:http://172.31.192.61:8089/v2/api-docs,合并模式:merge
log: 获取数据失败http status "connect ECONNREFUSED 192.168.15.161:8089"获取数据失败,请确认 swaggerUrl 是否正确
所以务必做好接口备份,这个工具莫名其妙的错误很多。