Recently, I have deployed several Django projects on Windows Server 2012 running on IIS. I Thought maybe it's helpful to share my experiences.
Using Django 1.8.7, python 3.4, IIS 8, Windows Server 2012
First, add Web Server Role to activate IIS on Windows. Tutorials on iis.net
Make sure to enable CGI.
Create a Django project by running:
cd D: mkdir sites cd sites django-admin startproject demo cd demo\demo mkdir statics mkdir media mklink demoPython.exe C:\Python34\python.exe
In your settings.py set MEDIA_ROOT, STATIC_ROOT, MEDIA_URL and STATIC_URL as below
# ... import os MEDIA_ROOT = os.path.join(BASE_DIR, 'media') STATIC_ROOT = os.path.join(BASE_DIR, 'statics') MEDIA_URL = '/media/' STATIC_URL = '/static/' # ...
The directory structure for our demo project will be as below.
demo ├── demo │ ├── __init__.py │ ├── settings.py │ ├── urls.py │ └── wsgi.py ├── media ├── statics ├── manage.py └── demoPython.exe
We will use wfastcgi.py script to get our project running under FastCGIModule of IIS.
First install wfastcgi.py
This will add wfastcgi.py to Scripts directory of your python installation path.
Open IIS. Add new website.
Set Physical Path to D:\sites\demo\demo\
Go to Handler Mappings and from the right panel choose Add Module Mapping.
Add Module Mapping
Set Executable: to D:\sites\demo\demo\demoPython.exe|C:\Python34\Scripts\wfastcgi.py
Open Request Restriction and uncheck Invoke handle if only request is mapped to
Invoke handle if only request is mapped to
Go to IIS Home and open FastCGI Settings.
Open the item having D:\sites\demo\demo\demoPython.exe as Full Path.
At FastCGI Properties open Environmental Variables and edit the collections.
Add new item: Name: WSGI_HANDLER, value: demo.wsgi.application
To serving media and static files by IIS, we create Virtual Directories
Add static and media Virtual Directory to demo website. Don't forget to remove demoDjango from Handler Mappings on both virtual directories