搭建Yapi接口文檔服務
標籤(空格分隔): linux
官方文檔
https://hellosean1025.github.io/yapi/devops/index.html
系統環境配置
# 關閉firewalld防火牆並禁止開機自啓動
systemctl disable firewalld && systemctl stop firewalld
# 臨時和永久關閉SElinux
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
# 清空iptables規則
iptables -X && iptables -F && iptables -Z
安裝Node、Npm
# 下載並解壓文件
cd /opt
wget https://nodejs.org/dist/v9.8.0/node-v9.8.0-linux-x64.tar.xz
xz -d node-v9.8.0-linux-x64.tar.xz
tar -xf node-v9.8.0-linux-x64.tar
# 創建鏈接文件
cd node-v9.8.0-linux-x64
ln -s /opt/node-v9.8.0-linux-x64/bin/node /usr/local/bin/node
ln -s /opt/node-v9.8.0-linux-x64/bin/npm /usr/local/bin/npm
# 查看版本並切換鏡像源
node -v
npm -v
npm config set registry https://registry.npm.taobao.org
安裝Mongodb
# 下載包
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.4.2.tgz
tar zxvf mongodb-linux-x86_64-rhel70-4.4.2.tgz -C /usr/local/
mv /usr/local/mongodb-linux-x86_64-rhel70-4.4.2 /usr/local/mongodb
rm -rf mongodb-linux-x86_64-rhel70-4.4.2.tgz
cd /usr/local/mongodb/
mkdir logs data conf
cd logs/
touch mongodb.logs
cd ../conf/
vim mongodb.conf
systemLog:
quiet: false
path: /usr/local/mongodb/logs/mongodb.logs
logAppend: false
destination: file
processManagement:
fork: true
pidFilePath: /usr/local/mongodb/bin/mongodb.pid
net:
bindIp: 0.0.0.0
port: 27017
maxIncomingConnections: 65536
wireObjectCheck: true
storage:
dbPath: /usr/local/mongodb/data
journal:
enabled: true
operationProfiling:
slowOpThresholdMs: 100
mode: off
# mongod 開啓自啓
vim /etc/systemd/system/mongodb.service
[Unit]
Description=mongodb
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
RuntimeDirectory=mongodb
RuntimeDirectoryMode=0751
PIDFile=/usr/local/mongodb/bin/mongodb.pid
ExecStart=/usr/local/mongodb/bin/mongod --config /usr/local/mongodb/conf/mongodb.conf
ExecStop=/usr/local/mongodb/bin/mongod --shutdown --config /usr/local/mongodb/conf/mongodb.conf
PrivateTmp=false
[Install]
WantedBy=multi-user.target
systemctl start mongodb.service
systemctl enable mongodb.service
# 查看端口
ss -tunl | grep 27017
安裝Git
yum install git -y
可視化部署Yapi
npm install -g yapi-cli --registry https://registry.npm.taobao.org
/opt/node-v9.8.0-linux-x64/bin/yapi server
根據命令行提示信息,在瀏覽器中訪問部署頁面
部署過程中遇到的坑
#如果出現以下錯誤,請安裝如下模塊文件並刷新頁面重新部署
Error: Cannot find module 'fs-extra'
npm install --save fs-extra
#如果出現如下錯誤,請安裝如下模塊文件並刷新頁面重新部署
Error: Cannot find module 'nodemailer'
npm install nodemailer --save
#如果出現如下錯誤,請修改添加以下路徑下的db.js文件內容並刷新頁面重新部署
Error: (node:687) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor
cd /etc/my-yapi/vendors/server/utils
在db.js文件的第19行處下面添加如下內容
mongoose.set('useUnifiedTopology', true);
# 如果出現如下錯誤,請根據提示刪除init.py文件
node server/install.js
Error: init.lock文件已存在,請確認您是否已安裝。如果需要重新安裝,請刪掉init.lock文件
進入/etc/my-yapi/目錄
rm init.lock -y
# 如果重新安裝,出現如下錯誤,請刪除管理員賬號信息
cd /etc/my-yapi/vendors
node server/install.js
(node:20024) UnhandledPromiseRejectionWarning: Error: 初始化管理員賬號 "[email protected]" 失敗, E11000 duplicate key error collection: yapi.user index: email_1 dup key: { : "[email protected]" }
# 進入數據庫刪除管理員賬戶信息
mongo
> use yapi;
> db.user.remove({"username":"admin"});
node server/install.js
啓動服務
cd /etc/my-yapi/vendors
node server/app.js