Skip to content

Deploying Python Applications with IIS on Windows

This guide explains how to deploy Python web applications on Microsoft Internet Information Services (IIS).

1. Introduction

IIS (Internet Information Services) is Microsoft's web server for Windows systems. With proper configuration, it can host Python web applications efficiently in Windows environments.

2. Prerequisites

  • Windows Server (2016/2019/2022) or Windows 10/11 with IIS installed
  • Python 3.8+ installed
  • A Python web application (Flask/Django/FastAPI)
  • Administrator access

3. Installing Required Components

Install IIS

If IIS is not already installed:

  1. Open the "Control Panel"
  2. Go to "Programs" → "Programs and Features" → "Turn Windows features on or off"
  3. Check "Internet Information Services" and expand it
  4. Ensure the following components are selected:
    • Web Management Tools → IIS Management Console
    • World Wide Web Services → Application Development Features → CGI

Install the Required Python Components

powershell
# Open PowerShell as Administrator
pip install wfastcgi

4. Setting Up Your Python Application

Create a Sample Flask Application

powershell
# Create application directory
mkdir C:\inetpub\wwwroot\myapp
cd C:\inetpub\wwwroot\myapp

# Create virtual environment
python -m venv venv
.\venv\Scripts\activate

# Install Flask
pip install flask

Create a sample application (app.py):

python
from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
    return "Hello from Python running on IIS!"

if __name__ == "__main__":
    app.run()

Create a web.config file for IIS:

xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <add name="PythonHandler" path="*" verb="*" modules="FastCgiModule" scriptProcessor="C:\inetpub\wwwroot\myapp\venv\Scripts\python.exe|C:\inetpub\wwwroot\myapp\venv\Lib\site-packages\wfastcgi.py" resourceType="Unspecified" requireAccess="Script" />
    </handlers>
    <fastCgi>
      <application fullPath="C:\inetpub\wwwroot\myapp\venv\Scripts\python.exe" arguments="C:\inetpub\wwwroot\myapp\venv\Lib\site-packages\wfastcgi.py" maxInstances="4" idleTimeout="1800" />
    </fastCgi>
  </system.webServer>
  <appSettings>
    <add key="WSGI_HANDLER" value="app.app" />
    <add key="PYTHONPATH" value="C:\inetpub\wwwroot\myapp" />
    <add key="WSGI_LOG" value="C:\inetpub\wwwroot\myapp\logs\wfastcgi.log" />
  </appSettings>
</configuration>

Create a logs directory:

powershell
mkdir logs

5. Configuring WFastCGI

Register the FastCGI module:

powershell
# Enable wfastcgi
wfastcgi-enable

Note the path output by the command, which should match what's in your web.config file.

6. Setting Up an IIS Website

Using IIS Manager (GUI Method)

  1. Open "IIS Manager"
  2. Right-click on "Sites" → "Add Website"
  3. Enter the following information:
    • Site name: MyPythonApp
    • Physical path: C:\inetpub\wwwroot\myapp
    • Binding: Choose port (e.g., 80 or any available port)
  4. Click "OK"

Using PowerShell (Command Line Method)

powershell
Import-Module WebAdministration
New-Website -Name "MyPythonApp" -Port 80 -PhysicalPath "C:\inetpub\wwwroot\myapp" -ApplicationPool "DefaultAppPool"

7. Configuring Application Pool

  1. Open "IIS Manager"
  2. Go to "Application Pools"
  3. Right-click on the application pool used by your site → "Advanced Settings"
  4. Set "Process Model" → "Identity" to an account with appropriate permissions

Alternatively, using PowerShell:

powershell
# Create a dedicated Application Pool
New-WebAppPool -Name "PythonAppPool"
Set-ItemProperty -Path "IIS:\AppPools\PythonAppPool" -Name "managedRuntimeVersion" -Value ""
Set-ItemProperty -Path "IIS:\AppPools\PythonAppPool" -Name "processModel.identityType" -Value "ApplicationPoolIdentity"

# Assign the Application Pool to your site
Set-ItemProperty -Path "IIS:\Sites\MyPythonApp" -Name "applicationPool" -Value "PythonAppPool"

8. Setting Permissions

powershell
# Grant permissions to the application pool identity
$ACL = Get-ACL "C:\inetpub\wwwroot\myapp"
$AccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule("IIS AppPool\PythonAppPool", "ReadAndExecute", "ContainerInherit,ObjectInherit", "None", "Allow")
$ACL.SetAccessRule($AccessRule)
Set-ACL "C:\inetpub\wwwroot\myapp" $ACL

9. Testing Your Application

Open a web browser and navigate to:

  • http://localhost (if you used port 80)
  • http://localhost:port (if you used a different port)

10. Configuring SSL/HTTPS

Generate or Import Certificate

  1. In IIS Manager, select the server node
  2. Double-click "Server Certificates"
  3. In the Actions pane, click "Create Self-Signed Certificate" (for testing) or import an existing certificate

Add HTTPS Binding

  1. Select your website in IIS Manager
  2. In the Actions pane, click "Bindings"
  3. Click "Add" and set:
    • Type: https
    • IP address: All Unassigned
    • Port: 443
    • SSL Certificate: Select your certificate
  4. Click "OK"

11. Django-Specific Configuration

For Django applications, update your web.config:

xml
<configuration>
  <system.webServer>
    <!-- Handler configuration -->

    <!-- Static files -->
    <staticContent>
      <mimeMap fileExtension=".woff" mimeType="application/font-woff" />
      <mimeMap fileExtension=".woff2" mimeType="application/font-woff2" />
    </staticContent>
  </system.webServer>
  <appSettings>
    <add key="WSGI_HANDLER" value="django.core.wsgi.get_wsgi_application()" />
    <add key="PYTHONPATH" value="C:\inetpub\wwwroot\myapp" />
    <add key="DJANGO_SETTINGS_MODULE" value="myproject.settings" />
  </appSettings>
</configuration>

12. Troubleshooting

Check Logs

  • IIS logs: %SystemDrive%\inetpub\logs\LogFiles
  • FastCGI logs: C:\inetpub\wwwroot\myapp\logs\wfastcgi.log
  • Event Viewer: Windows Logs → Application

Common Issues

500 Internal Server Error

  • Check the FastCGI log for Python errors
  • Verify paths in web.config
  • Ensure permissions are set correctly

404 Not Found

  • Verify the site binding is correct
  • Check that the physical path is correct
  • Ensure the URL you're accessing matches a route in your app

Module Not Found Errors

  • Verify all Python packages are installed in the virtual environment
  • Check PYTHONPATH in web.config

13. Performance Optimization

Application Pool Recycling

  1. Open IIS Manager
  2. Select Application Pools
  3. Right-click on your application pool → Advanced Settings
  4. Adjust "Recycling" settings based on your requirements

Output Caching

Add to your web.config:

xml
<system.webServer>
  <caching>
    <profiles>
      <add extension=".png" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" />
      <add extension=".js" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" />
      <add extension=".css" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" />
    </profiles>
  </caching>
</system.webServer>

Conclusion

You've successfully deployed a Python application on IIS. This approach allows you to leverage Windows Server infrastructure while running Python web applications. For production environments, consider implementing additional security measures and monitoring solutions.