在Windows IIS中部署Django项目
发表于 : 周二 8月 25, 2026 8:55 am
predictionsCool 部署说明
项目概述
环境要求
重要:不要使用 Windows 应用商店版 Python,其路径 (WindowsApps) 对 IIS 有访问限制,会导致部署失败。
部署步骤
1. 安装 IIS 和 HttpPlatformHandler
2. 解锁 IIS 配置节
以管理员身份运行 PowerShell:
3. 创建虚拟环境并安装依赖
4. 设置目录权限
以管理员身份运行 PowerShell,授权 IIS 访问所需目录:
5. 配置 IIS 网站
6. 验证
web.config 说明
关键配置项:
IIS 通过 HttpPlatformHandler 自动启动 waitress 进程,并将请求反向代理到 waitress 监听的动态端口。
项目概述
- 框架:Django 6.1
WSGI 服务器:waitress 3.0.2
数据来源:data/predictions_COOL.xlsx(后续可切换为数据库)
部署目标:Windows IIS + HttpPlatformHandler
Code: [全选] [Expand/Collapse]
- predictionsCool/
- ├── data/
- │ └── predictions_COOL.xlsx # 数据源
- ├── predictions/ # Django App
- │ ├── templatetags/
- │ │ ├── __init__.py
- │ │ └── prediction_tags.py # 自定义模板过滤器
- │ ├── __init__.py
- │ ├── services.py # 数据服务层(Excel/数据库抽象)
- │ ├── views.py # 视图(页面 + JSON API)
- │ └── urls.py # App 路由
- ├── templates/
- │ └── predictions/
- │ └── data_list.html # 数据展示页面
- ├── predictionsCool/
- │ ├── settings.py # 项目配置
- │ ├── urls.py # 项目路由
- │ └── wsgi.py # WSGI 入口
- ├── logs/ # IIS 运行日志
- ├── web.config # IIS 配置
- └── manage.py
Code: [全选] [Expand/Collapse]
- 组件 要求
- 操作系统 Windows 10/11 或 Windows Server
- Python 3.13+(非应用商店版,建议从 python.org 安装)
- IIS 需启用 HttpPlatformHandler 模块
- 依赖包 django、waitress、openpyxl
部署步骤
1. 安装 IIS 和 HttpPlatformHandler
- 启用 IIS:控制面板 → 程序和功能 → 启用或关闭 Windows 功能 → 勾选 Internet Information Services
下载安装 HttpPlatformHandler v1.2:- 下载地址:https://www.iis.net/downloads/microsoft ... rm-handler
选择 x64 版本
- 下载地址:https://www.iis.net/downloads/microsoft ... rm-handler
Code: [全选] [Expand/Collapse]
- # 管理员 PowerShell
- iisreset
以管理员身份运行 PowerShell:
Code: [全选] [Expand/Collapse]
- %windir%\system32\inetsrv\appcmd.exe unlock config /section:system.webServer/handlers
- %windir%\system32\inetsrv\appcmd.exe unlock config /section:system.webServer/httpPlatform
- %windir%\system32\inetsrv\appcmd.exe unlock config /section:system.webServer/modules
Code: [全选] [Expand/Collapse]
- cd D:\Projects\Python\predictionsCool
- # 使用非应用商店版 Python 创建虚拟环境
- python -m venv .venv
- # 安装依赖
- .venv\Scripts\pip.exe install django waitress openpyxl
以管理员身份运行 PowerShell,授权 IIS 访问所需目录:
Code: [全选] [Expand/Collapse]
- # Python 安装目录(读取+执行)—— 路径按实际 Python 安装位置修改
- icacls "C:\Users\hello\AppData\Local\Programs\Python\Python313" /grant "IIS_IUSRS:(OI)(CI)RX" /T
- # Python 路径的所有父级目录(遍历权限)
- icacls "C:\Users\hello\AppData\Local\Programs" /grant "IIS_IUSRS:(CI)(IO)RX"
- icacls "C:\Users\hello\AppData\Local" /grant "IIS_IUSRS:(CI)(IO)RX"
- icacls "C:\Users\hello\AppData" /grant "IIS_IUSRS:(CI)(IO)RX"
- icacls "C:\Users\hello" /grant "IIS_IUSRS:(CI)(IO)RX"
- # 项目目录(读取)
- icacls "D:\Projects\Python\predictionsCool" /grant "IIS_IUSRS:(OI)(CI)R" /T
- # 日志目录(写入)
- icacls "D:\Projects\Python\predictionsCool\logs" /grant "IIS_IUSRS:(OI)(CI)W" /T
5. 配置 IIS 网站
Code: [全选] [Expand/Collapse]
- 1.打开 IIS 管理器(inetmgr)
- 2.右键 应用程序池 → 添加应用程序池:
- 名称:predictionsCool
- .NET CLR 版本:无托管代码(重要!)
- 管道模式:集成
- 3.右键 网站 → 添加网站:
- 网站名称:predictionsCool
- 应用程序池:predictionsCool
- 物理路径:D:\Projects\Python\predictionsCool
- 端口:80(或自定义端口)
- 4.启动网站
Code: [全选] [Expand/Collapse]
- 页面访问:http://localhost/
- API 接口:http://localhost/api/data/
- 查看日志:D:\Projects\Python\predictionsCool\logs\stdout_*.log
Code: [全选] [Expand/Collapse]
- <?xml version="1.0" encoding="UTF-8"?>
- <configuration>
- <system.webServer>
- <handlers>
- <!-- 注册 HttpPlatformHandler,转发所有请求 -->
- <add name="httpPlatformHandler" path="*" verb="*"
- modules="httpPlatformHandler" resourceType="Unspecified" />
- </handlers>
- <httpPlatform
- processPath="D:\Projects\Python\predictionsCool\.venv\Scripts\waitress-serve.exe"
- arguments="--listen=127.0.0.1:%HTTP_PLATFORM_PORT% --channel-timeout=120 predictionsCool.wsgi:application"
- stdoutLogEnabled="true"
- stdoutLogFile="D:\Projects\Python\predictionsCool\logs\stdout"
- startupTimeLimit="60"
- processesPerApplication="1">
- <environmentVariables>
- <environmentVariable name="DJANGO_SETTINGS_MODULE" value="predictionsCool.settings" />
- </environmentVariables>
- </httpPlatform>
- </system.webServer>
- </configuration>
- processPath:指向虚拟环境中的 waitress-serve.exe
arguments:%HTTP_PLATFORM_PORT% 由 IIS 自动分配动态端口
stdoutLogFile:日志输出路径,需确保 logs 目录存在且有写权限
Code: [全选] [Expand/Collapse]
- 浏览器 → IIS (80端口) → HttpPlatformHandler → waitress (动态端口) → Django WSGI