Skip to content

Instantly share code, notes, and snippets.

@bparaj
Last active December 1, 2023 05:42
Show Gist options
  • Select an option

  • Save bparaj/ac8dd5c35a15a7633a268e668f4d2c94 to your computer and use it in GitHub Desktop.

Select an option

Save bparaj/ac8dd5c35a15a7633a268e668f4d2c94 to your computer and use it in GitHub Desktop.
Python Flask on IIS with wfastcgi
Assume IIS is installed. My machine already had IIs 8.5.
Install Python
==============
1. Download web installer (Python 3.6.3).
2. Run as Administrator.
3. Select custom installation for all users.
4. Choose install directory such that there are no white spaces in the path. Not sure if it needs to be done. Just being cautious.
5. Check the box for "Add Python 3.6 to PATH".
Install wfastcgi and others
===========================
1. Open Windows Powershell as Adminstrator.
2. Run: pip install wfastcgi
3. Run: pip install flask
Setting up Website
==================
1. On the powershell, run: wfastcgi-enable
It will produce configuration related output. Example:
"""
Applied configuration changes to section "system.webserver/fastcgi" for "MACHINE/WEBROOT/APPHOST" at configuration commit path "MACHINE/WEBROOT/APPHOST".
"c:\python36\python.exe|c:\python36\lib\site-packages\wfastcgi.py" can now be used as a FastCGI script processor.
"""
2. Run IIS as administrator.
3. Go to Connections and expand the tree.
4. Select "Sites".
5. Select "Add Website" under Actions panel on the right of the window.
6. A new window will pop up titled "Add Website". Fill in the necessary info: Site name, Directory containing the website content, IP address and port (I entered 5000).
Since I want to simply run it on local host, IP address can be left as "All unassigned".
7. The Physical Path you specified in Add Website contains the following files (barebones):
a. web.config: contains web configuration. It has the following content:
"""
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<handlers>
<remove name="Python27_via_FastCGI" />
<remove name="Python34_via_FastCGI" />
<add name="Python FastCGI"
path="*"
verb="*"
modules="FastCgiModule"
scriptProcessor="C:\Python36\python.exe|C:\Python36\Lib\site-packages\wfastcgi.py"
resourceType="Unspecified"
requireAccess="Script" />
</handlers>
</system.webServer>
<appSettings>
<!-- Required settings -->
<add key="WSGI_HANDLER" value="myapp.app" />
<add key="PYTHONPATH" value="C:\inetpub\wwwroot\stealth" />
</appSettings>
</configuration>
"""
b. myapp.py: contains Flask applicatioh
"""
from flask import Flask
app = Flask(__name__)
@app.route("/hello")
def hello():
return "Hello Stealth!"
"""
8. You might have to restart the Server and the website after configuration changes. Option will be under Actions on the right.
8. If you select the root node, you'll see a bunch of configuration features. We are interested in FastCGI Settings and Handler Mappings.
a. Under FastCGI settings, I have the following:
"""
Full Path | Arguments
c:\python36\python.exe | c:\python36\lib\site-packages\wfastcgi.py
c:\Program Files\PHP\php-cgi.exe
"""
b. Under Handler Mappings, you'll see different names. Based on web.config, you'll see "Python FastCGI".
10. You can now enter "localhost:5000" into the browser.
@abescott

Copy link
Copy Markdown

@boffti, Can you please share how did you fix that? I got the same problem. Thank you

I have the same issue- can you share solution?

@dharmeshsharma

dharmeshsharma commented Nov 7, 2019

Copy link
Copy Markdown

Hello, Sitll im getting HTTP Error 403.14 - Forbidden
The Web server is configured to not list the contents of this directory.

<handlers> <add name="PythonHandler" path="*" verb="*" modules="FastCgiModule" scriptProcessor="C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\python.exe|C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\Scripts\wfastcgi.py" resourceType="Unspecified" requireAccess="Script"/> </handlers>

app setting

<add key="PYTHONPATH" value="C:\Inetpub\vhosts\dharmeshsharma.com\pythontest.dharmeshsharma.com"/> <add key="WSGI_HANDLER" value="PythonExt.app"/> <add key="WSGI_LOG" value="c:\home\LogFiles\wfastcgi.log"/>

@ezekumar

Copy link
Copy Markdown

Error occurred while reading WSGI handler: Traceback (most recent call last):File "c:\anaconda3\lib\site-packages\wfastcgi.py", line 791, in mainenv, handler = read_wsgi_handler(response.physical_path)File "c:\anaconda3\lib\site-packages\wfastcgi.py", line 633, in read_wsgi_handlerhandler = get_wsgi_handler(os.getenv("WSGI_HANDLER"))File "c:\anaconda3\lib\site-packages\wfastcgi.py", line 616, in get_wsgi_handlerraise ValueError('"%s" could not be imported%s' % (handler_name, last_tb)) ValueError: "main.app()" could not be imported: Traceback (most recent call last):File "c:\anaconda3\lib\site-packages\wfastcgi.py", line 600, in get_wsgi_handlerhandler = import(module_name, fromlist=[name_list[0][0]])File ".\main.py", line 15, in import nltkFile "c:\anaconda3\lib\site-packages\nltk_init_.py", line 149, in from nltk.translate import *File "c:\anaconda3\lib\site-packages\nltk\translate_init_.py", line 23, in from nltk.translate.meteor_score import meteor_score as meteorFile "c:\anaconda3\lib\site-packages\nltk\translate\meteor_score.py", line 10, in from nltk.stem.porter import PorterStemmerFile "c:\anaconda3\lib\site-packages\nltk\stem_init_.py", line 29, in from nltk.stem.snowball import SnowballStemmerFile "c:\anaconda3\lib\site-packages\nltk\stem\snowball.py", line 29, in from nltk.corpus import stopwordsFile "c:\anaconda3\lib\site-packages\nltk\corpus_init_.py", line 66, in from nltk.corpus.reader import *File "c:\anaconda3\lib\site-packages\nltk\corpus\reader_init_.py", line 105, in from nltk.corpus.reader.panlex_lite import *File "c:\anaconda3\lib\site-packages\nltk\corpus\reader\panlex_lite.py", line 15, in import sqlite3File "c:\anaconda3\lib\sqlite3_init_.py", line 23, in from sqlite3.dbapi2 import *File "c:\anaconda3\lib\sqlite3\dbapi2.py", line 27, in from _sqlite3 import * ImportError: DLL load failed while importing _sqlite3: The specified module could not be found. StdOut: importing libraries... StdErr:

not able to install Gensim & NLTK in wfastcgi environment

@Udayaprasad

Copy link
Copy Markdown

Hello, Sitll im getting HTTP Error 403.14 - Forbidden The Web server is configured to not list the contents of this directory.

<handlers> <add name="PythonHandler" path="*" verb="*" modules="FastCgiModule" scriptProcessor="C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\python.exe|C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\Scripts\wfastcgi.py" resourceType="Unspecified" requireAccess="Script"/> </handlers>

app setting

<add key="PYTHONPATH" value="C:\Inetpub\vhosts\dharmeshsharma.com\pythontest.dharmeshsharma.com"/> <add key="WSGI_HANDLER" value="PythonExt.app"/> <add key="WSGI_LOG" value="c:\home\LogFiles\wfastcgi.log"/>

Were you successful in configuring the flask app in the python 3.8 environment? Please help me how to configure for python 3.8

@Udayaprasad

Copy link
Copy Markdown

Great tutorial. Thanks so much! However I am getting HTTP 500 error with " scriptProcessor could not be found in application configuration".
Is this a Python version issue? My Windows server is 2012 R2 running Python 3.7.1.

::EDIT:: Had to tweak the config file a bit to point to the right python dist. working great now! Thank you so much!

Were you successful in configuring the flask app in the python 3.8 environment? Please help me how to configure for python 3.8

@dharmeshsharma

Copy link
Copy Markdown

Hello, Sitll im getting HTTP Error 403.14 - Forbidden The Web server is configured to not list the contents of this directory.
<handlers> <add name="PythonHandler" path="*" verb="*" modules="FastCgiModule" scriptProcessor="C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\python.exe|C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\Scripts\wfastcgi.py" resourceType="Unspecified" requireAccess="Script"/> </handlers>
app setting
<add key="PYTHONPATH" value="C:\Inetpub\vhosts\dharmeshsharma.com\pythontest.dharmeshsharma.com"/> <add key="WSGI_HANDLER" value="PythonExt.app"/> <add key="WSGI_LOG" value="c:\home\LogFiles\wfastcgi.log"/>

Were you successful in configuring the flask app in the python 3.8 environment? Please help me how to configure for python 3.8

Yes i have successfully configured

@rvg296

rvg296 commented Nov 3, 2020

Copy link
Copy Markdown

Great tutorial. Thanks so much! However I am getting HTTP 500 error with " scriptProcessor could not be found in application configuration".
Is this a Python version issue? My Windows server is 2012 R2 running Python 3.7.1.

::EDIT:: Had to tweak the config file a bit to point to the right python dist. working great now! Thank you so much!

Will this work for Python 3.6 version and Windows Server 2016, IIS Version 10.0 ?

@rogerperkins

Copy link
Copy Markdown

Do I have to create a new website for every flask application? What if I just want to run some regular python script through the fastcgi module?

@rvsmann

rvsmann commented Apr 1, 2021

Copy link
Copy Markdown

I always get this error:
AppData\Local\Programs\Python\Python36\python.exe - The FastCGI process exited unexpectedly

Do you know why?

Check your python default installation directory, if the installation directory has any space inside it, you will get this error. Reinstalling it to a new directory without space fixes the issue.
Wrong Installation path : "C:\Program Files\python"
Correct Installation path : "C\python"

@yatesconsulting

yatesconsulting commented Jul 22, 2021

Copy link
Copy Markdown

If you must use spaces in your filename paths, you can use this notation to fix some errors (note the pair of &quot;s ):
<add name="Python FastCGI" path="" verb="" modules="FastCgiModule" scriptProcessor="c:\program files\python38\python.exe|&quot;c:\program files\python38\lib\site-packages\wfastcgi.py&quot;"

@rvsmann

rvsmann commented Jul 23, 2021 via email

Copy link
Copy Markdown

@DanielJBrown

Copy link
Copy Markdown

Is there a guide for installing wfastcgi within a DMZ zone? The lack of being able to pip make my life hard.

@rvsmann

rvsmann commented Sep 24, 2021 via email

Copy link
Copy Markdown

@DanielJBrown

Copy link
Copy Markdown

We have a development environment which has no restrictions.
So first in DMZ installed the pyhton 3.9. then I zipped up the entire python folder from dev so I got the scripts and site-packages.
Then could run wfastcgi.enable from cmd line to add in the fastcgi settings into the root of the IIS server.
I used this guide to do the Handler Mappings and its environment variables.
https://medium.com/@dpralay07/deploy-a-python-flask-application-in-iis-server-and-run-on-machine-ip-address-ddb81df8edf3
I will give your steps a try for the production server though, thanks.

@rvsmann

rvsmann commented Sep 24, 2021 via email

Copy link
Copy Markdown

@rogerperkins

Copy link
Copy Markdown

cmd as admin:

set HTTP_PROXY=http://userid:password@your.proxy.domain:proxyport
set HTTPS_PROXY=http://userid:password@your.proxy.domain:proxyport

python -m pip install wfastcgi --trusted-host pypi.org --trusted-host files.pythonhosted.org --trusted-host pypi.python.org

python -m pip install flask --trusted-host pypi.org --trusted-host files.pythonhosted.org --trusted-host pypi.python.org

wfastcgi-enable

@diiimarins

diiimarins commented Mar 11, 2022

Copy link
Copy Markdown

Hiii!

I'm not sure if this is too late.. But.. The only missed Steps was the "Application Pool" and Authentication on IIS Admin. Final Step:

1 - Go to "Application Pools" and find the one that was created for the Site. Under "Advanced Settings" in the "Identity", click on the "..." to select a new one. In Build-In Account, select "Local System"

2 - Open your Site using the nav bar (left), and Change Authentication. Disable the "Windows Authentication" and Enable the "Anonimous Authentication)

I hope that this helps :)

ghost commented Sep 13, 2022

Copy link
Copy Markdown

Are there any alternatives to wfastcgi if we want to run Python > 3.6? I have dependencies that require 3.9.

@tommycarstensen

Copy link
Copy Markdown

Are there any alternatives to wfastcgi if we want to run Python > 3.6? I have dependencies that require 3.9.

I have the same question. Python 3.6 is no longer supported. Will this work with Python 3.10?

@hkcassie

hkcassie commented Jul 6, 2023

Copy link
Copy Markdown

I always get this error:
AppData\Local\Programs\Python\Python36\python.exe - The FastCGI process exited unexpectedly
Do you know why?

Check your python default installation directory, if the installation directory has any space inside it, you will get this error. Reinstalling it to a new directory without space fixes the issue. Wrong Installation path : "C:\Program Files\python" Correct Installation path : "C\python"

wow, thanks !! it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment