Linux centos7部署Yapi接口管理平台详细步骤


Yapi是一个接口管理平台,支持本地部署.如果你们公司有自己的接口需要供内人员使用或者维护,可以在自己的服务器上部署下YAPI接口管理平台.下面是本人测试使用部署的过程,基本没有特殊配置.

该项目主要需要三个模块


1.node.js
2.mongondb数据库
3.Yapi源码


第一步:安装node.js

(1)下载node.js

nodejs下载地址:https://nodejs.org/en/download/

node.下载
主要命令:

yum install -y wget   安装wget用来下载数据库和nodejs一级yapi源码
yum install gcc-c++  安装nodejs需要的编译环境

第二步 安装mongondb数据库

(1)下载mongondb

mongondb下载地址:https://www.mongodb.com/download-center/community
可以下载到本地,也可以在服务器上是用wget /curl命令下载.
在这里插入图片描述

安装nodejs和mongodb相对简单,出现问题按照提示基本都可以解决,mongondb若没有mongodb.conf配置文件,需要新建一个放进去.主要配置信息如下
dbpath = /data/db #数据文件存放目录
logpath = /logs/mongodb.log #日志文件存放目录
port = 27017 #端口
fork = true #以守护程序的方式启用,即在后台运行
noauth = true # 不进行安全验证
#auth=true #需要认证。如果放开注释,就必须创建MongoDB的账号,使用账号与密码才可远程访问,第一次安装建议注释
bind_ip=0.0.0.0 #允许远程访问,或者直接注释,127.0.0.1是只允许本地访问`

第三步 部署Yapi

1.安装源文件

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

2.配置yapi

ln -s ~/node-v10.13.0-linux-x64/bin/yapi /usr/bin/yapi

3.启动yapi

yapi server

4.服务启动后我们需要将yapi配置部署下,因为之前并没有配置对应的数据库及端口,所以配置下才可以访问记存储数据.

访问http:// 部署YApi服务器IP:9090

访问后出现如下界面说明正确的
在这里插入图片描述
内容填写完后点击开始部署,记得记住管理员邮箱,这个是用来登录的.,部署完成后界面上会提示登录的邮箱,密码及登录地址.一般情况下登录密码都是ymfe.org
,登录地址是:http://服务器ip:3000
在这里插入图片描述最终出现上边的界面说明大功告成.就可以登录进去管理接口了.

执行yapi server,打开部署界面后,填入信息,点击安装部署报错(getaddrinfo ENOTFOUND yapi.demo.qunar.com)

报错如下:

在浏览器打开 http://0.0.0.0:9090 访问。非本地服务器,请将 0.0.0.0 替换成指定的域名或ip
(node:32272) Warning: Accessing non-existent property 'cat' of module exports inside circular dependency
(Use `node --trace-warnings ...` to show where the warning was created)
(node:32272) Warning: Accessing non-existent property 'cd' of module exports inside circular dependency
(node:32272) Warning: Accessing non-existent property 'chmod' of module exports inside circular dependency
(node:32272) Warning: Accessing non-existent property 'cp' of module exports inside circular dependency
(node:32272) Warning: Accessing non-existent property 'dirs' of module exports inside circular dependency
(node:32272) Warning: Accessing non-existent property 'pushd' of module exports inside circular dependency
(node:32272) Warning: Accessing non-existent property 'popd' of module exports inside circular dependency
(node:32272) Warning: Accessing non-existent property 'echo' of module exports inside circular dependency
(node:32272) Warning: Accessing non-existent property 'toEnd' of module exports inside circular dependency
(node:32272) Warning: Accessing non-existent property 'touch' of module exports inside circular dependency
(node:32272) Warning: Accessing non-existent property 'uniq' of module exports inside circular dependency
(node:32272) Warning: Accessing non-existent property 'which' of module exports inside circular dependency
node:internal/process/promises:246
          triggerUncaughtException(err, true /* fromPromise */);
<ref *1> Error: getaddrinfo ENOTFOUND yapi.demo.qunar.com
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:71:26) {
  errno: -3008,
  code: 'ENOTFOUND',
  syscall: 'getaddrinfo',
  hostname: 'yapi.demo.qunar.com',
  config: {
    adapter: [Function: httpAdapter],
    transformRequest: { '0': [Function: transformRequest] },
    transformResponse: { '0': [Function: transformResponse] },

关于此问题找了网上说了很多但是没有解决的,因为我的node和npm用的都是最新的版本,考虑到可能是版本太高,于是将版本从V16.6.0降低到V11.15.0后重新尝试,解决该问题.

Ubuntu 开启端口

lnt@lnt:~/yapi/nodejs11/bin$ sudo ufw allow 8080  #开启端口
lnt@lnt:~/yapi/nodejs11/bin$ sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
22                         ALLOW       Anywhere
lnt@lnt:~/yapi/nodejs11/bin$ sudo ufw  delete  allow 9090#关闭端口
Rule deleted
Rule deleted (v6)
lnt@lnt:~/yapi/nodejs11/bin$ sudo ufw  reload
Firewall reloaded
lnt@lnt:~/yapi/nodejs11/bin$ sudo ufw  status
Status: active

To                         Action      From
--                         ------      ----
22                         ALLOW       Anywhere
80                         ALLOW       Anywhere

lnt@lnt:~/yapi/nodejs11/bin$

服务器重启后,重新启动yapi

有次服务器重启,然后yapi相关的服务都没有了,需要重启下,走了一些弯路.
1.yapi重启首先要启动mongodb及对应的服务.找到你的mongo安装目录中的bin目录,通过./mongod -f mongodb.conf,启动mongondb,然后可以适用./mongo看是否可以进去到mongon中.

[root@9z bin]# cd /root/mongodb-linux-4.2.1/bin/
[root@9z bin]# ll
total 282152
-rwxr-xr-x 1 root root 10100811 Oct 16  2019 bsondump
-rwxr-xr-x 1 root root     7694 Oct 16  2019 install_compass
-rwxr-xr-x 1 root root 47534944 Oct 16  2019 mongo
-rwxr-xr-x 1 root root 71584856 Oct 16  2019 mongod
-rw-r--r-- 1 root root      239 Nov  2  2019 mongodb.conf
-rwxr-xr-x 1 root root 14765905 Oct 16  2019 mongodump
-rwxr-xr-x 1 root root 14511931 Oct 16  2019 mongoexport
-rwxr-xr-x 1 root root 14473296 Oct 16  2019 mongofiles
-rwxr-xr-x 1 root root 14690724 Oct 16  2019 mongoimport
-rwxr-xr-x 1 root root 18154584 Oct 16  2019 mongoreplay
-rwxr-xr-x 1 root root 15088133 Oct 16  2019 mongorestore
-rwxr-xr-x 1 root root 39806704 Oct 16  2019 mongos
-rwxr-xr-x 1 root root 14258080 Oct 16  2019 mongostat
-rwxr-xr-x 1 root root 13918118 Oct 16  2019 mongotop
drwxr-xr-x 4 root root     4096 Nov  2  2019 my-yapi
[root@9z bin]# ./mongod -f mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 5451
child process started successfully, parent exiting

[root@9z bin]# ./mongo
MongoDB shell version v4.2.1
connecting to: mongodb://127.0.0.1:27017/?
---
---
---
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---

> 
#出现光标可以输入mongon命令,说明是进入到mongon中了,mongon服务已经启动.

2.找到你的yapi启动目录.搜索下app.js文件,带有vendors/server/app.js这个的就是你需要找到的.

[root@9z my-yapi]# find / -name app.js
/root/node-v12.13.0-linux-x64/lib/node_modules/yapi-cli/node_modules/mongoose/examples/express/connection-sharing/app.js
/root/mongodb-linux-4.2.1/bin/my-yapi/vendors/server/app.js
/root/mongodb-linux-4.2.1/bin/my-yapi/vendors/node_modules/mongoose/examples/express/connection-sharing/app.js
/vdb/iothtml/taolin/taolin_20190620/classic/app.js
[root@izuf69lmwvnm1jclna8l69z my-yapi]#

3.进入到app.js文件目录,node vendors/server/app.js &命令,以守护进程启动yapi.有如下提示说明启动成功.

[root@9z my-yapi]# node vendors/server/app.js &
[1] 6825
[root@9z my-yapi]# log: -------------------------------------swaggerSyncUtils constructor-------                           ----------------------------------------
log: 服务已启动,请打开下面链接访问:
http://127.0.0.1:3000/
log: mongodb load success...
(node:6825) [DEP0066] DeprecationWarning: OutgoingMessage.prototype._headers is deprecated

Centos8 防火墙开放端口

查看firewall防火墙状态
firewall-cmd --state

查看防火墙某个端口是否开放
firewall-cmd --query-port=80/tcp

开放防火墙端口80
firewall-cmd --zone=public --add-port=80/tcp --permanent

关闭80端口
firewall-cmd --zone=public --remove-port=80/tcp --permanent  

配置立即生效
firewall-cmd --reload 
查看防火墙状态
systemctl status firewalld

关闭防火墙
systemctl stop firewalld

打开防火墙
systemctl start firewalld

开放一段端口
firewall-cmd --zone=public --add-port=8121-8124/tcp --permanent

查看开放的端口列表
firewall-cmd --zone=public --list-ports

yapi后台运行

方式1:
关闭终端界面后,yapi也无法访问了,在网上找到个处理方法:

#安装screen

 yum install screen -y

#新建一个名为yapi的进程

screen -S yapi

创建完进程后,重新找到 my-yapi的目录,重新开启yapi

node vendors/server/app.js

方式2:
1.安装PM2
sudo npm i -g pm2

[root@iZuf6pq7hZ ~]# npm i -g pm2
npm WARN deprecated uuid@3.4.0: Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.
/usr/local/bin/pm2 -> /usr/local/lib/node_modules/pm2/bin/pm2
/usr/local/bin/pm2-dev -> /usr/local/lib/node_modules/pm2/bin/pm2-dev
/usr/local/bin/pm2-docker -> /usr/local/lib/node_modules/pm2/bin/pm2-docker
/usr/local/bin/pm2-runtime -> /usr/local/lib/node_modules/pm2/bin/pm2-runtime
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@~2.3.2 (node_modules/pm2/node_modules/chokidar/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.3.2: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

+ pm2@5.1.0
added 180 packages from 203 contributors in 163.694s
[root@iZujpq7hZ ~]# pm2 -v

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

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


2.修改PM2启动路径(如果路径对则此步骤跳过)
ln12t@$ sudo ln -s /home/lnt/yapi/nodejs11/bin/pm2 /usr/local/bin/
3.使用PM2启动yapi

ln12t@ln12t:~/yapi/nodejs11/bin$ pm2 -v
5.1.0
ln12t@ln12t$ pm2 start /home/lnt/yapi/my-yapi/vendors/server/app.js
[PM2] Starting /home/lnt/yapi/my-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.9.2   │ fork    │ 4153     │ 0s     │ 0    │ online    │ 0%       │ 24.8mb   │ lnt      │ disabled │
└─────┴────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘

yapi接口运行插件安装

使用git下载cross-request源码,下载后在chrom浏览器中加载已解压扩展程序,把该文件夹加载到扩展程序中即可.

git clone https://github.com/YMFE/cross-request

YAPI部署后禁止注册

在开放注册的情况下,攻击者可以通过注册登录并注入恶意脚本从而执行任意命令,控制服务器,由于内网部署yapi,只是自己在用,所以禁止注册.
在 config.json 添加 closeRegister:true 配置项,和port同一级,就可以禁止用户注册 yapi 平台,修改完成后,请重启 yapi 服务器即可.

{
   "port": "*****",
   "adminAccount": "******",
   "closeRegister":true,
   "db": {
      "servername": "127.0.0.1",
      "DATABASE": "***",
      "port": "***"
   },
   ......
}

禁止注册后添加用户

禁止注册后我们如何添加用户呢,因为yapi平台上没有操作界面,所以我们只能通过操作数据库的方式来添加数据,这个需要用到mongo数据库的一些基本命令

[root@i9z bin]# ./mongo 127.0.0.1:27017     //找到mongo安装bin目录,使用mongo命令连接数据库.,当然有用户和密码的需要用户名和密码登录

[root@i9z bin]# ./mongo --host 127.0.0.1 -u "myUserAdmin" --authenticationDatabase "admin" -p'abc123'

//执行后出现如下界面说明连接成功,可以开始使用了
To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
>  //尖括号后边输入mongodb命令

------------------------------下边是一些基本命令-------------------------------
> show dbs   //展示有哪些数据库类似mysql的show database
admin   0.000GB
config  0.000GB
local   0.000GB
yapi    0.151GB
> use yapi   //表示使用哪个数据库,和mysql用法一样
switched to db yapi
> show collections   //展示该数据库的表,类似mysql的show tables
> db.user.find();  //展示user表中的所有数据,类似mysql的select * from user;
> db.user.find();  //展示user表中的所有数据,类似mysql的select * from user;
>db.user.insert({"study":"*****", "type": "site", "username": "测试","password":"******","email":"ceshi@ceshi.com","passsalt":"******","role":"member","add_time":"1572687240","up_time":"1602233254","__v":"0"});   //user表中插入一条数据

FAQ

YAPI源码仓库:https://github.com/YMFE/yapi
常见问题:https://github.com/YMFE/yapi/issues
教程/文档地址:https://hellosean1025.github.io/yapi/
问题社区:https://github.com/YMFE/yapi/pulls

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值