在Windows IIS中部署Django项目

发表回复

确认码
输入您在图片中看到的字符,不必区分大小写。
表情
:D :) ;) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :!: :?: :idea: :arrow: :| :mrgreen: :geek: :ugeek:

BBCode 允许
[img] 允许
[url] 允许
表情 允许

主题浏览
   

展开视图 主题浏览: 在Windows IIS中部署Django项目

在Windows IIS中部署Django项目

BG6RSH » 周二 8月 25, 2026 8:55 am

predictionsCool 部署说明

项目概述
  • 框架:Django 6.1
    WSGI 服务器:waitress 3.0.2
    数据来源:data/predictions_COOL.xlsx(后续可切换为数据库)
    部署目标:Windows IIS + HttpPlatformHandler
项目结构
  1. predictionsCool/
  2. ├── data/
  3. │   └── predictions_COOL.xlsx          # 数据源
  4. ├── predictions/                        # Django App
  5. │   ├── templatetags/
  6. │   │   ├── __init__.py
  7. │   │   └── prediction_tags.py          # 自定义模板过滤器
  8. │   ├── __init__.py
  9. │   ├── services.py                     # 数据服务层(Excel/数据库抽象)
  10. │   ├── views.py                        # 视图(页面 + JSON API)
  11. │   └── urls.py                         # App 路由
  12. ├── templates/
  13. │   └── predictions/
  14. │       └── data_list.html              # 数据展示页面
  15. ├── predictionsCool/
  16. │   ├── settings.py                     # 项目配置
  17. │   ├── urls.py                         # 项目路由
  18. │   └── wsgi.py                         # WSGI 入口
  19. ├── logs/                               # IIS 运行日志
  20. ├── web.config                          # IIS 配置
  21. └── manage.py
环境要求
  1. 组件  要求
  2. 操作系统    Windows 10/11 或 Windows Server
  3. Python  3.13+(非应用商店版,建议从 python.org 安装)
  4. IIS 需启用 HttpPlatformHandler 模块
  5. 依赖包   django、waitress、openpyxl
重要:不要使用 Windows 应用商店版 Python,其路径 (WindowsApps) 对 IIS 有访问限制,会导致部署失败。

部署步骤
1. 安装 IIS 和 HttpPlatformHandler
  • 启用 IIS:控制面板 → 程序和功能 → 启用或关闭 Windows 功能 → 勾选 Internet Information Services
    下载安装 HttpPlatformHandler v1.2: 安装后重启 IIS:
  1. # 管理员 PowerShell
  2. iisreset
2. 解锁 IIS 配置节
以管理员身份运行 PowerShell:
  1. %windir%\system32\inetsrv\appcmd.exe unlock config /section:system.webServer/handlers
  2. %windir%\system32\inetsrv\appcmd.exe unlock config /section:system.webServer/httpPlatform
  3. %windir%\system32\inetsrv\appcmd.exe unlock config /section:system.webServer/modules
3. 创建虚拟环境并安装依赖
  1. cd D:\Projects\Python\predictionsCool
  2.  
  3. # 使用非应用商店版 Python 创建虚拟环境
  4. python -m venv .venv
  5.  
  6. # 安装依赖
  7. .venv\Scripts\pip.exe install django waitress openpyxl
4. 设置目录权限
以管理员身份运行 PowerShell,授权 IIS 访问所需目录:
  1. # Python 安装目录(读取+执行)—— 路径按实际 Python 安装位置修改
  2. icacls "C:\Users\hello\AppData\Local\Programs\Python\Python313" /grant "IIS_IUSRS:(OI)(CI)RX" /T
  3.  
  4. # Python 路径的所有父级目录(遍历权限)
  5. icacls "C:\Users\hello\AppData\Local\Programs" /grant "IIS_IUSRS:(CI)(IO)RX"
  6. icacls "C:\Users\hello\AppData\Local" /grant "IIS_IUSRS:(CI)(IO)RX"
  7. icacls "C:\Users\hello\AppData" /grant "IIS_IUSRS:(CI)(IO)RX"
  8. icacls "C:\Users\hello" /grant "IIS_IUSRS:(CI)(IO)RX"
  9.  
  10. # 项目目录(读取)
  11. icacls "D:\Projects\Python\predictionsCool" /grant "IIS_IUSRS:(OI)(CI)R" /T
  12.  
  13. # 日志目录(写入)
  14. icacls "D:\Projects\Python\predictionsCool\logs" /grant "IIS_IUSRS:(OI)(CI)W" /T

5. 配置 IIS 网站
  1. 1.打开 IIS 管理器(inetmgr)
  2. 2.右键 应用程序池 → 添加应用程序池:
  3.   名称:predictionsCool
  4.   .NET CLR 版本:无托管代码(重要!)
  5.   管道模式:集成
  6. 3.右键 网站 → 添加网站:
  7.   网站名称:predictionsCool
  8.   应用程序池:predictionsCool
  9.   物理路径:D:\Projects\Python\predictionsCool
  10.   端口:80(或自定义端口)
  11. 4.启动网站
6. 验证
  1. 页面访问:http://localhost/
  2. API 接口:http://localhost/api/data/
  3. 查看日志:D:\Projects\Python\predictionsCool\logs\stdout_*.log
web.config 说明
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <configuration>
  3.     <system.webServer>
  4.         <handlers>
  5.             <!-- 注册 HttpPlatformHandler,转发所有请求 -->
  6.             <add name="httpPlatformHandler" path="*" verb="*"
  7.                 modules="httpPlatformHandler" resourceType="Unspecified" />
  8.         </handlers>
  9.         <httpPlatform
  10.            processPath="D:\Projects\Python\predictionsCool\.venv\Scripts\waitress-serve.exe"
  11.            arguments="--listen=127.0.0.1:%HTTP_PLATFORM_PORT% --channel-timeout=120 predictionsCool.wsgi:application"
  12.            stdoutLogEnabled="true"
  13.            stdoutLogFile="D:\Projects\Python\predictionsCool\logs\stdout"
  14.            startupTimeLimit="60"
  15.            processesPerApplication="1">
  16.             <environmentVariables>
  17.                 <environmentVariable name="DJANGO_SETTINGS_MODULE" value="predictionsCool.settings" />
  18.             </environmentVariables>
  19.         </httpPlatform>
  20.     </system.webServer>
  21. </configuration>
关键配置项:
  • processPath:指向虚拟环境中的 waitress-serve.exe
    arguments:%HTTP_PLATFORM_PORT% 由 IIS 自动分配动态端口
    stdoutLogFile:日志输出路径,需确保 logs 目录存在且有写权限
架构说明
  1. 浏览器 → IIS (80端口) → HttpPlatformHandler → waitress (动态端口) → Django WSGI
IIS 通过 HttpPlatformHandler 自动启动 waitress 进程,并将请求反向代理到 waitress 监听的动态端口。

页首