求最新Django部署至IIS生产环境的配置文档
Absolutely! I’ve put together an up-to-date step-by-step guide for deploying modern Django (4.x+) on IIS 10+, since most old tutorials skip over recent changes like virtual environment best practices and updated handler mapping quirks. Let’s walk through it:
First, make sure you have these ready:
- Windows Server 2016+/Windows 10+ with IIS installed
- Python 3.8+ (don’t forget to check "Add Python to PATH" during installation)
- A tested Django 4.x+ project (runs locally without errors)
- The
wfastcgipackage (we’ll install this in the setup phase)
Start by getting your project ready for deployment:
- Open Command Prompt or PowerShell, navigate to your project’s root folder (where
manage.pylives) - Create a virtual environment to isolate dependencies:
python -m venv venv - Activate the virtual environment:
- Command Prompt:
venv\Scripts\activate.bat - PowerShell:
venv\Scripts\Activate.ps1(if you get an execution policy error, runSet-ExecutionPolicy RemoteSigned -Scope CurrentUserfirst)
- Command Prompt:
- Install required packages:
pip install django wfastcgi - Double-check your project runs locally:
python manage.py runserver– fix any bugs before moving on.
First, enable the IIS components we need:
- Open "Turn Windows features on or off" (search for it in the Start menu)
- Check these boxes:
- Internet Information Services > Web Management Tools > IIS Management Console
- Internet Information Services > World Wide Web Services > Application Development Features > CGI
- Internet Information Services > World Wide Web Services > Common HTTP Features > Static Content, Default Document, Directory Browsing
- Click "OK" and wait for the features to install.
Now set up the site in IIS:
Open IIS Manager, right-click "Sites" in the left pane > "Add Website"
Fill in the details:
- Site name: A descriptive name (e.g., "MyDjangoApp")
- Physical path: The full path to your Django project root (not the
venvfolder) - Binding: Choose a port (like 8080) or assign a hostname if you have one
Click "OK" – we’ll fix the application pool next.
Tweak the Application Pool:
- Right-click your site’s application pool (same name as the site) > "Advanced Settings"
- Under "General":
- Set ".NET Framework version" to
No Managed Code(we’re using Python, not .NET) - Set "Identity" to
LocalSystem(or a dedicated service account with read/write access to your project folder)
- Set ".NET Framework version" to
- Under "Process Model":
- Enable "Load User Profile" – this fixes virtual environment activation issues.
We’ll use wfastcgi to connect IIS to Django:
Open an elevated Command Prompt/PowerShell (run as Administrator)
Activate your virtual environment, then run:
wfastcgi-enableCopy the output line (it looks like
C:\path\to\venv\Scripts\python.exe|C:\path\to\venv\Lib\site-packages\wfastcgi.py) – you’ll need this for the handler mapping.Back in IIS Manager:
- Select your website, double-click "Handler Mappings" in the middle pane
- Click "Add Module Mapping" on the right
- Fill in the fields:
- Request path:
* - Module:
FastCgiModule - Executable: Paste the line you copied from
wfastcgi-enable - Name:
Django FastCGI
- Request path:
- Click "OK" and confirm the prompt to create a FastCGI application.
Create a web.config file in your project root (next to manage.py) with this content:
<?xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer> <handlers> <add name="Django FastCGI" path="*" verb="*" modules="FastCgiModule" scriptProcessor="YOUR_COPIED_EXECUTABLE_LINE" resourceType="Unspecified" requireAccess="Script" /> </handlers> <staticContent> <mimeMap fileExtension=".woff2" mimeType="font/woff2" /> <!-- Add other mime types if your static files need them (e.g., .svg, .webp) --> </staticContent> </system.webServer> <appSettings> <!-- Django WSGI handler --> <add key="WSGI_HANDLER" value="django.core.wsgi.get_wsgi_application()" /> <!-- Full path to your project root --> <add key="PYTHONPATH" value="C:\PATH\TO\YOUR\DJANGO\PROJECT" /> <!-- Your project's settings module (e.g., myblog.settings) --> <add key="DJANGO_SETTINGS_MODULE" value="your_project_name.settings" /> </appSettings> </configuration>
Replace these placeholders:
YOUR_COPIED_EXECUTABLE_LINE: The line you copied fromwfastcgi-enableC:\PATH\TO\YOUR\DJANGO\PROJECT: Full path to your project rootyour_project_name.settings: Your Django project’s settings module (e.g.,myblog.settings)
Django doesn’t serve static files in production, so we’ll let IIS handle that:
- In your Django
settings.py:STATIC_ROOT = BASE_DIR / 'staticfiles' MEDIA_ROOT = BASE_DIR / 'media' STATIC_URL = '/static/' MEDIA_URL = '/media/' - Run this command to collect all static files into the
staticfilesfolder:python manage.py collectstatic - In IIS Manager:
- Right-click your website > "Add Virtual Directory"
- Alias:
static - Physical path:
C:\PATH\TO\YOUR\PROJECT\staticfiles
- Alias:
- Repeat for media files:
- Alias:
media - Physical path:
C:\PATH\TO\YOUR\PROJECT\media
- Alias:
- Right-click your website > "Add Virtual Directory"
- For each virtual directory:
- Double-click "Handler Mappings" > "View Ordered List"
- Move the "StaticFile" handler above the "Django FastCGI" handler – this ensures IIS serves static files directly instead of passing them to Django.
- Restart your IIS website (right-click > "Manage Website" > "Restart")
- Open your browser and navigate to
http://localhost:8080(or your assigned hostname/port)
If you see your Django site, you’re all set! If not, check these troubleshooting tips:
- Look at IIS logs in
C:\inetpub\logs\LogFilesfor specific errors - Ensure you activated the virtual environment when running
wfastcgi-enable - Double-check all paths in
web.configare correct - Verify the application pool identity has read/write permissions to your project folder
内容的提问来源于stack exchange,提问作者Victor Rodriguez

