CentOS7 部署yapi API 文檔管理工具

yapi是高效、易用、功能強大的API管理平臺,旨在爲開發、產品、測試人員提供更優雅的接口管理服務 html

部署環境要求:node

1)nodejs(7.6+) linux

2)mongodb(2.6+)git

1、部署nodejs

參考文檔:https://www.cnblogs.com/hujinzhong/p/11994526.htmlgithub

[root@yapi ~]# node -v
v12.13.1
[root@yapi ~]# npm -v
6.12.1
[root@yapi ~]# pm2 -v
4.2.0

2、部署mongodb

參考文檔:https://www.cnblogs.com/hujinzhong/p/11621909.htmlmongodb

#一、關閉大葉內存
[root@yapi ~]# echo never > /sys/kernel/mm/transparent_hugepage/enabled   
[root@yapi ~]# echo never > /sys/kernel/mm/transparent_hugepage/defrag

#二、修改文件描述符
[root@yapi ~]# vim /etc/security/limits.conf
#*               -       nofile          65535  #註釋便可,reboot重啓生效

#三、建立用戶
[root@yapi ~]# useradd mongod
[root@yapi ~]# echo 123456|passwd --stdin mongod

#四、建立mongodb所需目錄結構
[root@yapi ~]# mkdir -p /mongodb/{bin,conf,log,data}

#五、上傳並解壓軟件到指定位置
[root@yapi ~]# mkdir /server/tools -p
[root@yapi ~]# mv mongodb-linux-x86_64-rhel70-v3.6-latest.gz /server/tools/
[root@yapi ~]# ll /server/tools/
total 117936
-rw-r--r-- 1 root root 120764476 Dec  6 15:19 mongodb-linux-x86_64-rhel70-v3.6-latest.gz
[root@yapi ~]# cd /server/tools/
[root@yapi tools]# tar xf mongodb-linux-x86_64-rhel70-v3.6-latest.gz 
[root@yapi tools]# cp -a mongodb-linux-x86_64-rhel70-3.6.11-14-g48d999c/bin/* /mongodb/bin/

#六、設置目錄權限
[root@yapi tools]# chown -R mongod:mongod /mongodb
[root@yapi tools]# ll /mongodb
total 0
drwxr-xr-x 2 mongod mongod 248 Dec  6 15:27 bin
drwxr-xr-x 2 mongod mongod   6 Dec  6 15:24 conf
drwxr-xr-x 2 mongod mongod   6 Dec  6 15:24 data
drwxr-xr-x 2 mongod mongod   6 Dec  6 15:24 log

#七、設置用戶環境變量
[root@yapi tools]# su - mongod
[mongod@yapi ~]$ vim .bash_profile
export PATH=/mongodb/bin:$PATH
[mongod@yapi ~]$ source .bash_profile

#八、啓動mongodb(切換到mongod)
[mongod@yapi ~]$ mongod --dbpath=/mongodb/data --logpath=/mongodb/log/mongodb.log --port=27017 --logappend --fork
about to fork child process, waiting until server is ready for connections.
forked process: 40637
child process started successfully, parent exiting

#九、登陸mongodb
[mongod@yapi ~]$ mongo
MongoDB shell version v3.6.11-14-g48d999c
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("a00b73d1-cb47-45f2-a915-086e20a90768") }
MongoDB server version: 3.6.11-14-g48d999c
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
	http://docs.mongodb.org/
Questions? Try the support group
	http://groups.google.com/group/mongodb-user
Server has startup warnings: 
2019-12-06T15:31:59.318+0800 I CONTROL  [initandlisten] 
2019-12-06T15:31:59.318+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-12-06T15:31:59.318+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2019-12-06T15:31:59.318+0800 I CONTROL  [initandlisten] 
2019-12-06T15:31:59.318+0800 I CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.
2019-12-06T15:31:59.318+0800 I CONTROL  [initandlisten] **          Remote systems will be unable to connect to this server. 
2019-12-06T15:31:59.318+0800 I CONTROL  [initandlisten] **          Start the server with --bind_ip <address> to specify which IP 
2019-12-06T15:31:59.318+0800 I CONTROL  [initandlisten] **          addresses it should serve responses from, or with --bind_ip_all to
2019-12-06T15:31:59.318+0800 I CONTROL  [initandlisten] **          bind to all interfaces. If this behavior is desired, start the
2019-12-06T15:31:59.318+0800 I CONTROL  [initandlisten] **          server with --bind_ip 127.0.0.1 to disable this warning.
2019-12-06T15:31:59.318+0800 I CONTROL  [initandlisten] 
> show dbs
admin   0.000GB
config  0.000GB
local   0.000G

#十、編輯配置文件
[mongod@yapi ~]$ vim /mongodb/conf/mongo.conf
systemLog:
   destination: file
   path: "/mongodb/log/mongodb.log"
   logAppend: true
storage:
   journal:
      enabled: true
   dbPath: "/mongodb/data/"
processManagement:
   fork: true
net:
   port: 27017
   bindIp: 192.168.11.11,127.0.0.1

#十一、重啓mongodb
[mongod@yapi ~]$ mongod -f /mongodb/conf/mongo.conf --shutdown
killing process with pid: 40637
[mongod@yapi ~]$ mongod -f /mongodb/conf/mongo.conf 
about to fork child process, waiting until server is ready for connections.
forked process: 42694
child process started successfully, parent exiting

3、部署yapi

參考文檔:docker

https://www.linuxidc.com/Linux/2018-01/150513.htmshell

https://blog.csdn.net/kxzhaohuan/article/details/81713949數據庫

https://hellosean1025.github.io/yapi/devops/index.htmlnpm

[root@yapi ~]# npm install -g yapi-cli --registry https://registry.npm.taobao.org
/usr/local/node/bin/yapi -> /usr/local/node/lib/node_modules/yapi-cli/bin/yapi-cli
/usr/local/node/bin/yapi-cli -> /usr/local/node/lib/node_modules/yapi-cli/bin/yapi-cli
+ yapi-cli@1.5.0
added 266 packages from 125 contributors in 31.938s
[root@yapi ~]# yapi server
在瀏覽器打開 http://0.0.0.0:9090 訪問。非本地服務器,請將 0.0.0.0 替換成指定的域名或ip

image

根據提示,瀏覽器訪問 http://部署YApi服務器的IP:9090。

image

若是報錯以下:

image

能夠考慮更換aliyun的dns地址

[root@yapi ~]# cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 223.5.5.5
nameserver 114.114.114.114

image

啓動yapi:

[root@yapi my-yapi]# node vendors/server/app.js &>/dev/null &
[1] 54729   
[root@yapi my-yapi]# netstat -lntp|grep 3000
tcp6       0      0 :::3000                 :::*                    LISTEN      54729/node          
[root@yapi my-yapi]# ps -ef|grep 54729
root      54729  49546  4 15:52 pts/1    00:00:01 node vendors/server/app.js
root      55083  49546  0 15:53 pts/1    00:00:00 grep --color=auto 54729

瀏覽器訪問:http://192.168.11.11:3000/

image

image

能夠使用pm2進行管理:

image

4、docker化部署yapi

參考文檔:https://www.jianshu.com/p/a97d2efb23c5

官方文檔:https://github.com/YMFE/yapi

1)安裝docker

參考文檔:https://www.cnblogs.com/hujinzhong/p/11934559.html

2)配置aliyun鏡像加速

[root@yapi ~]# vim /etc/docker/daemon.json
{
   "registry-mirrors": ["https://bkajg8ma.mirror.aliyuncs.com"]
}

[root@yapi ~]# systemctl restart docker

3)啓動 MongoDB

docker run -d --name mongo-yapi mongo

4)獲取 Yapi 鏡像,版本信息可在 阿里雲鏡像倉庫 查看

docker pull registry.cn-hangzhou.aliyuncs.com/anoy/yapi

5)初始化 Yapi 數據庫索引及管理員帳號

docker run -it --rm \
  --link mongo-yapi:mongo \
  --entrypoint npm \
  --workdir /api/vendors \
  registry.cn-hangzhou.aliyuncs.com/anoy/yapi \
  run install-server

6)啓動 Yapi 服務

docker run -d \
  --name yapi \
  --link mongo-yapi:mongo \
  --workdir /api/vendors \
  -p 3000:3000 \
  registry.cn-hangzhou.aliyuncs.com/anoy/yapi \
  server/app.js

7)瀏覽器訪問

訪問 http://ip:3000 登陸帳號 admin@admin.com,密碼 ymfe.org

image

image

5、docker-compose安裝yapi

參考文檔:

https://github.com/fjc0k/docker-YApi

相關文章
相關標籤/搜索