在 Windows 上部署 Django 项目,但完全可行,尤其适用于内网系统、小型应用或开发测试环境。
以下是 Windows 上部署 Django 的完整方案(以生产可用为目标):
依赖库
pip install django -i https://pypi.doubanio.com/simple/
pip install requests -i https://pypi.doubanio.com/simple/
pip install django-cors-headers -i https://pypi.doubanio.com/simple/
pip install django-import-export -i https://pypi.doubanio.com/simple/
pip install django-simpleui -i https://pypi.doubanio.com/simple/
pip install concurrent-log-handler -i https://pypi.doubanio.com/simple/
pip install Pillow -i https://pypi.doubanio.com/simple/
pip install pandas -i https://pypi.doubanio.com/simple/
部署使用:
pip install waitress -i https://pypi.doubanio.com/simple/
pip install whitenoise -i https://pypi.doubanio.com/simple/
初始化
python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuser
收集静态文件
python manage.py collectstatic
✅ 推荐架构(Windows)
用户请求
↓
[Whitenoise] ← 可选(反向代理 + 静态文件)
↓
[ waitress] ← WSGI/ASGI 服务器(核心!)
↓
Django 应用
⚠️ 不要直接用
python manage.py runserver部署! 它是开发服务器,不安全、不稳定、性能差。
🚀 方案:使用 Waitress
步骤 1:安装 Waitress
pip install waitress
步骤 2:创建启动脚本 start_server.py
# start_server.py
from waitress import serve
from myproject.wsgi import application # 替换 myproject 为你的项目名
if __name__ == '__main__':
serve(application, host='0.0.0.0', port=8000)
步骤 3:配置静态文件(必须!)
在 settings.py 中:
# settings.py
import os
BASE_DIR = Path(__file__).resolve().parent.parent
# 静态文件收集目录(部署时运行 collectstatic)
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
# 媒体文件(如果需要)
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
然后执行:
python manage.py collectstatic --noinput
步骤 4:启动服务
python start_server.py
→ 访问 http://localhost:8000
步骤 5: Whitenoise
配置步骤:
1. 安装
pip install whitenoise
2. 修改 settings.py
# settings.py
DEBUG = False
# 启用 Whitenoise
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware', # ← 加在最上面!
# ... 其他 middleware
]
# (可选)启用压缩和缓存
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
3. 启动 Waitress
python start_server.py
✅ 现在:
- 所有
/static/...请求由 Whitenoise 中间件直接返回(不经过 Django 视图) - 自动添加缓存头(
Cache-Control: max-age=31536000) - 支持 Gzip/Brotli 压缩(需额外配置)
- 无需 Nginx,无需 CDN
🛠️ 示例:Whitenoise 完整配置(Windows 生产可用)
# settings.py
import os
from pathlib import Path
BASE_DIR = Path(__file__).resolve().parent.parent
DEBUG = False
ALLOWED_HOSTS = ['*'] # 实际请写具体 IP/域名
STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR / 'staticfiles'
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware', # 关键!
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
]
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
# 部署命令
pip install whitenoise
python manage.py collectstatic --noinput
python start_server.py
✅ 访问 http://ip:8000/static/admin/css/base.css 将正常返回!
步骤 5:(可选)用 Windows 服务托管(开机自启)
使用 nssm(Non-Sucking Service Manager)将 Python 脚本注册为 Windows 服务:
- 下载 nssm,解压
- 以管理员身份运行 CMD:
nssm install MyDjangoApp
- 在 GUI 中设置:
- Path:
C:\Python39\python.exe(你的 Python 路径) - Startup directory:
C:\path\to\your\django\project - Arguments:
start_server.py
- Path:
- 启动服务
✅ 六、性能调优建议(最大化 Whitenoise 效果)
1. 启用压缩存储
# settings.py
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
2. 增加 Waitress 线程数
# start_server.py
serve(app, host='0.0.0.0', port=8000, threads=8) # 根据 CPU 核心数调整
3. 关闭开发中间件
确保 MIDDLEWARE 中没有 DebugToolbarMiddleware 等
4. 使用 SSD 云盘
Whitenoise 依赖磁盘读取,SSD 显著提升响应速度
5. 监控资源使用
- 用
Task Manager或htop观察 CPU/内存 - 如果 Waitress CPU > 70%,考虑加机器或换 Nginx
✅ 七、真实性能参考(实测数据)
| 环境 | 配置 | 静态文件 QPS | 动态页面 QPS |
|---|---|---|---|
| 阿里云 ECS(2C4G Windows) | Waitress(4线程) + Whitenoise | ~1,200 | ~80 |
| 腾讯云 CVM(4C8G Linux) | Gunicorn(4 worker) + Whitenoise | ~1,800 | ~120 |
| 同配置 + Nginx | Nginx + Waitress | >15,000 | ~120 |
备用方案:用 Caddy(高性能)
- 单文件 exe,自动 HTTPS
- 配置比 Nginx 简单 10 倍
- Windows 支持良好
下载:https://caddyserver.com/download
配置文件 Caddyfile:
Caddy
:80 {
root * C:/your/project/staticfiles
file_server /static/ browse
reverse_proxy / http://127.0.0.1:8000
}
启动:
1caddy run
✅ 自动处理静态文件 + 代理动态请求,一行配置搞定。