centos7 Yapi环境搭建

环境准备

  • 操作系统:centos 7
  • 环境要求:
    • nodejs>7.6
    • mongodb>2.6
    • git

安装nodejs

  1. 获取资源(部署nodejs尽可能选择偶数版本,因为偶数版本官方有较长的维护时间,故这次选择8.x。)
    curl --silent --location https://rpm.nodesource.com/setup_8.x | sudo bash -

  2. 安装
    yum install -y nodejs

  3. 查看版本
    node -v

  4. 查看npm版本
    npm -v

安装mongodb

  1. 更新yum源,非必须但是更新一下无害
    yum -y update
  2. 添加mongodb源文件,在/etc/yum.repos.d 创建一个 mongodb-org.repo 文件
    touch /etc/yum.repos.d/mongodb-org.repo
  3. 编辑mongodb-org.repo文件
    vim /etc/yum.repos.d/mongodb-org.repo
  4. 添加文件内容
[mongodb-org]
name=MongoDB 
Repositorybaseurl=http://mirrors.aliyun.com/mongodb/yum/redhat/7Server/mongodb-org/3.2/x86_64/
gpgcheck=0
enabled=1
  1. 安装mongodb
    yum install -y mongodb-org
  2. 启动mongodb
    service mongod start
  3. 设置开机启动
    chkconfig mongod on
  4. 打开moMongoDB(可忽略)
    /bin/mongo
  5. 配置远程访问
  • 修改mongod.conf配置文件
    vim /etc/mongod.conf
  • 注释 bindIp: 127.0.0.1
    #bindIp: 127.0.0.1
  • 重启mongod
    service mongod restart

安装git

  • 官方推荐,在内网部署的时候安装git,可以根据自己所需安装
    yum -y install git

搭建YApi环境

搭建YApi

  1. 准备环境搭建完成后,开始搭建YApi,安装命令
npm install -g yapi-cli --registry https://registry.npm.taobao.org
yapi server
  1. 执行 yapi server 启动可视化部署程序,浏览器打开提示窗口上的地址,非本地服务器,将0.0.0.0替换指定的域名或IP,进入部署页面。
  1. 根据项目实际需要可以修改部署信息,点击【开始部署】,完成部署任务
  • 部署成功

4.根据部署日志截图上的提示信息,启动服务
cd /my-yapi
启动服务:node vendors/server/app.js

浏览器打开部署日志上的访问地址http://127.0.0.1:3000就可以访问搭建的YApi工具了(非本地服务器,将127.0.0.1替换指定的域名或IP),此时YApi本地环境搭建完成

如果是阿里云ecs服务器,端口需要在安全组里开放,不然访问不了

5.pm2方式管理进程

要保证后台保持进程,需要安装pm2。
全局安装:
npm install -g pm2

启动:
cd vendors
pm2 start server/app.js --watch

重启:

pm2 restart server/app.js

停止:

可以先通过pm2 list获取应用的名字(--name指定的)或者进程id。

pm2 stop app_name|app_id

问题:
错误

执行
npm install -g pm2

报错:


[daoshu@Demo-RiskInsight-225 vendors]$ npm install -g pm2
npm WARN checkPermissions Missing write access to /data/nodejs-12.18.3/node-v12.18.3-linux-x64/lib/node_modules/pm2
npm WARN checkPermissions Missing write access to /data/nodejs-12.18.3/node-v12.18.3-linux-x64/lib/node_modules/pm2/node_modules
npm WARN checkPermissions Missing write access to /data/nodejs-12.18.3/node-v12.18.3-linux-x64/lib/node_modules
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@~2.3.1 (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"})

npm ERR! code EACCES
npm ERR! syscall access
npm ERR! path /data/nodejs-12.18.3/node-v12.18.3-linux-x64/lib/node_modules/pm2
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, access '/data/nodejs-12.18.3/node-v12.18.3-linux-x64/lib/node_modules/pm2'
npm ERR!  [Error: EACCES: permission denied, access '/data/nodejs-12.18.3/node-v12.18.3-linux-x64/lib/node_modules/pm2'] {
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'access',
npm ERR!   path: '/data/nodejs-12.18.3/node-v12.18.3-linux-x64/lib/node_modules/pm2'
npm ERR! }
npm ERR!
npm ERR! The operation was rejected by your operating system.
npm ERR! It is likely you do not have the permissions to access this file as the current user
npm ERR!
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/daoshu/.npm/_logs/2021-05-10T02_24_11_554Z-debug.log
[daoshu@Demo-RiskInsight-225 vendors]$

解决:

Linux 下安装pm2后找不到pm2命令解决方法

在安装提示中可以看到pm2安装位置,此时我们只要使用以下命令将pm2放入系统路径下就可以了:

sudo ln -s /data/nodejs-12.18.3/node-v12.18.3-linux-x64/bin/pm2 /usr/local/bin/

参考:
pm2使用参考文档:
http://imweb.io/topic/57c8cbb27f226f687b365636
Yapi环境搭建(史上最详细步骤)
在centos7安装nodejs并升级nodejs到最新版本
CentOS 6.9/7通过yum安装指定版本的Node.js

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 前言 Yapi是去哪儿网开源的一款接口管理工具。接口的文档的管理是一件比较痛苦的事情,后端开发不得不使用很长的时间...
    扑棱扑棱的小菜鸟阅读 44,059评论 8 22
  • 前言 Yapi是去哪儿网开源的一款接口管理工具。接口的文档的管理是一件比较痛苦的事情,后端开发不得不使用很长的时间...
    _52Hertz阅读 10,086评论 1 0
  • 安装node 安装mongodb 添加mongodb源文件touch /etc/yum.repos.d/mongo...
    蟠龙有悔阅读 5,431评论 1 1
  • 搭建线上环境需要做什么?1、购买域名。2、购买服务器(就是一台带外网ip的虚拟电脑)。3、配置服务器应用环境(用户...
    LoveBugs_King阅读 6,602评论 0 2
  • Yapi简介 Yapi 由 YMFE 开源,旨在为开发、产品、测试人员提供更优雅的接口管理服务,可以帮助开发者轻松...
    LittleJessy阅读 5,480评论 2 9