How to Deploy Django with Uvicorn (ASGI) on Windows IIS

Django is a high-level web framework written in Python that encourages rapid development and clean pragmatic design. It's designed to help developers build web applications quickly and efficiently by providing a robust set of tools, libraries, and functionalities.

In my previous blog, I have shared on how to deploy Django application on Windows IIS. While the previous method focuses on deploying the application using WSGI (Web Server Gateway Interface), this method allows any Django application to run on Windows Server or Windows environment. However, today we would delve into using ASGI (Asynchronous Server Gateway Interface), a successor of WSGI.

But what are WSGI and ASGI? The difference between them and the usage behind it.

Let's find out!

Strolling into web application, Web Server Gateway Interface (WSGI) and Asynchronous Server Gateway Interface (ASGI) play a crucial role acting like a middle person, managing requests that come from the client (Web, other PC, server, mobile). According to Philip J. Eby, WSGI was originally defined as PEP333 in 2010. While WSGI provides a synchronous Python web application, however, the performance was not picking up. Hence, this is how we got ASGI in 2019 as the successor.

Living up to its name, ASGI would enable you to handle requests asynchronously. This gradually increase processing speed as your computer no longer need to undergo sequential processing.

Take a look at the table below to see the differences:

Web Server Gateway InterfaceAsynchronous Server Gateway Interface
Concurrency ModelSynchronousAsynchronous
Handling Asynchronous OpsSequential processingSupports asynchronous operation.
Use CasesTraditional Web ApplicationReal-time Application like Chat and Streaming
CompatibilityWidely adopted and matureEmerging and increasing adoption

Now I will demonstrate on how you can deploy Django application by using Uvicorn (ASGI) on Windows IIS.

  1. Install Django and start a new project.

  2. Create a virtual environment - python -m venv venv .

  3. Open command prompt in the directory where the venv folder is and enter.\venv\Scripts\activate to activate the virtual environment and pip install django, pip install uvicorn and pip install wfastcgi.

  4. Set ALLOWED_HOST to all by adding '*' in settings.py.

  5. Open Windows IIS and create a new site with the Django project as physical directory and remember the application pool name because you will need it to for security access to the folder later.

  6. Download and install HTTP Platform Handler from here.

  7. Set a module mapping in handler mapping of the site.

    Request path: *

    Module: httpPlatformHandler

    Executable(optional): [Python venv path]|[wfastcgi.py path], for example: D:\Tutorial\tutorial\venv\Scripts\python.exe|D:\Tutorial\tutorial\venv\Lib\site-packages\wfastcgi.py
    Name: HTTP Platform Handler
    Click on Request Restrictions... button and untick Invoke handler only if request is mapped to:

    Click OK.

    If you encounter an error that says '|' character is invalid. Remove this line |D:\Tutorial\tutorial\venv\Lib\site-packages\wfastcgi.py. And create a web.config file in the project directory.

    And copy the same scripts listed below and replace with web.config content:

     <?xml version="1.0" encoding="UTF-8"?>
     <configuration>
         <appSettings>
         <add key="WSGI_HANDLER" value="tutorial.asgi.application" />
         </appSettings>
         <system.webServer>
    
             <handlers>
                 <add name="HTTP Platform Handler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified" />
             </handlers>
             <httpPlatform requestTimeout="00:05:00" startupTimeLimit="120" startupRetryCount="3" stdoutLogEnabled="true" stdoutLogFile=".\logs\python-stdout" processPath="D:\Tutorial\tutorial\venv\Scripts\python.exe" arguments="-m uvicorn tutorial.asgi:application --port %HTTP_PLATFORM_PORT% --host 0.0.0.0">
                 <environmentVariables>
                     <environmentVariable name="PORT" value="%HTTP_PLATFORM_PORT%" />
                 </environmentVariables>
             </httpPlatform>
         </system.webServer>
     </configuration>
    
  8. This step is mandatory for IIS to access the web application. Click on Edit Permission in Windows IIS.

    Click on Add button.

    Key in the application pool name and click on Check Names to ensure IIS can find the application pool. And click OK.
    Tick the checkbox for Write and click Apply and OK.

  9. Navigate to python.exe folder in the computer/server and right click on the folder and repeat Step 8.

  10. Modify web.config in the project folder.
    <add key="WSGI_HANDLER" value="[your project name].asgi.application" />

    processPath="[your python.exe in venv folder path]"

    arguments="-m uvicorn [your project name].asgi:application --port %HTTP_PLATFORM_PORT% --host 0.0.0.0"

  11. Congratulations! You may now browse into the asynchronous web application.

References:
i. HTTP Platform Handler for IIS
ii. Stackoverflow - Hypercorn/Uvicorn for Python App on Windows IIS
iii. The differences between WSGI and ASGI
iv. Python ASGI on IIS
v. WSGI vs ASGI
vi. FastCGI Wikipedia

Share with me what are you building on my Linkedin.(it is not a must but please)