You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

348 lines
11 KiB
Markdown

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# 浩景CNC机床数据采集分析系统 - 部署指南
## 文档信息
| 项目 | 内容 |
|------|------|
| 版本 | v1.0.0 |
| 日期 | 2026-04-13 |
| 部署方式 | 使用 deploy.ps1 一键构建 + IIS 手动配置 |
---
## 目录
1. [部署流程概述](#1-部署流程概述)
2. [第一步:运行部署脚本](#2-第一步运行部署脚本)
3. [第二步:配置数据库](#3-第二步配置数据库)
4. [第三步:复制文件到 IIS 目录](#4-第三步复制文件到-iis-目录)
5. [第四步IIS 配置](#5-第四步iis-配置)
6. [第五步:修改生产配置](#6-第五步修改生产配置)
7. [第六步:验证部署](#7-第六步验证部署)
8. [故障排查](#8-故障排查)
---
## 1. 部署流程概述
### 目录结构(扁平化)
```
C:\wwwroot\haoliang\ ← IIS 网站根目录
├── Haoliang.Api.exe # 后端 API 主程序
├── Haoliang.Api.dll
├── appsettings.json # 开发配置
├── appsettings.Production.json # 生产配置 ← 需修改
├── web.config # IIS 配置文件
├── logs\ # 日志目录
├── data\ # 数据目录
├── admin\ # 管理后台 (访问 /admin/*)
│ ├── index.html
│ └── assets\
└── dashboard\ # BI 大屏 (访问 /dashboard/*)
├── index.html
└── assets\
```
### 访问地址
| 应用 | URL |
|------|-----|
| API | http://localhost:5000/api/v1/* |
| Admin | http://localhost:5000/admin |
| Dashboard | http://localhost:5000/dashboard |
| Swagger | http://localhost:5000/swagger |
### 部署流程
```
┌─────────────────────────────────────────────────────────────┐
│ 1. 运行 deploy.ps1 构建项目 │
│ 输出: publish\wwwroot\ │
└─────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────┐
│ 2. 配置数据库 (MariaDB) │
│ ├─ 创建数据库和用户 │
│ └─ 导入 cnc_business.sql / cnc_log_fixed.sql │
└─────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────┐
│ 3. 复制文件到 IIS 目录 │
│ └─ publish\wwwroot\* → C:\wwwroot\haoliang\ │
└─────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────┐
│ 4. IIS 配置(极简:仅 1 个网站) │
│ └─ 网站 Haoliang物理路径 C:\wwwroot\haoliang │
└─────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────┐
│ 5. 修改生产配置 │
│ └─ 修改 appsettings.Production.json 数据库连接 │
└─────────────────────────────────────────────────────────────┘
```
---
## 2. 第一步:运行部署脚本
### 2.1 执行构建
```powershell
cd D:\git\git\haoliang
.\deploy.ps1
```
### 2.2 构建输出
```
D:\git\git\haoliang\publish\wwwroot\
├── Haoliang.Api.exe # 后端主程序
├── Haoliang.Api.dll
├── appsettings.json
├── appsettings.Production.json # ← 稍后修改
├── web.config
├── logs\
├── data\
├── admin\ # /admin 访问
│ ├── index.html
│ └── assets\
└── dashboard\ # /dashboard 访问
├── index.html
└── assets\
```
---
## 3. 第二步:配置数据库
### 3.1 登录 MariaDB
```powershell
mysql -u root -p
```
### 3.2 创建数据库和用户
```sql
-- 创建专用数据库用户(请修改密码)
CREATE USER 'haoliang'@'localhost' IDENTIFIED BY 'YourStrongPassword123!';
CREATE USER 'haoliang'@'127.0.0.1' IDENTIFIED BY 'YourStrongPassword123!';
-- 创建业务库
CREATE DATABASE cnc_business CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- 创建日志库
CREATE DATABASE cnc_log CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- 授权
GRANT ALL PRIVILEGES ON cnc_business.* TO 'haoliang'@'localhost';
GRANT ALL PRIVILEGES ON cnc_log.* TO 'haoliang'@'localhost';
FLUSH PRIVILEGES;
```
### 3.3 导入数据库脚本
```powershell
cd D:\git\git\haoliang\database
mysql -u haoliang -p cnc_business < cnc_business.sql
mysql -u haoliang -p cnc_log < cnc_log_fixed.sql
```
### 3.4 验证数据库
```powershell
mysql -u haoliang -p -e "USE cnc_business; SHOW TABLES;"
mysql -u haoliang -p -e "USE cnc_log; SHOW TABLES;"
```
---
## 4. 第三步:复制文件到 IIS 目录
### 4.1 创建目标目录
```powershell
New-Item -ItemType Directory -Path "C:\wwwroot\haoliang" -Force
New-Item -ItemType Directory -Path "C:\wwwroot\haoliang\logs" -Force
New-Item -ItemType Directory -Path "C:\wwwroot\haoliang\data" -Force
```
### 4.2 复制文件
```powershell
Copy-Item -Path "D:\git\git\haoliang\publish\wwwroot\*" -Destination "C:\wwwroot\haoliang\" -Recurse -Force
```
### 4.3 验证目录结构
```
C:\wwwroot\haoliang\
├── Haoliang.Api.exe
├── web.config
├── appsettings.json
├── appsettings.Production.json
├── logs\
├── data\
├── admin\
│ ├── index.html
│ └── assets\
└── dashboard\
├── index.html
└── assets\
```
---
## 5. 第四步IIS 配置
### 5.1 创建应用程序池
```powershell
New-WebAppPool -Name "HaoliangApi"
Set-ItemProperty -Path "IIS:\AppPools\HaoliangApi" -Name "managedRuntimeVersion" -Value ""
Set-ItemProperty -Path "IIS:\AppPools\HaoliangApi" -Name "managedPipelineMode" -Value "Integrated"
Start-WebAppPool -Name "HaoliangApi"
```
### 5.2 创建网站(重要:直接指向根目录)
```powershell
New-WebSite -Name "Haoliang" -PhysicalPath "C:\wwwroot\haoliang" -Port 5000 -ApplicationPool "HaoliangApi"
Start-WebSite -Name "Haoliang"
```
> **注意**:物理路径直接指向 `C:\wwwroot\haoliang`不是子目录。admin 和 dashboard 是文件目录,不需要创建 IIS 子应用。
### 5.3 设置目录权限
```powershell
# API 目录权限
icacls "C:\wwwroot\haoliang" /grant "IIS AppPool\HaoliangApi:(OI)(CI)M"
icacls "C:\wwwroot\haoliang\logs" /grant "IIS AppPool\HaoliangApi:(OI)(CI)M"
icacls "C:\wwwroot\haoliang\data" /grant "IIS AppPool\HaoliangApi:(OI)(CI)M"
# 前端目录权限
icacls "C:\wwwroot\haoliang\admin" /grant "IIS_IUSRS:(OI)(CI)RX"
icacls "C:\wwwroot\haoliang\dashboard" /grant "IIS_IUSRS:(OI)(CI)RX"
```
### 5.4 防火墙配置(如需要)
```powershell
New-NetFirewallRule -DisplayName "Haoliang API" -Direction Inbound -Protocol TCP -LocalPort 5000 -Action Allow -Profile Any
```
---
## 6. 第五步:修改生产配置
### 6.1 编辑配置文件
打开 `C:\wwwroot\haoliang\appsettings.Production.json`
### 6.2 修改数据库连接
```json
{
"ConnectionStrings": {
"CNCBusinessDB": "Server=localhost;Database=cnc_business;User=haoliang;Password=YourStrongPassword123!;CharSet=utf8mb4;",
"CNCLLogDB": "Server=localhost;Database=cnc_log;User=haoliang;Password=YourStrongPassword123!;CharSet=utf8mb4;"
}
}
```
### 6.3 重启应用程序池
```powershell
Restart-WebAppPool -Name "HaoliangApi"
```
---
## 7. 第六步:验证部署
### 7.1 检查服务状态
```powershell
Get-WebSiteState -Name "Haoliang"
Get-WebAppPoolState -Name "HaoliangApi"
```
### 7.2 浏览器访问测试
| URL | 预期结果 |
|-----|----------|
| http://localhost:5000/ | API 响应 |
| http://localhost:5000/api/v1/health | 健康检查 JSON |
| http://localhost:5000/admin | 管理后台登录页 |
| http://localhost:5000/dashboard | BI 大屏首页 |
### 7.3 查看日志
```powershell
Get-Content "C:\wwwroot\haoliang\logs\stdout_*.log" -Tail 20
Get-Content "C:\wwwroot\haoliang\logs\app-*.log" -Tail 20
```
---
## 8. 故障排查
### 8.1 常见错误
| 错误 | 原因 | 解决方案 |
|------|------|----------|
| 500.19 | web.config 语法错误 | 检查 web.config |
| 500.21 | .NET Core 模块未安装 | 安装 Hosting Bundle |
| 502.5 | 应用程序启动失败 | 检查数据库连接 |
| 404 | 路径不存在 | 检查物理路径配置 |
### 8.2 排查步骤
```powershell
# 1. 检查应用程序池状态
Get-WebAppPoolState -Name "HaoliangApi"
# 2. 查看错误日志
Get-Content "C:\wwwroot\haoliang\logs\stdout_*.log" | Select-String "Error"
# 3. 测试数据库连接
mysql -u haoliang -p -e "USE cnc_business; SELECT 1;"
# 4. 检查端口占用
netstat -an | findstr ":5000"
```
### 8.3 快速重启
```powershell
Restart-WebAppPool -Name "HaoliangApi"
Start-WebSite -Name "Haoliang"
```
---
## 部署清单
```
[ ] 1. 运行 deploy.ps1 构建成功
[ ] 2. MariaDB 数据库已创建
[ ] 3. 数据库用户 haoliang 已创建并授权
[ ] 4. 数据库脚本已导入
[ ] 5. 文件已复制到 C:\wwwroot\haoliang
[ ] 6. 应用程序池 HaoliangApi 已创建
[ ] 7. 网站 Haoliang 已创建 (端口 5000物理路径指向根目录)
[ ] 8. 目录权限已设置
[ ] 9. appsettings.Production.json 数据库连接已修改
[ ] 10. 应用程序池已重启
[ ] 11. 浏览器访问测试通过
```
---
*文档最后更新2026-04-13*