Getting 500 Internal Server Error while setting up Python and Flask using FastCgiModule on Windows

Anyone who can configure Python using IIS? Tried to figure it out, but it doesn't work, and it drives me crazy. I see many examples, but I cannot get it to work.


Here is my setup


  • Windows 2008R2
  • IIS 7.5 (7.5.7600.16385)
  • wfastcgi.py (2.2.0)
  • Flask (0.10.1)
  • Python (3.5.1)

Python + Flask Web Application Path

C:\inetpub\wwwroot 

Below are the steps that I followed:

  • Verify that the CGI windows feature is installed.
  • An application with the following values ​​has been added in the IIS manager of the dedicated web server and in the FastCGI settings:

     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 display settings of the handler for the website, a mapping of modules for FastCgiModule has been added:

     Request path: * Module: FastCgiModule Executable: C:\python35\python.exe|C:\inetpub\wwwroot\wfastcgi.py Name: FlaskHandler 

A Simple Python application using Flask found on the Internet and added it to the root folder on the Internet. So, in C: \ inetpub \ wwww there are only 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:

 <add name="FastCgiModule" image="%windir%\System32\inetsrv\iisfcgi.dll" /> 

module section:

 <add name="FastCgiModule" lockItem="true" /> 

system web server 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 for some reason this is not working properly.

This is what I experience:

 HTTP Error 500.0 - Internal Server Error C:\python35\python.exe - The FastCGI process exited unexpectedly 

What I tried.

The handler script for the handler does not seem to be called correctly. It seems to be called, though, because when I change the value to something else, I get an error message:

 HTTP Error 500.0 - Internal Server Error <handler> scriptProcessor could not be found in <fastCGI> application configuration 

I thought it was an access issue, so I made sure the NETWORK SERVICE account has Read & Execute. I even gave each account full control. I believe NETWORK SERVICE is an account that requires permission.

I added registration to exit debugging information in app.py. I did not receive registration information in order to be able to write to the log file. I'm not sure how wfastcgi.py uses IIS configurations, even if I added it to web.config to configure the location of the log file. Perhaps if the Processor script is actually called, it will eventually configure it. But looking at the code, it seems to use a system environment variable, so I just added the value WSGI_LOG as the OS environment variable. Still does not log debugging information in the log file.

So, I tried to run the python executable with the wfastcgi file as an argument right on the command line and it seems to work as expected by entering a while loop while waiting and waiting for the request. And it also logs debugging information in the log file.

 python.exe c:\inetpub\wwwroot\wfastcgi.py 

And when I start the python server, it works when I go to localhost: 8081 in the browser, so I suspect this is a configuration issue.

 python app.py runserver 0.0.0.0:8081 

So what's going on. I have no ideas. Any help would be appreciated.

Thanks! It drives me crazy.

+6
source share
2 answers

Based on the description, it looks like you need some configuration changes in IIS. I have configuration settings in IIS 7.5 and running Python Flask with FastCgiModule. Here are the steps you can take:

  • Open IIS Manager . In the left pane, click Server Name. Then, in the middle pane, double-click FastCGI Settings. You need to click ActionsAdd from the right panel and have two settings. I have Python 2.7, for your case you need to enter a path for Python 3.5

enter image description here

  1. In IIS "Add Website" to publish the website . For the screenshot below, the site name is "pyprojects" and the content is published in the physical folder "C: \ inetpub \ wwwroot \ pyprojects"

enter image description here

  1. Select the file name on the left and double-click on the handler mappings. In the right pane, under "Actions", click "Add Mapping Modules" and enter

    • Request path: handler.fcgi
    • Module: FastCgiModule
    • Executable file: C:\Python27\python.exe|C:\Python27\Scripts\wfastcgi.py (path to the version for Python)
    • Name: Python FastCGI

enter image description here

  1. Select the file name on the left again. Now double-click Application Settings. For existing %ROOTDIR% configurations, you must replace the deployment directory for PYTHONPATH and WSGI_ALT_VIRTUALENV_ACTIVATE_THIS .

enter image description here

  1. View website

Hope this helps you and save time for those who face the same problem in the future.

+1
source

I know this question is several months old, but I think I need to share my solution. It really could help someone get stuck in the same situation that I could not find a single person with this problem. We were two inches closer to abandoning the alternative server manager, as I spent no less than a full day working on this problem, crawling the entire Internet to find an explanation or any hint of a working solution.

It turned out that the input I came across was:

Executable file (optional): C:\Python27\python.exe | C:\Python27\Scripts\wfastcgi.py C:\Python27\python.exe | C:\Python27\Scripts\wfastcgi.py

What this means was to delete the interval around the paths in the "Executable (optional)" field. I deeply believed that such a field, which carries two inputs, was kind enough to cut the analyzed values, so I did the same distance every time. But I was wrong, and my resulting configuration was matching with an empty game path with a space in front of it, etc. After removing all the spaces that are very inconvenient for me to watch, it worked as expected, without even deleting the handler and not returning it back. This type of deletion and retries on IIS forums pollute developers' thoughts with the false belief that everything needs to be undone when really good configurations are just needed.

Working input:

Executable file (optional): C:\Python27\python.exe|C:\Python27\Scripts\wfastcgi.py

I hope this helps someone, and if the MS developer gets here by accident, delete this evil double-entry hack field, since after that it is divided into two different fields if you go into FastCgi settings on a global level and it just misleading to specify two inputs in one field in another place. At the very least, trim both values, so that deep input list entry practices no longer run into this problem! It is extremely perplexing when a mistake says that basically what you just entered does not exist.

Update:

It should be mentioned that in the end you should open the application object inside the python module for IIS and nothing more. This may seem obvious to some people, but it is not very well documented, and it will work if the python module actually runs the application on its side. It is absolutely necessary to have a working development environment:

Python - app.py

 # <imports...> # <routes...> myApp.secret_key = 'this-is-a-secret-key' if __name__ == '__main__': # start application in python # IIS will get access to variable `myApp` and start it himself, # without starting it twice because IIS won't run the script # as `__main__`. In this scenario `WSGI_HANDLER` will be required # to have `app.myApp` to reach your application. 

Your application inside IIS will do its job. And you can still run it in debug mode using python app.py

+1
source

All Articles