The first deployment process of Xiaobai’s MidwayJS project

Some time ago, I refactored the Paul API project with MidwayJS and React , the new one is called Paul API Next . After testing on the development machine, it is found that the speed of each request can be increased by about 20ms , which is slightly better than the PHP program running with cgi .

The most important thing is that I also use the framework for the NodeJS version, while the PHP version is natively developed. In terms of development efficiency, the former will be better. You can also use the process persistence feature to better extend the future functional design of the program and increase more training opportunities.

You ask me why not open source? In fact, I have also thought about this problem, mainly because NetEase has gone crazy with a round of open source projects on GitHub before. My API project essentially represents the functions of many ready-made interfaces, and there is a certain “copyright risk”. If these functions are removed and open sourced, the meaning will be reduced by more than half. In addition, this API may actually be called more by my small site, so I chose closed source maintenance in the end.

Considering that the actual resources of the server are relatively small, the current program does not use an automated deployment solution similar to Jenkins, nor does it use Docker packaged images to run in isolation, or manual deployment.

In the early days, it was easy to rectify on the domestic Windows development machine, and I felt that I could almost throw it online and play. So I started tossing around today. Although the steps are a bit complicated, it is definitely safer to store the database in the real environment of the server than in the container.

server environment

OS: Ubuntu 20.04 LTS
Memory: 971MiB

Database: MariaDB
Cache: Redis
Operating environment: NodeJS V16 (NVM) / Nginx

The original server has already installed NVM and version 16 of NodeJS, and then you need to install yarn and pm2 to achieve better package management and process management.

 npm install --global yarn npm install --global pm2

Then you can use git to pull down the code repository, install dependencies, create + modify database configuration and deploy.

 git clone xxxx . yarn

There is nothing to say about the steps to create a new database. Use database management software (phpMyAdmin, Navicat, etc.) to create an independent account and a database with the same name , and finally use vim to modify the project configuration.

Initialize the Prisma database

I have modified the .env project configuration earlier, and Prisma’s schema file can directly reference the configuration items in it. Reference documentation

 datasource db { provider = "mysql" url = env("DB_URL") }

Next we have to connect and initialize the database tables and structures on the new environment. For the characteristics of the generate command, please refer to the documentation . It will be executed by default when you develop locally, but not necessarily in the server environment.

 npx prisma generate // 生成类型文件

For normal operation, we need to generate the type file of the project in the new environment and then publish it. Previously, I found that I couldn’t connect, actually because the password of the MariaDB database needs to meet certain rules (if it is a random password, special characters may explode, and the following prompt will appear, just change it to numbers or uppercase and lowercase letters)

 Error: P1013: The provided database string is invalid. invalid port number in database URL. Please refer to the documentation in https://www.prisma.io/docs/reference/database-reference/connection-urls for constructing a correct connection string. In some cases, certain characters must be escaped. Please check the string for any illegal characters.

If the defined connection address is localhost , it may not be able to connect (it should be that localhost will not point to this machine under some Linux), set the host name corresponding to the database account to 127.0.0.1 , and then modify .env configuration file, it should be fine .

The data is generated successfully, and the following information is displayed:

 ✔ Generated Prisma Client (3.12.0 | library) to ./node_modules/@prisma/client in 1.02s You can now start using Prisma Client in your code. Reference: https://pris.ly/d/client

Then it is to execute the push and seed instructions. I have tried to execute the pull instruction, which should not be needed at present.

 npx prisma db pull // 在数据库为空的情况下,Pull 不来(应该是用于同步数据库实际情况给CLI) npx prisma db push // 发布到实际数据库环境npx prisma db seed // 执行播种流程(预设数据库的数据,需要编写seed.ts 文件)

During the period, I found that my server environment is still quite strange. I don’t know why executing any command under npx prompts insufficient permissions. Referring to the problem on the Internet, I used which node to locate the actual directory of NodeJS. I saw that many directories are www group, I used chown -R root:root . The commands are all changed to the current login user root , but they are still invalid. Using sudo npx also prompts that the command cannot be found, and I don’t know what the reason is.

I ended up doing this by using the scripts on package.json for directive substitution. It’s just stupid, you have to preset all the commands you need to use it before you can use it, which is not flexible enough. This kind of operation and maintenance problem is really difficult for me.

 // package.json "scripts": { "start": "hooks start", "dev": "hooks dev", "build": "hooks build", "dbgen": "prisma generate", "dbpull": "prisma db pull", "dbpush": "prisma db push", "dbseed": "prisma db seed" }

Finally, execute yarn build to deploy a set of production code, and then it is ready to run persistently with pm2 .

Running processes with PM2

Referring to the relevant instructions of the official documentation , I used the simplest command to run a process. The correct way to eat should be to write a pm2.json file, and then fill in the corresponding command.

 pm2 start "yarn start"

The project runs on port 3000 by default. Since it is the first NodeJS project of the server, I did not specify another port. If you need to specify a port, it is similar to the development time. Just give a parameter such as --port 5000 .

You can use the wget tool to obtain the return data of an interface to ensure that the service is running normally.

 wget http://127.0.0.1:3000/api/acgm

Modify Nginx configuration

Finally, modify the Nginx virtual host configuration file and publish the project to the external network.

I use the OneInstack integrated environment. Most of the default virtual host configurations are implemented for PHP. We need to remove the original Nginx configuration items and use the reverse proxy function instead. That is, use Nginx as a springboard and jump to the service of NodeJS.

Note: No other location configuration can be left, otherwise it may cause access failures. After all, we have already linked the whole site to the reverse proxy to the NodeJS process. If the rules of Nginx are used to match, it will be regarded as an invalid path and an error will occur.

 server { ... # 不能有其他location 规则,我们已经把这个站的请求全权交给了NodeJS 了location / { proxy_pass http://127.0.0.1:3000; proxy_set_header Host 127.0.0.1; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header REMOTE-HOST $remote_addr; add_header Cache-Control no-cache; add_header X-Cache $upstream_cache_status; proxy_set_header Accept-Encoding ""; sub_filter_once off; } # 不能使用,否则就炸了#location ~ .*\.(wma|wmv|asf|mp3|mmf|zip|rar|jpg|gif|png|swf|flv|mp4)$ { #valid_referers none blocked *.paugram.com api-next.paugram.com; #if ($invalid_referer) { #return 403; #} #} }

Use the command service nginx restart to restart the Nginx process, and use the browser to verify. If there are no other problems, the service runs successfully!

Make complaints

After talking for a long time, the overall feeling is not too technical, but I think it is quite meaningful to make such a record. Maybe someone like me has been stuck for a long time because of a small problem? And in the future, similar scenarios can be solved more quickly.

But this problem that npx can’t be used, is there any big guy who is willing to give me some advice for this little white ==

This article is reprinted from: https://paugram.com/tech/first-midwayjs-app-deploy.html
This site is for inclusion only, and the copyright belongs to the original author.

Leave a Comment