How to deploy yapi yourself

Posted by BinaryStar on Fri, 24 Sep 2021 17:33:20 +0200


Recently, we need to use yapi with team members, but some interfaces are not suitable for the yapi already deployed by the company. So we have the idea of deploying a set of yapi ourselves. There are twists and turns in the middle, and finally it is built.

1. Environment

At first, I applied for a free cloud server on a cloud website. Although it is free, it is only five days. If I want to continue to use it, I need to publish advertising posts in each post bar to renew it for five days each time. After three renewals, I forgot the fourth time. When I remembered, I found that the ECS had been deleted. Finally, I simply spent some money to buy a reliable cloud server. The system used is Alibaba Cloud Linux 2, which is perfectly compatible with centos7.
I created a new yapi user to deploy yapi. The command of creating a new user on Linux:

# Add a user, specify a home directory, and create a group with the same user name
useradd -d /home/yapi -U yapi
# Change Password
passwd yapi

2. Deploy nodejs

The nodejs I use is version 12.20.2. At the beginning, I downloaded the latest version 14, but the deployment failed all the time. If I read it online and said that the version needs to be reduced, it was reduced to version 12. After downloading on the official website, unzip it directly.
Official website download address:
Alicloud image address. You can select any version:

# tar -xvf node-v12.20.2-linux-x64.tar.gz after decompression
$ ls
# Setting environment variables
vi ~/.bash_profile
# nodejs writes the following four lines into the file
# Effective after saving
source ~/.bash_profile
# Use node - V; NPM - V check whether the setting is successful
$ node -v

3. Deploy mongodb

mongodb I use is community version 4.0.27.
Download from the official website:

# Unzip after download
$ ls
# Create a directory for storing data and logs
$ cd mongodb-4.0.27/
$ mkdir data logs
# create profile
$ mkdir etc
$ vi mongod.conf
# Profile content
  destination: file
  path: "/home/yapi/tools/mongodb-4.0.27/logs/mongod.log"  #Log storage path
  logAppend: true
  dbPath: "/home/yapi/tools/mongodb-4.0.27/data"  #Data storage path
    enabled: true
  fork: true
  bindIp: localhost
  port: 27017
# start-up
$ cd bin
$ ./mongod --config ../etc/mongod.conf./mongod --config ../etc/mongod.conf

#Check for success
$ ./mongo
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
yapi    0.001GB

4. Deploy yapi

Deploy according to this document.
Official website address:
I have tried both methods. The first one is relatively simple:

# Install Yapi CLI and specify Taobao image, which is fast
$ npm install -g yapi-cli --registry
# After the installation is completed, directly use the command, and you will be prompted to open an address in the browser. After replacing the ip with your own virtual machine address, open it in the browser
$ yapi server
 Open in browser access. Non local server, please replace with the specified domain name or ip address

This is a visual interface for deployment. After the browser opens the address, fill in the corresponding information and start automatic deployment. After successful deployment, enter the yapi installation directory (filled in during deployment).

$ ls
config.json  init.lock  log  vendors
$ cd vendors
# Start the yapi service. After successful deployment, it should start automatically. I forgot this place, but it is not running in the background. After exiting, the service stops. You can start it manually using the following command
$ node server/app.js

The login address, administrator user and password will be prompted. Login address is http://ip:3000 , the user name is filled in by yourself during deployment, and the password is generally by default

5. pm2 background management

yapi services can run in the background using pm2.
pm2 installation is simple

# Installation command
npm install -g pm2

# Run the yapi service and enter the yapi deployment directory
cd vendors/server
pm2 start app.js --name myyapi

# yapi service will be running in the background all the time. Check pm2 status
$ pm2 status
│ id  │ name      │ namespace   │ version │ mode    │ pid      │ uptime │ ↺    │ status    │ cpu      │ mem      │ user     │ watching │
│ 0   │ myyapi    │ default     │ 1.10.1  │ fork    │ 8330     │ 4D     │ 0    │ online    │ 0%       │ 85.3mb   │ yapi     │ disabled │

6. Closure

So far, yapi has been deployed, simple functions can be used, and expensive functions have not been used. However, there is a problem. Obviously, 1.10.1 is deployed. After logging in, the prompt version is 1.10.0. It is recommended to upgrade 1.10.1. I didn't find the reason, but I didn't pay attention if it didn't affect the use. If you have any friends who know, please leave a message.
During deployment, I encountered many problems. At the beginning, the operating system used Alibaba cloud Linux 3 (perfect compatibility with centos8), which may be the compatibility problem of the operating system. I changed several versions of node and mongodb, but they failed. Later, Alibaba Cloud Linux 2 was replaced and the node version was reduced to 12 before the deployment was successful. Compatibility problems kill people.

Topics: Java node.js Linux MongoDB interface