部署指南(Windows Server + IIS)

從 CLAUDE.md 分離。部署相關設定,開發時不需載入。


前置需求

項目說明
.NET 8 Runtime + Hosting Bundle安裝後重啟 IIS
Node.js 18+前端編譯用
GitServer 上拉取程式碼
IIS 啟用開啟 Windows 功能 → IIS + WebSocket Protocol

IIS 設定

  1. 應用程式集區:新增 ExamSystem,.NET CLR 版本選「沒有受控碼」(No Managed Code)
  2. 應用程式集區進階設定(BackgroundService 必要):
    • 啟動模式(Start Mode)AlwaysRunning(預設 OnDemand 會閒置回收,導致排程停止)
    • 閒置逾時(Idle Time-out)0(停用閒置回收)
  3. 站台進階設定
    • 預先載入已啟用(Preload Enabled)True(確保站台啟動時立即載入,BackgroundService 隨之啟動)
  4. 新增網站:指向發佈資料夾(如 C:\inetpub\ExamSystem),綁定 Port(如 80 或 5199)
  5. web.configdotnet publish 會自動產生,確認 aspNetCore 節點指向正確的 dll

發佈步驟

# 1. 拉取最新程式碼
git pull origin main
 
# 2. 發佈後端
dotnet publish -c Release -o C:\inetpub\ExamSystem
 
# 3. 發佈前端
cd client-app
npm install
npm run build
 
# 4. 複製前端到後端 wwwroot
xcopy /E /Y dist\* C:\inetpub\ExamSystem\wwwroot\
 
# 5. 重啟 IIS 應用程式集區
iisreset /restart

設定檔調整

檔案說明
appsettings.Production.jsonMSSQL 連線字串、JWT Secret、SMTP 等正式環境設定
client-app/.env.productionAPI Base URL(同域部署可省略)

appsettings.Production.json 範例

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=MSSQL主機;Database=ExamSystem;Trusted_Connection=True;TrustServerCertificate=True;"
  },
  "Jwt": {
    "Key": "正式環境請更換為高強度密鑰",
    "Issuer": "ExamSystem",
    "ExpireHours": 8
  }
}

注意事項

  • MSSQL 連線:確認 Windows Server 可連到 MSSQL 主機,防火牆開放 1433 Port
  • 檔案上傳目錄wwwroot/uploads/ 需確保 IIS 應用程式集區帳號有寫入權限
    wwwroot/uploads/
    ├── attachments/          ← 考核表單附件(具體事蹟說明檔案)
    │   └── attach_{participantId}_{yyyyMMddHHmmss}.{ext}
    ├── bug-reports/          ← Bug 回報截圖
    │   └── {bugReportId}/
    │       └── {guid}.{ext}
    └── interviews/           ← 強制面談紀錄(PDF/圖片)
        └── interview_{participantId}_{timestamp}.{ext}
    
    • 檔案存於主機本地磁碟,不上傳雲端
    • 部署時需手動搬移或保留 uploads/ 目錄,dotnet publish 不會包含已上傳的檔案
    • 刪除考核記錄時應同步清理對應的附件檔案與 ExamAttachments 資料表記錄
  • HTTPS:正式環境建議綁定 SSL 憑證
  • 前後端同域:發佈後前端放在 wwwroot/,不需要 CORS 和 Vite proxy
  • SPA Fallbackweb.config 需加入 URL Rewrite 規則,讓所有前端路由指向 index.html
  • 自動 Migration:程式啟動時已有自動 migrate 邏輯,首次部署會自動建表
  • BackgroundService:自動排程催繳等背景服務依賴 IIS 的 AlwaysRunning 模式,務必設定應用程式集區啟動模式與閒置逾時

最後更新:2026/04/19(本次無部署設定變更)