Karush Logo

Running a Django Website on IIS

Running a Python Django Website on Windows Server with IIS

This post will summarize how to run a website created using Python/Django on Windows Server with IIS.

The Problem

It would be more common to run a Python/Django website on a Linux server, but I wanted to see if I could make this work on Windows Server with IIS.

The Solution

I did this recently using Windows Server 2022, Python 3.14, and Django 6+.

Prerequisites (not explained in detail here): Create a Windows Server 2022 VM and install IIS, using the standard "add server role" process. Install Python from python.org. Install Django (pip install django). (note: the python installs can be done in a virtual environment, instead of globally)

Next, install HttpPlatformHandler from Microsoft. This is the recommended approach from Microsoft, as FastCGI is deprecated. HttpPlatformHandler acts as a proxy between IIS and Python.

Then, install uvicorn (pip install uvicorn). Uvicorn is an ASGI (Asynchronous Server Gateway Interface) web server implementation for Python - basically a runtime environment for the Python website. This is used instead of "python manage.py runserver", which is what you used in development.

A Django website is a series of nested folders. For example:

  • myproject (folder)
    • web.config (file - we'll make this later)
    • mysite (folder - this is considered the "root" folder, since it contains manage.py)
      • manage.py (file)
      • mysite (folder - contains settings.py, urls.py, etc.)
      • logs (folder)

Copy this folder structure from your development environment to your Windows Server. In IIS, do "Add Website..." and point the Physical path value to the myproject folder. Fill out the site name and select IP address as appropriate and click Ok.

You need to create the "web.config" file above, and put it in the myproject folder. The contents should be as shown in Appendix A below. In the XML, replace the text "myproject" and "mysite" with your actual values per your Django project. You can also create the web.config using the IIS GUI, but it's easier to just create the text file.

Important! IIS will ignore your web.config file unless you do the following: In IIS, double click on the server node (not your website) and then double click on Configuration Editor. In Configuration Editor under Section, drill down to system.webServer and click on "handlers". Then on the right Actions panel under Section, click "Unlock Section". This will allow your web.config file to override the values set at the server level.

Restart IIS by entering the command "iisreset" in powershell. Note that anytime you make a configuration change to the website in IIS, web.config, or to the Python files in your project, you need to do "iisreset" for the changes to appear. Make sure you see the message "Internet services successfully restarted". You will be doing this frequently as you initially deploy/configure/test your website.

Now, go to the URL or IP address of your website and you will see initial page of your website.

APPENDIX A

<?xml version="1.0" encoding="UTF-8"?>
<configuration><system.webServer><httpPlatform processPath="C:\Python314\python.exe" arguments="-m uvicorn mysite.asgi:application --host 127.0.0.1 --port %HTTP_PLATFORM_PORT%" stdoutLogEnabled="true" stdoutLogFile="C:\DATA\myproject\mysite\logs\"><environmentVariables><environmentVariable name="SERVER_PORT" value="%HTTP_PLATFORM_PORT%" /><environmentVariable name="DJANGO_SETTINGS_MODULE" value="mysite.settings" /><environmentVariable name="PYTHONPATH" value="C:\DATA\myproject\mysite" /><environmentVariable name="DJANGO_DEBUG" value="false" /></environmentVariables></httpPlatform><handlers><add name="MyPyHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified" /></handlers></system.webServer><appSettings></appSettings>
</configuration>

(apologize that the markdown engine doesn't preserve formatting)

Other Tips

  1. Before trying to get this all to work with a complex Django site, just get it to work with a simple, default Django site (example: "python -m django startproject mysite")

  2. In your settings.py file, you will want to add values to the ALLOWED_HOSTS:

ALLOWED_HOSTS = ['172.9.9.9',    <--- your server IP address
'mydomain.com',
'www.mydomain.com',
]
  1. In your settings.py file, make sure to set DEBUG = False, or add logic to dynamically set the value based on the environment the code is running in:
# DEBUG = True  <--- set this to False in production
DEBUG = os.environ.get("DJANGO_DEBUG", "false").lower() == "true"  <--- or use this, and then set an Environment variable on the server

Conclusion

It is definitely possible to run a Python/Django website on Windows Server 2022 with IIS and HttpPlatformHandler using the steps described above.