centos7.x安裝最新版YAPI(1.18.8)

官方地址

https://github.com/YMFE/yapi

機器環境

lsb_release -a

在這裏插入圖片描述

安裝YAPI基礎環境

  • nodejs(7.6+)
  • mongodb(2.6+)
  • git

nodejs安裝

參見我另一篇博文:
https://blog.csdn.net/weixin_42183854/article/details/104819890

安裝mongodb

參見另一篇博文:
https://blog.csdn.net/weixin_42183854/article/details/104804860

安裝包編譯的方式安裝

參照官方issues裏面的一個回答
https://github.com/YMFE/yapi/issues/1605

1.創建文件夾

mkdir /opt/YApi

2.下載代碼

cd /opt/YApi

git clone https://gitee.com/mirrors/YApi.git vendors

3.修改配置

cp vendors/config_example.json ./config.json

在這裏插入圖片描述

vim config.json

在這裏插入圖片描述

4.進入mongo創建數據庫和用戶密碼等

我這裏用的是docker生成的mongo

docker exec -it mongo bash
mongo
use yapi
# 剛纔寫的用戶和密碼
db.createUser({user: "yapi", pwd: "yapi", roles: [{ role: "dbOwner", db: "yapi" }]})

在這裏插入圖片描述
退出exit

5.修改代碼中的連接參數,不然抱錯

cd vendors

vim server/utils/db.js
* 原代碼:let options = {useNewUrlParser: true, useCreateIndex: true};
* 修改爲: let options = {useNewUrlParser: true, useCreateIndex: true,useUnifiedTopology: true };

在這裏插入圖片描述
在這裏插入圖片描述

6.安裝

還是再vendors目錄下

npm install --production --registry https://registry.npm.taobao.org
npm run install-server 

抱錯

> [email protected] install /opt/YApi/vendors/node_modules/dtrace-provider
> node-gyp rebuild || node suppress-error.js

gyp WARN EACCES user "root" does not have permission to access the dev dir "/root/.node-gyp/8.12.0"
gyp WARN EACCES attempting to reinstall using temporary dev dir "/opt/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 '/opt/YApi/vendors/node_modules/dtrace-provider/.node-gyp'
gyp ERR! System Linux 3.10.0-1062.1.1.el7.x86_64
gyp ERR! command "/usr/local/node-v8.12.0/bin/node" "/usr/local/node-v8.12.0/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /opt/YApi/vendors/node_modules/dtrace-provider
gyp ERR! node -v v8.12.0
gyp ERR! node-gyp -v v3.8.0
gyp ERR! not ok
added 334 packages from 322 contributors in 14.925s

在這裏插入圖片描述
解決方式:改權限

chmod -R 777 /opt/YApi
# 然後再執行  
npm install --production --registry https://registry.npm.taobao.org  
npm run install-server 

在這裏插入圖片描述
到這裏就安裝完成了,我們可以啓動了.

7.啓動

簡單啓動

掛掉控制檯,就不能用了

node server/app.js

當然可以 放到後臺

pm2管理

## 安裝
 cnpm install pm2 
# 開機啓動pm2
pm2 startup

因爲太慢,所以我用cnpm安裝的
在這裏插入圖片描述
pm2 啓動yapi
指向安裝目錄下的app.js

pm2 start /opt/YApi/vendors/server/app.js

在這裏插入圖片描述
pm常用命令
https://blog.csdn.net/chengxuyuanyonghu/article/details/74910875

8.訪問ip:3000(默認)

用戶名密碼是剛纔輸出的
默認是:
賬號名:“[email protected]”,密碼:“ymfe.org”
在這裏插入圖片描述
在這裏插入圖片描述

官方安裝

不用官方安裝的原因,
安裝最新版的時候,總是再出錯.

下載運行

npm install -g yapi-cli --registry https://registry.npm.taobao.org
yapi server

在這裏插入圖片描述

填寫配置

在這裏插入圖片描述
點擊部署

錯誤一

數據庫連接錯誤


Error:  (node:1787) 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 /sixmillions/yapi/vendors/  
ls
vim server/utils/db.js
# 添加
 useUnifiedTopology: true

在這裏插入圖片描述
在這裏插入圖片描述

錯誤二

權限錯誤

 gyp
  WARN EACCES user "root" does not have permission to access the dev dir "/root/.node-gyp/8.12.0"

 gyp WARN EACCES attempting to reinstall using temporary dev dir "/opt/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 '/opt/YApi/vendors/node_modules/dtrace-provider/.node-gyp'

 gyp
  ERR! System Linux 3.10.0-1062.1.1.el7.x86_64

 gyp ERR! command "/usr/local/node-v8.12.0/bin/node" "/usr/local/node-v8.12.0/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /opt/YApi/vendors/node_modules/dtrace-provider
gyp ERR! node -v v8.12.0
gyp
 ERR! node-gyp -v v3.8.0
gyp ERR! not ok

在這裏插入圖片描述

解決方式

修改安裝目錄權限

chmod -R 755 /sixmillions/yapi

再運行

進入/sixmillions/yapi/vendors目錄(安裝目錄下的vendors)
cd /sixmillions/yapi/vendors
npm run install-server

抱錯

在這裏插入圖片描述

解決辦法

刪除這個文件
文件在安裝目錄下

cd /sixmillions/yapi
ls
rm -rf init.lock

在這裏插入圖片描述

再運行

npm run install-server

抱錯

數據庫認證失敗錯誤

> [email protected] install-server /sixmillions/yapi/vendors
>  node server/install.js

log: mongodb load success...
(node:8921) UnhandledPromiseRejectionWarning: Error: 初始化管理員賬號 "[email protected]" 失敗, E11000 duplicate key error collection: myyapi.user index: email_1 dup key: { email: "[email protected]" }
    at /sixmillions/yapi/vendors/server/install.js:146:17
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:189:7)
(node:8921) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:8921) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

在這裏插入圖片描述

解決辦法

進入數據創建用戶密碼然後修改配置再運行

進入數據庫

docker exec -it mongotest bash
mongo
use yapi
db.createUser({user: "yapi", pwd: "yapi", roles: [{ role: "dbOwner", db: "yapi" }]})

給yapi數據庫創建了用戶名和密碼都是yapi
在這裏插入圖片描述
退出 exit

改config

進入安裝目錄

cd /sixmillions/yapi
vim config.json
# 和下面對比,修改相關項目
{
   "port": "3000",
   "adminAccount": "[email protected]",
   "db": {
      "servername": "127.0.0.1",
      "DATABASE": "yapi",
      "port": "27017",
      "user":"yapi",
      "pass":"yapi",
      "authSource":""
   },
   "mail": {
      "enable": false,
      "host": "smtp.163.com",
      "port": 465,
      "from": "***@163.com",
      "auth": {
         "user": "***@163.com",
         "pass": "*****"
      }
   }
}

在這裏插入圖片描述
在這裏插入圖片描述
保存後在運行

再運行

cd /sixmillions/yapi/vendors/
npm run install-server

在這裏插入圖片描述

成功

參上上面安裝包的方法,運行項目

已經安裝pm2

pm2 start /sixmillions/yapi/vendors/server/app.js --name="yapitest"

在這裏插入圖片描述
可以看到我們的成功運行了(那個app是安裝包安裝的yapi,讓我先停止了)

查看

在這裏插入圖片描述

碼字不易,大家多多提鞋

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章