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:
- Open the "Control Panel"
- Go to "Programs" → "Programs and Features" → "Turn Windows features on or off"
- Check "Internet Information Services" and expand it
- 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
# Open PowerShell as Administrator
pip install wfastcgi4. Setting Up Your Python Application
Create a Sample Flask Application
# 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 flaskCreate a sample application (app.py):
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 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:
mkdir logs5. Configuring WFastCGI
Register the FastCGI module:
# Enable wfastcgi
wfastcgi-enableNote 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)
- Open "IIS Manager"
- Right-click on "Sites" → "Add Website"
- Enter the following information:
- Site name: MyPythonApp
- Physical path: C:\inetpub\wwwroot\myapp
- Binding: Choose port (e.g., 80 or any available port)
- Click "OK"
Using PowerShell (Command Line Method)
Import-Module WebAdministration
New-Website -Name "MyPythonApp" -Port 80 -PhysicalPath "C:\inetpub\wwwroot\myapp" -ApplicationPool "DefaultAppPool"7. Configuring Application Pool
- Open "IIS Manager"
- Go to "Application Pools"
- Right-click on the application pool used by your site → "Advanced Settings"
- Set "Process Model" → "Identity" to an account with appropriate permissions
Alternatively, using 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
# 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" $ACL9. 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
- In IIS Manager, select the server node
- Double-click "Server Certificates"
- In the Actions pane, click "Create Self-Signed Certificate" (for testing) or import an existing certificate
Add HTTPS Binding
- Select your website in IIS Manager
- In the Actions pane, click "Bindings"
- Click "Add" and set:
- Type: https
- IP address: All Unassigned
- Port: 443
- SSL Certificate: Select your certificate
- Click "OK"
11. Django-Specific Configuration
For Django applications, update your web.config:
<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
- Open IIS Manager
- Select Application Pools
- Right-click on your application pool → Advanced Settings
- Adjust "Recycling" settings based on your requirements
Output Caching
Add to your web.config:
<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.