|
|
@@ -0,0 +1,322 @@
|
|
|
+# 📘 Box 系统后端部署指南(中文完整版)
|
|
|
+
|
|
|
+---
|
|
|
+
|
|
|
+# 1. 安装 Node.js 20.x(使用 NVM)
|
|
|
+
|
|
|
+```bash
|
|
|
+# 安装 NVM(Node 版本管理器)
|
|
|
+curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
|
|
|
+
|
|
|
+# 让 NVM 生效(当前终端会话)
|
|
|
+export NVM_DIR="$HOME/.nvm"
|
|
|
+[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
|
|
|
+[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
|
|
|
+
|
|
|
+# 检查 NVM 是否安装成功
|
|
|
+nvm --version
|
|
|
+
|
|
|
+# 安装 Node.js 20 LTS
|
|
|
+nvm install 20
|
|
|
+
|
|
|
+# 设置默认 Node 版本
|
|
|
+nvm alias default 20
|
|
|
+
|
|
|
+# 确认 Node & npm 版本
|
|
|
+node --version
|
|
|
+npm --version
|
|
|
+```
|
|
|
+
|
|
|
+---
|
|
|
+
|
|
|
+# 2. 安装 pnpm
|
|
|
+
|
|
|
+```bash
|
|
|
+npm install -g pnpm
|
|
|
+pnpm --version
|
|
|
+```
|
|
|
+
|
|
|
+---
|
|
|
+
|
|
|
+# 3. 安装与配置 MySQL 8.0
|
|
|
+
|
|
|
+## 创建数据库与用户
|
|
|
+
|
|
|
+```bash
|
|
|
+mysql -u root -p
|
|
|
+```
|
|
|
+
|
|
|
+```sql
|
|
|
+CREATE DATABASE box_admin CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
|
|
+
|
|
|
+CREATE USER 'boxdbuser'@'localhost' IDENTIFIED BY 'ZXcv1234';
|
|
|
+GRANT ALL PRIVILEGES ON box_admin.* TO 'boxdbuser'@'localhost';
|
|
|
+FLUSH PRIVILEGES;
|
|
|
+```
|
|
|
+
|
|
|
+## 配置环境变量
|
|
|
+
|
|
|
+编辑 `.env.mgnt.test` 与 `.env.mgnt`:
|
|
|
+
|
|
|
+```bash
|
|
|
+MYSQL_URL="mysql://boxdbuser:ZXcv1234@localhost:3306/box_admin"
|
|
|
+```
|
|
|
+
|
|
|
+---
|
|
|
+
|
|
|
+# 4. 安装与配置 MongoDB 6.0(含副本集)
|
|
|
+
|
|
|
+## 创建用户(启用认证前)
|
|
|
+
|
|
|
+```bash
|
|
|
+mongosh
|
|
|
+```
|
|
|
+
|
|
|
+```javascript
|
|
|
+use admin
|
|
|
+db.createUser({
|
|
|
+ user: "admin",
|
|
|
+ pwd: "ZXcv1234",
|
|
|
+ roles: [ { role: "userAdminAnyDatabase", db: "admin" }, { role: "root", db: "admin" } ]
|
|
|
+})
|
|
|
+
|
|
|
+use box_admin
|
|
|
+db.createUser({
|
|
|
+ user: "boxuser",
|
|
|
+ pwd: "ZXcv1234",
|
|
|
+ roles: [ { role: "readWrite", db: "box_admin" } ]
|
|
|
+})
|
|
|
+
|
|
|
+exit
|
|
|
+```
|
|
|
+
|
|
|
+## 环境变量
|
|
|
+
|
|
|
+编辑:
|
|
|
+
|
|
|
+- `.env.mgnt.test`
|
|
|
+- `.env.mgnt`
|
|
|
+- `.env.app.test`
|
|
|
+- `.env.app`
|
|
|
+
|
|
|
+确保:
|
|
|
+
|
|
|
+```bash
|
|
|
+MONGO_URL="mongodb://boxuser:ZXcv1234@localhost:27017/box_admin?authSource=box_admin&replicaSet=rs0"
|
|
|
+```
|
|
|
+
|
|
|
+## 生成 Mongo Keyfile
|
|
|
+
|
|
|
+```bash
|
|
|
+sudo mkdir -p /var/lib/mongo/keyfile
|
|
|
+sudo openssl rand -base64 756 | sudo tee /var/lib/mongo/keyfile/mongodb-keyfile > /dev/null
|
|
|
+sudo chown mongod:mongod /var/lib/mongo/keyfile/mongodb-keyfile
|
|
|
+sudo chmod 400 /var/lib/mongo/keyfile/mongodb-keyfile
|
|
|
+```
|
|
|
+
|
|
|
+## 配置 /etc/mongod.conf
|
|
|
+
|
|
|
+```yaml
|
|
|
+net:
|
|
|
+ port: 27017
|
|
|
+ bindIp: 127.0.0.1
|
|
|
+
|
|
|
+storage:
|
|
|
+ dbPath: /var/lib/mongo
|
|
|
+ journal:
|
|
|
+ enabled: true
|
|
|
+
|
|
|
+security:
|
|
|
+ authorization: enabled
|
|
|
+ keyFile: /var/lib/mongo/keyfile/mongodb-keyfile
|
|
|
+
|
|
|
+replication:
|
|
|
+ replSetName: 'rs0'
|
|
|
+
|
|
|
+systemLog:
|
|
|
+ destination: file
|
|
|
+ logAppend: true
|
|
|
+ path: /var/log/mongodb/mongod.log
|
|
|
+ verbosity: 0
|
|
|
+```
|
|
|
+
|
|
|
+## 重启 + 初始化副本集
|
|
|
+
|
|
|
+```bash
|
|
|
+sudo systemctl restart mongod
|
|
|
+sudo systemctl status mongod
|
|
|
+```
|
|
|
+
|
|
|
+```bash
|
|
|
+mongosh -u admin -p
|
|
|
+use admin
|
|
|
+rs.initiate({ _id: "rs0", members: [ { _id: 0, host: "localhost:27017" } ] })
|
|
|
+rs.status()
|
|
|
+exit
|
|
|
+```
|
|
|
+
|
|
|
+---
|
|
|
+
|
|
|
+# 5. 安装 PM2
|
|
|
+
|
|
|
+```bash
|
|
|
+npm install -g pm2
|
|
|
+pm2 --version
|
|
|
+pm2 startup systemd
|
|
|
+```
|
|
|
+
|
|
|
+---
|
|
|
+
|
|
|
+# 6. 后端部署(TEST 环境)
|
|
|
+
|
|
|
+## Step 1:准备目录
|
|
|
+
|
|
|
+```bash
|
|
|
+mkdir -p /usr/local/apps/box-project
|
|
|
+cd /usr/local/apps/box-project
|
|
|
+```
|
|
|
+
|
|
|
+## Step 2:部署后端
|
|
|
+
|
|
|
+```bash
|
|
|
+git clone http://47.238.201.61:3000/fct/box-nestjs-monorepo.git
|
|
|
+cd box-nestjs-monorepo
|
|
|
+
|
|
|
+# 使用 TEST 环境
|
|
|
+tcp .env.mgnt.test .env.mgnt
|
|
|
+cp .env.app.test .env.app
|
|
|
+
|
|
|
+pnpm install
|
|
|
+npm rebuild bcrypt
|
|
|
+pnpm prisma:generate
|
|
|
+pnpm prisma:setup:test
|
|
|
+```
|
|
|
+
|
|
|
+---
|
|
|
+
|
|
|
+# 7. PM2 启动后端服务
|
|
|
+
|
|
|
+## 启动 mgnt API
|
|
|
+
|
|
|
+```bash
|
|
|
+pnpm build:mgnt
|
|
|
+pm2 start dist/apps/box-mgnt-api/main.js --name box-mgnt
|
|
|
+pm2 save
|
|
|
+pm2 logs box-mgnt
|
|
|
+```
|
|
|
+
|
|
|
+## 启动 app API
|
|
|
+
|
|
|
+```bash
|
|
|
+pnpm build:app
|
|
|
+pm2 start dist/apps/box-app-api/main.js --name box-app
|
|
|
+pm2 save
|
|
|
+pm2 logs box-app
|
|
|
+```
|
|
|
+
|
|
|
+---
|
|
|
+
|
|
|
+# 8. 配置 Nginx 反代
|
|
|
+
|
|
|
+⚠️ 端口需与 `.env.mgnt` / `.env.app` 对应。
|
|
|
+
|
|
|
+## mgnt(后台)
|
|
|
+
|
|
|
+```bash
|
|
|
+sudo vim /etc/nginx/sites-available/box-mgnt.conf
|
|
|
+```
|
|
|
+
|
|
|
+```nginx
|
|
|
+server {
|
|
|
+ listen 80;
|
|
|
+ server_name mgnt.box-domain.com;
|
|
|
+
|
|
|
+ location / {
|
|
|
+ proxy_pass http://localhost:3300;
|
|
|
+ proxy_set_header Host $host;
|
|
|
+ proxy_set_header X-Real-IP $remote_addr;
|
|
|
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
|
+ proxy_set_header X-Forwarded-Proto $scheme;
|
|
|
+ }
|
|
|
+}
|
|
|
+```
|
|
|
+
|
|
|
+```bash
|
|
|
+sudo ln -s /etc/nginx/sites-available/box-mgnt.conf /etc/nginx/sites-enabled/
|
|
|
+sudo nginx -t
|
|
|
+sudo systemctl reload nginx
|
|
|
+```
|
|
|
+
|
|
|
+## app(前台)
|
|
|
+
|
|
|
+```bash
|
|
|
+sudo vim /etc/nginx/sites-available/box-app.conf
|
|
|
+```
|
|
|
+
|
|
|
+```nginx
|
|
|
+server {
|
|
|
+ listen 80;
|
|
|
+ server_name app.box-domain.com;
|
|
|
+
|
|
|
+ location / {
|
|
|
+ proxy_pass http://localhost:3301;
|
|
|
+ proxy_set_header Host $host;
|
|
|
+ proxy_set_header X-Real-IP $remote_addr;
|
|
|
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
|
+ proxy_set_header X-Forwarded-Proto $scheme;
|
|
|
+ }
|
|
|
+}
|
|
|
+```
|
|
|
+
|
|
|
+```bash
|
|
|
+sudo ln -s /etc/nginx/sites-available/box-app.conf /etc/nginx/sites-enabled/
|
|
|
+sudo nginx -t
|
|
|
+sudo systemctl reload nginx
|
|
|
+```
|
|
|
+
|
|
|
+---
|
|
|
+
|
|
|
+# 9. 部署前端
|
|
|
+
|
|
|
+```bash
|
|
|
+git clone http://47.238.201.61:3000/fct/box-frontend.git
|
|
|
+cd box-frontend
|
|
|
+
|
|
|
+pnpm install
|
|
|
+pnpm build
|
|
|
+
|
|
|
+pm2 serve ./dist 9000 --name box-frontend --spa
|
|
|
+pm2 save
|
|
|
+pm2 logs box-frontend
|
|
|
+```
|
|
|
+
|
|
|
+## Nginx 配置前端
|
|
|
+
|
|
|
+```bash
|
|
|
+sudo vim /etc/nginx/sites-available/box-frontend.conf
|
|
|
+```
|
|
|
+
|
|
|
+```nginx
|
|
|
+server {
|
|
|
+ listen 80;
|
|
|
+ server_name man.box-domain.com;
|
|
|
+
|
|
|
+ location / {
|
|
|
+ proxy_pass http://localhost:9000;
|
|
|
+ proxy_set_header Host $host;
|
|
|
+ proxy_set_header X-Real-IP $remote_addr;
|
|
|
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
|
+ proxy_set_header X-Forwarded-Proto $scheme;
|
|
|
+ }
|
|
|
+}
|
|
|
+```
|
|
|
+
|
|
|
+```bash
|
|
|
+sudo ln -s /etc/nginx/sites-available/box-frontend.conf /etc/nginx/sites-enabled/
|
|
|
+sudo nginx-t
|
|
|
+sudo systemctl reload nginx
|
|
|
+```
|
|
|
+
|
|
|
+---
|
|
|
+
|
|
|
+# 🎉 部署完成!
|