【YAPI】Ubuntu环境下安装

1,152 阅读1分钟

一、安装Node.js

nodejs -v ## 查看是否已经安装
sudo apt-get install -y nodejs

二、安装npm

sudo apt-get -y npm

报错

user@user:/usr/bin$ sudo apt-get install npm
[sudo] password for user: 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
 npm : Depends: node-gyp (>= 0.10.9) but it is not going to be installed
E: Unable to correct problems, you have held broken packages.

缺少环境,安装缺少的包就可以

sudo apt-get install nodejs-dev node-gyp libssl1.0-dev

三、安装mongodb

参考我的另一篇

四、下载和安装YAPI

sudo npm install -g yapi-cli --registry https://registry.npm.taobao.org  ##这个命令会很慢,耐心等候
yapi server 启动
在浏览器打开 http://0.0.0.0:9090 访问。非本地服务器,请将 0.0.0.0 替换成指定的域名或ip 
events.js:183
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE :::9890
    at Object._errnoException (util.js:1022:11)
    at _exceptionWithHostPort (util.js:1044:20)
    at Server.setupListenHandle [as _listen2] (net.js:1367:14)
    at listenInCluster (net.js:1408:12)
    at Server.listen (net.js:1492:7)
    at Function.serverListen [as listen] (/usr/local/lib/node_modules/yapi-cli/node_modules/express-ws/lib/index.js:42:40)
    at Object.run [as handler] (/usr/local/lib/node_modules/yapi-cli/src/commands/server.js:92:9)
    at Object.runCommand (/usr/local/lib/node_modules/yapi-cli/node_modules/yargs/lib/command.js:226:22)
    at Object.parseArgs [as _parseArgs] (/usr/local/lib/node_modules/yapi-cli/node_modules/yargs/yargs.js:970:30)
    at Function.get [as argv] (/usr/local/lib/node_modules/yapi-cli/node_modules/yargs/yargs.js:912:21)
    at Object.<anonymous> (/usr/local/lib/node_modules/yapi-cli/src/index.js:19:9)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)

如果提示端口占用,修改端口再次启动,浏览器打开 http://127.0.0.1:9090(默认端口),进入可视化配置界面开始部署

注意:这个过程超级慢,而且还发生警告,就算降低YAPI的版本,一样会提示警告,请耐心等候,我等了大概2个小时,可能是npm源的问题,把源设置成taobao的再部署(没事过)

进入安装目录下的修改配置文件(开始部署时配置的部署路径)

cd /usr/local/lib/node_modules/yapi-cli/my-yapi
vi config.json

修改端口、数据库等

{
   "port": "3000",
   "adminAccount": "1138564284@qq.com",
   "db": {
      "servername": "192.168.0.51",
      "DATABASE": "yapi",
      "port": "27017",
      "user": "yapi",
      "pass": "user123456"
   },
   "mail": {
      "enable": false,
      "host": "smtp.163.com",
      "port": 465,
      "from": "***@163.com",
      "auth": {
         "user": "***@163.com",
         "pass": "*****"
      }
   }
}

启动,如果提示端口占用,修改配置文件后再次启动

cd  /usr/local/lib/node_modules/yapi-cli/my-yapi  ## 配置的部署路径
node vendors/server/app.js

五、下载和安装pm2

使用node启动时,如果关闭SSH会退出进程,所以需要后台启动,使用pm2实现后台启动

sudo npm install -g pm2 
sudo pm2 start 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=/home/user/.pm2
[PM2] PM2 Successfully daemonized
[PM2] Starting /usr/local/lib/node_modules/yapi-cli/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.6.0   │ fork    │ 14504    │ 0s     │ 0    │ online    │ 0%       │ 15.8mb   │ root     │ disabled │
└─────┴────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘

相关命令

pm2 restart appId  ##重启服务
pm2 stop app.js   ##停止服务