Hosting Django sites on IIS
The following instructions outline the steps to get a Django 1.7/1.8/1.9 site running on IIS 7/8.
These instructions borrow heavily from http://codesmartinc.com/2013/04/12/running-django-in-iis7iis8/. Unfortunately, those instructions are out of date for Django 1.7/1.8 and do not work.
Please note that wfastcgi.py will need to be copied into the project directory. If you do not already have this file you can obtain it from the Python installation installed by the Web Platform installer (see original instructions references above for more info).
Copy your django project into c:\inetpub\wwwroot<project name>
Copy the wfastcgi.py file referenced above into c:\inetpub\wwwroot<project name>
Open IIS Manger
Select the root web server and verify icons for CGI and FastCGI Settings are present.
If the icons are not present then you will need to enable the CGI feature.
- Open Server Manager
- Click Add Roles and Features from the Manage menu
- Navigate through the Wizard until you get to the Server Roles section
- Check the CGI box under Web Server (IIS), Web Server, Application Development
- Click Install
Create a new website and set the physical path to c:\inetpub\wwwroot<project name>
If you are hosting multiple web sites on the sever and they need to share port 80 or 443 then you will need to set the host name for each site to avoid port conflicts. You may need to add to add an entry to the host file for the specified sub-domain to access it from the local server.
If you want to access the site using
. .com you will need to enter that in full name for the Host name. You will need to add an entry to the hosts file that maps . .com to 127.0.0.1. You may also need to add a entry to your DNS zone file to map it to the public IP address of the web server. Click Add Module Mapping (located in actions pane on the right side) and use the following settings:
Request Path: *
Module FastCgiModule
Executable: C:\Python27\python.exe|C:\inetpub\wwwroot<project_name>\wfastcgi.pyAdjust the Python path accordingly for Python 3.
If you are using a virtual environment you should specify the path to the python executable located in your virtual environment. Here is an example:
C:\virtualenvironments<projectname>\Scripts\python.exe|C:\inetpub\wwwroot<project_name>\wfastcgi.py
Click Request Restrictions
Uncheck the option to invoke handler only if requested is mapped
Click OK to close the Request Restrictions dialog box
Click OK to close the Edit Module Mapping dialog box
Click YES when prompted to add an entry to the FastCGI Collection in IIS
Select the root server
Open FastCGI Settings
Find the entry to the python path you entered in step 7 and open it
Open the Environment Variables collection property box (click the … button)
Add the following environment variables:
DJANGOSETTINGSMODULE:
.settings If you are using multiple setting files as recommended by Two Scoops of Django then your DJANGOSETTINGSMODULE variable may look like this:
.settings.production PYTHONPATH: c:\inetpub\wwwroot<projectname>
WSGIHANDLER: django.core.wsgi.getwsgiapplication()Additional Environment variables
If you are storing secrets in environment variables (such as secret keys and database credentials) as recommended by Two Scoops of Django then you would add them here as well. Please note that you’ll still need to set these additional environment variables at the console to run manage.py commands.
Click OK to close the Environment Variables Collection Editor dialog box.
Click OK to close the Edit FastCGI application dialog box.
Browse to the site and enjoy.
Serving Static Files
You must configure IIS to host the static files.
Right click the web site and choose Add Virtual Directory
Configure the following settings:
Alias - This will be the name specified for STATICURL in your project’s settings file.
Physical Path - This will be name specified for STATICROOT in your project’s setting file.Click ok to close the Add Virtual Directory dialog box
Select the newly created virtual directory
Open Handler Mappings
Click View Ordered List (located in actions pane on the right side)
Select StaticFile and click Move Up until the entry is at the top of the list.
Click Yes on the Handler Mappings warning dialog box informing you that changing made at the parent level will no longer be inherited at this level.
Serving Media Files
You must configure IIS to host the media files.
Right click the web site and choose Add Virtual Directory
Configure the following settings:
Alias - This will be the name specified for MEDIAURL in your project’s settings file.
Physical Path - This will be name specified for MEDIAROOT in your project’s setting file.Click ok to close the Add Virtual Directory dialog box
Select the newly created virtual directory
Open Handler Mappings
Click View Ordered List (located in actions pane on the right side)
Select StaticFile and click Move Up until the entry is at the top of the list.
Click Yes on the Handler Mappings warning dialog box informing you that changing made at the parent level will no longer be inherited at this level.