CentOS7 安裝 YAPI

CentOS7 安裝 YAPI

官網安裝教程 : https://hellosean1025.github.io/yapi/devops/index.html

環境要求

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

環境準備

  • nodejs12
# 清除yum命令緩存

	sudo yum clean all

# yum卸載低版本nodejs

	yum remove nodejs npm -y

# # 獲取資源,安裝高版本nodejs

	curl -sL https://rpm.nodesource.com/setup_8.x | bash -

	sudo yum install -y nodejs

# 驗證版本

    [root@iz2ze9d7x8qiddthmdvrv6z local]# node -v
    v12.13.1
    [root@iz2ze9d7x8qiddthmdvrv6z local]# npm -v
    6.12.1
  • git
# 驗證版本
	git --version

# 安裝
	yum install -y git
  • mongodb4.0
# 配置MongoDB的yum源,vi /etc/yum.repos.d/mongodb-org-4.0.repo,添加內容:

    [mongodb-org-4.0]
    name=MongoDB Repository
    baseurl=https://repo.mongodb.org/yum/redhat/7Server/mongodb-org/4.0/x86_64/
    gpgcheck=0
    enabled=1
    gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc

# 安裝MongoDB

	yum install -y mongodb-org
	
# 安裝完畢後,看看都裝到哪裏去了

	rpm -ql mongodb-org-server
	
# 如有必要,修改配置文件,vi /etc/mongod.conf

# 官方教程有提到selinux對mongodb會產生負面影響,故選擇禁用。vim /etc/selinux/config

	將SELINUX=enforcing改爲SELINUX=disabled,wq保存。

# 啓動mongodb

	systemctl enable mongod
    systemctl start mongod
    systemctl status mongod

YAPI安裝

  • npm install -g yapi-cli --registry https://registry.npm.taobao.org
[root@iz2ze9d7x8qiddthmdvrv6z yum.repos.d]# npm install -g yapi-cli --registry https://registry.npm.taobao.org
/usr/bin/yapi -> /usr/lib/node_modules/yapi-cli/bin/yapi-cli
/usr/bin/yapi-cli -> /usr/lib/node_modules/yapi-cli/bin/yapi-cli
+ [email protected]
added 266 packages from 125 contributors in 14.992s

  • yapi server

安裝過程大約兩分鐘,安裝成功後,前臺頁面和後臺服務器都會有提示!

[root@iz2ze9d7x8qiddthmdvrv6z yum.repos.d]# yapi server
在瀏覽器打開 http://0.0.0.0:9090 訪問。非本地服務器,請將 0.0.0.0 替換成指定的域名或ip
當前安裝版本: 1.8.5
連接數據庫成功!
開始下載平臺文件壓縮包...
http://registry.npm.taobao.org/yapi-vendor/download/yapi-vendor-1.8.5.tgz
部署文件完成,正在安裝依賴庫...
 npm WARN deprecated [email protected]: In 6.x, the babel package has been deprecated in favor of babel-cli. Check https://opencollective.com/babel to support the Babel maintainers

......
......

> [email protected] postinstall /usr/local/yapi/vendors/node_modules/core-js
> node postinstall || echo "ignore"

......
......
 added 338 packages from 330 contributors in 50.65s

依賴庫安裝完成,正在初始化數據庫mongodb...

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

 log: mongodb load success...

 初始化管理員賬號成功,賬號名:"[email protected]",密碼:"ymfe.org"

部署成功,請切換到部署目錄,輸入: "node vendors/server/app.js" 指令啓動服務器, 然後在瀏覽器打開 http://127.0.0.1:3000 訪問

YAPI1
yapi2
yapi3

  • 這裏我們不急着根據提示進行啓動,有些參數我們可以通過修改配置達到。
 # 修改config.json ,vim /root/my-yapi/config.json
 
 {
  "port": "80",
  "adminAccount": "[email protected]",
  "db": {
       "servername": "127.0.0.1",
       "DATABASE": "yapi",
       "port": "27017"
   },
  "mail": {
       "enable": true,
       "host": "smtp.163.com",
       "port": 465,
       "from": "可用於發送郵件的163郵箱",
       "auth": {
           "user": "163郵箱",
           "pass": "163郵箱對應的密碼或授權碼"
       }
  },
  "ldapLogin": {
      "enable": true,
      "server": "ldap://你的LDAP服務器IP:389",
      "baseDn": "CN=Admin,CN=Users,DC=test,DC=com",
      "bindPassword": "123456",
      "searchDn": "OU=UserContainer,DC=test,DC=com",
      "searchStandard": "mail"
   },
  "closeRegister":true
}

1. LDAP配置項(ldapLogin)

enable 表示是否配置 LDAP 登錄,true(支持 LDAP登錄 )/false(不支持LDAP登錄);
server LDAP 服務器地址,前面需要加上 ldap:// 前綴,也可以是 ldaps:// 表示是通過 SSL 連接;
baseDn LDAP 服務器的登錄用戶名,必須是從根結點到用戶節點的全路徑;
bindPassword 登錄該 LDAP 服務器的密碼;
searchDn 查詢用戶數據的路徑,類似數據庫中的一張表的地址,注意這裏也必須是全路徑;
searchStandard 查詢條件,這裏是 mail 表示查詢用戶信息是通過郵箱信息來查詢的。注意,該字段信息與LDAP數據庫存儲數據的字段相對應,如果如果存儲用戶郵箱信息的字段是 email, 這裏就需要修改成 email.

2. 註冊配置項(closeRegister)

值爲true,表示禁止用戶註冊 
值爲false(缺損),表示允許註冊
  • cd /usr/local/yapi/ && node vendors/server/app.js
[root@iz2ze9d7x8qiddthmdvrv6z yapi]# node vendors/server/app.js
log: -------------------------------------swaggerSyncUtils constructor-----------------------------------------------
log: 服務已啓動,請打開下面鏈接訪問:
http://127.0.0.1:3000/
log: mongodb load success...
  • 瀏覽器訪問:http://127.0.0.1:3000/,安裝成功

yapi4
yapi5

  • 這樣存在一個問題,當shell窗口關閉時,服務也關閉了,解決方法:
# 安裝pm2
	
	npm install -g pm2 --registry=https://registry.npm.taobao.org

提示

[root@iz2ze9d7x8qiddthmdvrv6z yapi]# npm install -g pm2 --registry=https://registry.npm.taobao.org
/usr/bin/pm2 -> /usr/lib/node_modules/pm2/bin/pm2
/usr/bin/pm2-dev -> /usr/lib/node_modules/pm2/bin/pm2-dev
/usr/bin/pm2-docker -> /usr/lib/node_modules/pm2/bin/pm2-docker
/usr/bin/pm2-runtime -> /usr/lib/node_modules/pm2/bin/pm2-runtime
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules/pm2/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

+ [email protected]
added 206 packages from 202 contributors in 12.917s

# 後臺啓動,關閉

    我的yapi是安裝在/usr/local/yapi/
    啓動執行: pm2 start /usr/local/yapi/vendors/server/app.js
    關閉執行: pm2 stop /usr/local/yapi/vendors/server/app.js


[root@iz2ze9d7x8qiddthmdvrv6z yapi]# pm2 start /usr/local/yapi/vendors/server/app.js

                        -------------

__/\\\\\\\\\\\\\____/\\\\____________/\\\\____/\\\\\\\\\_____
 _\/\\\/////////\\\_\/\\\\\\________/\\\\\\__/\\\///////\\\___
  _\/\\\_______\/\\\_\/\\\//\\\____/\\\//\\\_\///______\//\\\__
   _\/\\\\\\\\\\\\\/__\/\\\\///\\\/\\\/_\/\\\___________/\\\/___
    _\/\\\/////////____\/\\\__\///\\\/___\/\\\________/\\\//_____
     _\/\\\_____________\/\\\____\///_____\/\\\_____/\\\//________
      _\/\\\_____________\/\\\_____________\/\\\___/\\\/___________
       _\/\\\_____________\/\\\_____________\/\\\__/\\\\\\\\\\\\\\\_
        _\///______________\///______________\///__\///////////////__


                          Runtime Edition

        PM2 is a Production Process Manager for Node.js applications
                     with a built-in Load Balancer.

                Start and Daemonize any application:
                $ pm2 start app.js

                Load Balance 4 instances of api.js:
                $ pm2 start api.js -i 4

                Monitor in production:
                $ pm2 monitor

                Make pm2 auto-boot at server restart:
                $ pm2 startup

                To go further checkout:
                http://pm2.io/


                        -------------

[PM2] Spawning PM2 daemon with pm2_home=/root/.pm2
[PM2] PM2 Successfully daemonized
[PM2] Starting /usr/local/yapi/vendors/server/app.js in fork_mode (1 instance)
[PM2] Done.
┌─────┬──────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id  │ name │ namespace   │ version │ mode    │ pid      │ uptime │ ↺    │ status    │ cpu      │ mem      │ user     │ watching │
├─────┼──────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0   │ app  │ default     │ 1.8.5   │ fork    │ 19180    │ 0s     │ 0    │ online    │ 0%       │ 11.0mb   │ root     │ disabled │
└─────┴──────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘

  • 驗證進程:ps aux | grep yapi

YAPI升級

YApi的升級非常容易,且不會影響用戶的數據,只會更新vendors目錄

	cd /usr/local/yapi/ && yapi ls

# 更新到最新版本

	yapi update
 
# yapi也支持升級到指定的版本

	yapi update -v {Version}

備註

  • 每次服務器重啓,mongodb和yapi都需要重啓
  • 如果安裝失敗,可能是目錄權限問題,本文基於centos7.4 root用戶安裝,安裝過程未遇到明顯問題。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章