# MES 生产管理系统部署指南

## 架构说明

```
┌─────────────────┐         FRP穿透          ┌─────────────────┐
│  腾讯云服务器    │  ◄────────────────────►  │  阿里云服务器    │
│  118.126.91.196 │    8091 ──► 7009         │  8.138.219.128  │
│  3009 ──► 7010  │                         │  www.we-smart.cn│
└─────────────────┘                         └─────────────────┘
       │                                              │
       │  内网访问                                     │  外网访问
       │  http://118.126.91.196:8091                  │  https://erp.we-smart.cn
       │  http://118.126.91.196:3009/api              │
       └──────────────────────────────────────────────┘
```

## 端口配置

| 服务 | 本地端口 | FRP远程端口 | 用途 |
|------|----------|-------------|------|
| MES Web | 8091 | 7009 | 前端页面 |
| MES API | 3009 | 7010 | 后端API |

## 部署步骤

### 1. 腾讯云服务器 (内网)

```bash
# 进入项目目录
cd /var/www/html/internal/mes

# 安装依赖
cd api && npm install && cd ..

# 启动服务
chmod +x start-mes.sh
./start-mes.sh

# 或者手动启动
# 启动API
cd api && nohup node server.js > ../logs/api.log 2>&1 &

# 启动Web服务器 (在项目根目录)
nohup python3 -m http.server 8091 > logs/web.log 2>&1 &

# 启动FRP (如果frpc在/usr/local/bin/)
nohup frpc -c frpc.ini > logs/frp.log 2>&1 &
```

### 2. 阿里云服务器 (外网入口)

```bash
# SSH登录阿里云
ssh root@8.138.219.128

# 复制Nginx配置
cp /path/to/nginx-erp.conf /etc/nginx/conf.d/erp.conf

# 检查配置
nginx -t

# 重载Nginx
systemctl reload nginx
```

### 3. 配置DNS

在域名解析商处添加记录：
- 类型: A
- 主机记录: erp
- 记录值: 8.138.219.128

## 访问地址

| 环境 | 地址 |
|------|------|
| 内网 | http://118.126.91.196:8091 |
| 外网 | https://erp.we-smart.cn |
| API | https://erp.we-smart.cn/api/ |

## 服务管理

```bash
# 查看进程
ps aux | grep -E "node|http.server|frpc" | grep -v grep

# 停止服务
pkill -f "node server.js"
pkill -f "http.server 8091"
pkill -f "frpc.*mes"

# 查看日志
tail -f logs/api.log
tail -f logs/web.log
tail -f logs/frp.log
```

## 文件结构

```
/var/www/html/internal/mes/
├── index.html          # 订单管理页面
├── schedule.html       # 生产排期页面
├── board.html          # 生产看板页面
├── gantt.html          # 甘特图页面
├── frpc.ini            # FRP客户端配置
├── start-mes.sh        # 启动脚本
├── nginx-erp.conf      # Nginx配置模板
├── logs/               # 日志目录
└── api/                # 后端API
    ├── server.js       # Express服务器
    ├── package.json    # 依赖配置
    └── data/
        └── db.json     # 数据库文件
```
