Anyone able to set up Python with IIS? Been trying to figure it out, but it's not working and it's driving me crazy. I see plenty of examples but I can not get it to work.
Path of Python + Flask web app
C:\inetpub\wwwroot
Here are the steps I followed:
In the IIS Manager, highlighted web server and in FastCGI Settings added an application with the values:
Full Path: C:\python35\python.exe
Arguments: C:\inetpub\wwwroot\wfastcgi.py
Environment Variable Collection:
PYTHONPATH: C:\inetpub\wwwroot\
WSGI_HANDLER: app.app
WSGI_LOG: C:\logs\app.txt
Created a website called MyWebSite pointing to C:\inetpub\wwwroot
In the handler mapping settings for web site, added a module mapping for FastCgiModule:
Request path: *
Module: FastCgiModule
Executable: C:\python35\python.exe|C:\inetpub\wwwroot\wfastcgi.py
Name: FlaskHandler
Simple Python app using Flask found on the web and added it to the web root folder. So at C:\inetpub\wwww there are just 3 files: app.py, web.config, and wfastcgi.py
web.config
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appSettings>
<add key="PYTHONPATH" value="" />
<add key="WSGI_HANDLER" value="app.app" />
<add key="WSGI_RESTART_FILE_REGEX" value="(?i).*\.(py|cnf|config)$" />
</appSettings>
<system.webServer>
<handlers>
<add name="FlaskHandler" path="*" verb="*" modules="FastCgiModule" scriptProcessor="C:\python35\python.exe|C:\inetpub\wwwroot\wfastcgi.py" resourceType="Unspecified" requireAccess="Script" />
</handlers>
</system.webServer>
</configuration>
app.py
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello from FastCGI via IIS!"
if __name__ == "__main__":
app.run()
applicationHost.config:
global module section:
<add name="FastCgiModule" image="%windir%\System32\inetsrv\iisfcgi.dll" />
module section:
<add name="FastCgiModule" lockItem="true" />
system webserver section:
<section name="fastCgi" allowDefinition="AppHostOnly" overrideModeDefault="Deny" />
...
<fastCgi>
<application fullPath="C:\Python35\python.exe" arguments="C:\inetpub\wwwroot\wfastcgi.py">
<environmentVariables>
<environmentVariable name="PYTHONPATH" value="C:\inetpub\wwwroot\" />
<environmentVariable name="WSGI_HANDLER" value="app.app" />
<environmentVariable name="WSGI_LOG" value="c:\logs\my_log.txt" />
</environmentVariables>
</application>
</fastCgi>
So some reason it does not work as expected.
HTTP Error 500.0 - Internal Server Error
C:\python35\python.exe - The FastCGI process exited unexpectedly
The scriptProcessor for the handler doesn't seem to be called correctly. It does seem to be called though because when I change the value to something else I get an error:
HTTP Error 500.0 - Internal Server Error
<handler> scriptProcessor could not be found in <fastCGI> application configuration
I thought it was a permissions thing so i made sure NETWORK SERVICE account has Read&Execute. I even gave everyone account Full control. I believe NETWORK SERVICE is the account that needs permission, I think.
I added logging to log out debug information in app.py. I did not get any logging information to be able to write out to log file. I am not sure how wfastcgi.py uses the IIS configurations, even if I added it to the web.config, to set up the location of the log file. Maybe that if the scriptProcessor is actually getting called, it will set it up eventually. But looking at the code it seems to using system environment variable, so I just added WSGI_LOG value as a OS environment variable. Still not logging debug info to the log file.
So I tried, running the python executable with the wfastcgi file as the argument straight on the command line, and it seems to work as expected, entering the wfastcgi while loop and waiting for a request. And it also logs the debug info to the log file.
python.exe c:\inetpub\wwwroot\wfastcgi.py
And when I run the python server it works when I navigate to localhost:8081 in the browser, so I suspect it is a configuration thing.
python app.py runserver 0.0.0.0:8081
So what is going on. I'm out of ideas. Any help would be appreciated.
Thanks! This is driving me nuts.
Based on the description, looks like you need some configuration changes on IIS. I have below configuration settings on IIS 7.5 and running Python Flask with FastCgiModule. Here are the steps you can take:
Actions
>> Add
from the right pane and have two settings. I have Python 2.7, for your case you have to enter path for Python 3.5Select the sitename on left and double click on Handler Mappings. On right pane under Actions, click "Add Module Mapping" and enter
handler.fcgi
FastCgiModule
C:\Python27\python.exe|C:\Python27\Scripts\wfastcgi.py
(Python version specific path) Python FastCGI
%ROOTDIR%
need to be replaced with the deployment directory for PYTHONPATH
and WSGI_ALT_VIRTUALENV_ACTIVATE_THIS
. Hope it will help you and save some time for those who encounters the same issue in future.