Flask, wfastcgi, and IIS on Windows Server 2012
We had a similar problem recently with IIS 7, Flask 0.12 and Python 3.6.4. Your web.config
looks good. Two recommendations:
- Use a
virtualenv
for your script processor. It avoids side-effects from using a system-level Python installation. It's easier to debug that way. - Double check in IIS that IIS_IUSRS and IUSR have modify permissions for the folders in your Python path.
Related videos on Youtube
twip
Updated on June 04, 2022Comments
-
twip almost 2 years
I am trying to deploy a flask service on IIS on Windows Server 2012. To arrive at this point:
- pip installed flask (Python versions 2.7 and 3 were already installed.)
- pip installed wfastcgi
- Ran wfastcgi-enable
- Established a new site under IIS
- Added a handler for wfastcgi
- Modified Web.config
Running from localhost returns the output I expect. However, when I visit the website from the sitename, the following error is returned (paths omitted):
Error occurred while reading WSGI handler: Traceback (most recent call last): File "wfastcgi.py", line 791, in main env, handler = read_wsgi_handler(response.physical_path) File "wfastcgi.py", line 633, in read_wsgi_handler handler = get_wsgi_handler(os.getenv("WSGI_HANDLER")) File "wfastcgi.py", line 586, in get_wsgi_handler raise Exception('WSGI_HANDLER env var must be set') Exception: WSGI_HANDLER env var must be set
This is the case whether on the server or from another machine on the domain. It seems as though when the app is requested from anything but localhost, the environment is unreachable. Nothing gets written to the wfastcgi log.
I have included
app.py
andWeb.config
below. I omitted thescriptProcessor
path here, but it is set to the value returned from wfastcgi-enable.When running from localhost, the environment is available. How do I make the environment available to the app when called beyond locahost?
app.py
from flask import Flask myapp = Flask(__name__) @myapp.route("/hello") def hello(): return "Hello from flask!" if __name__ == "__main__": myapp.run(port=8080)
Web.config
<?xml version="1.0" encoding="UTF-8"?> <configuration> <appSettings> <add key="WSGI_HANDLER" value="app.myapp" /> <add key="PYTHONPATH" value="c:/inetpub/wwwroot/flask-services/" /> <add key="WSGI_LOG" value="C:/TMP/logs/app.log" /> </appSettings> <system.webServer> <handlers> <add name="python-wfastcgi" path="*" verb="*" modules="FastCgiModule" scriptProcessor="[Omitted]" resourceType="Unspecified" requireAccess="Script" /> </handlers> </system.webServer> </configuration>
-
kflaw over 5 yearsI am getting this same error did you find the answer?
-
twip over 5 yearsIt was a user problem, just not with IIS: we needed a domain user to talk across the network to the DB server. :P
-
susodapop over 4 yearsI revised per Anjani's request.