You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求最新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:

Prerequisites

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 wfastcgi package (we’ll install this in the setup phase)
Step 1: Set Up Your Django Project & Virtual Environment

Start by getting your project ready for deployment:

  • Open Command Prompt or PowerShell, navigate to your project’s root folder (where manage.py lives)
  • 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, run Set-ExecutionPolicy RemoteSigned -Scope CurrentUser first)
  • Install required packages:
    pip install django wfastcgi
    
  • Double-check your project runs locally: python manage.py runserver – fix any bugs before moving on.
Step 2: Configure IIS Core Features

First, enable the IIS components we need:

  1. Open "Turn Windows features on or off" (search for it in the Start menu)
  2. 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
  3. Click "OK" and wait for the features to install.
Step 3: Create & Configure Your IIS Website

Now set up the site in IIS:

  1. Open IIS Manager, right-click "Sites" in the left pane > "Add Website"

  2. Fill in the details:

    • Site name: A descriptive name (e.g., "MyDjangoApp")
    • Physical path: The full path to your Django project root (not the venv folder)
    • Binding: Choose a port (like 8080) or assign a hostname if you have one
  3. Click "OK" – we’ll fix the application pool next.

  4. 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)
    • Under "Process Model":
      • Enable "Load User Profile" – this fixes virtual environment activation issues.
Step 4: Set Up FastCGI Handler Mapping

We’ll use wfastcgi to connect IIS to Django:

  1. Open an elevated Command Prompt/PowerShell (run as Administrator)

  2. Activate your virtual environment, then run:

    wfastcgi-enable
    
  3. Copy 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.

  4. 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
    • Click "OK" and confirm the prompt to create a FastCGI application.
Step 5: Configure web.config for Django

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 from wfastcgi-enable
  • C:\PATH\TO\YOUR\DJANGO\PROJECT: Full path to your project root
  • your_project_name.settings: Your Django project’s settings module (e.g., myblog.settings)
Step 6: Handle Static & Media Files

Django doesn’t serve static files in production, so we’ll let IIS handle that:

  1. In your Django settings.py:
    STATIC_ROOT = BASE_DIR / 'staticfiles'
    MEDIA_ROOT = BASE_DIR / 'media'
    STATIC_URL = '/static/'
    MEDIA_URL = '/media/'
    
  2. Run this command to collect all static files into the staticfiles folder:
    python manage.py collectstatic
    
  3. In IIS Manager:
    • Right-click your website > "Add Virtual Directory"
      • Alias: static
      • Physical path: C:\PATH\TO\YOUR\PROJECT\staticfiles
    • Repeat for media files:
      • Alias: media
      • Physical path: C:\PATH\TO\YOUR\PROJECT\media
  4. 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.
Step 7: Test Your Deployment
  • 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\LogFiles for specific errors
  • Ensure you activated the virtual environment when running wfastcgi-enable
  • Double-check all paths in web.config are correct
  • Verify the application pool identity has read/write permissions to your project folder

内容的提问来源于stack exchange,提问作者Victor Rodriguez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 21:47:59