Home  >  Article  >  Topics  >  Pagoda panel deploys egg+web project (with detailed steps)

Pagoda panel deploys egg+web project (with detailed steps)

藏色散人
藏色散人forward
2021-12-20 14:28:484748browse

This article is written by the tutorial column of Pagoda to introduce to you how to deploy the egg web project in the Pagoda panel. I hope it will be helpful to you if you need it!

Pagoda deployment egg web project

Requires local access to the remote database Note:

1. Alibaba Cloud settings security group

2. Pagoda installs "System Firewall 2.9" => Set the release port

For example: 7002 is used to access mongodb, 7001 is used to access api

The simplest way to test the database connection: Just access your domain name: port directly in the browser or use the server ip: port. If you are using a domain name, please note that the domain name needs to be resolved and bound to the IP. You can see the browser prompt as follows That’s it:

3. Migrate local mongoDB to online/database initialization script

Initialize database: app.js

app.beforeStart(async () => {
    const ctx = app.createAnonymousContext();
    const model = ctx.model;
    const existGroup = await model.UserGroup.findOne({name: '超级管理员'})
    if(!existGroup){
            const group = await model.UserGroup.create({
            "name" : "普通管理员",
            "idName" : "admin",
            "role" : 100
            });
    }
    console.log("==app beforeStart==");
});

4 , egg running configuration

1. Add a website and upload the project (if the code is hosted in a remote warehouse, it is recommended to open the terminal panel directly in the website directory to perform git clone)

Note: When uploading the project, delete the node_modules file and then upload it. This will be much faster. Then find "File" => "Terminal" on the left (next to the favorites), open the password connection (or use the server's terminal); enter the command line :cd /www/wwwroot/project directory, and then re-npm install. This can avoid the embarrassment of being unable to start the project to a certain extent.

2. Process management: (to be verified!)

You can create index.js to start the egg service

Method 1, egg has a built-in process manager, which can be directly used in the project directory Run the command below: node index.js index.js code:

const egg = require('egg');
const workers = Number(process.argv[2] || require('os').cpus().length);
egg.startCluster({
  workers,
  baseDir: __dirname,
});

Method 2, install PM2 for node process management, select the file and click "Add"

Note: After configuring, discover the front end It can be accessed but the api cannot be accessed. Pay attention to see if there is a port occupancy problem

Check the occupancy of all ports, enter the command: `netstat -ano`, and use the command `pkill node` to kill the processes in batches

3. Add a reverse proxy to the running port of the egg project

5. The front-end project is packaged and placed in the static directory of egg, and configured in config/config.default.js (You can also start a new project)

config.static = {
prefix: '/',
dir: [ 
path.join(__dirname, '../app/public'), //上传文件等的目录
path.join(__dirname, '../dist') //放置前端打包后的文件
]}

Then add a redirect in router.js to redirect non-api requests to index.html to display the front-end page

app.router.redirect('/', '/index.html', 302);

6. Code synchronization (using git)

(假设本地已有git并且提交到远程)
1、在宝塔面板,项目路径下打开终端:
2、方法一、文件未提交则直接git clone [url]
    方法二、如果文件已提交,
     初始化:
        git init
     设置远程仓库地址:
        git remote origin set-url [url] 或 git remote add origin [url]
    设置用户名:
        git config --global user.name [用户名]
        git config --global user.email [邮箱]  
        想要只在当前项目下设置的话把--global去掉
    如本地有文件,先执行:
        git add .
        git commit -m "init"
        git push -u origin master
    拉取:
        git config --global credential.helper store //让git记住用户名密码,下次不用填
        git pull  //输入用户名密码 拉取远程代码便可

The above is the detailed content of Pagoda panel deploys egg+web project (with detailed steps). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.im. If there is any infringement, please contact admin@php.cn delete