0

I've configured the basic Flask web sample in a VENV and now it is correctly published through IIS via FastCGI.

Well, here it is my trivial sample00.py

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello, World, from Flask!"

and the web.config, a little more involved:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
      <handlers>
        <add name="PythonHandler" path="*" verb="*" modules="FastCgiModule" 
         scriptProcessor="F:\Appl\flask\venv\Scripts\python.exe|F:\Appl\flask\venv\Lib\site-packages\wfastcgi.py" 
         resourceType="Unspecified" requireAccess="Script" />
      </handlers>
  </system.webServer>
    <appSettings>
      <add key="PYTHONPATH" value="F:\Appl\flask" />
      <!-- Flask apps only: change the project name to match your app -->
      <add key="WSGI_HANDLER" value="sample00.app" />
      <add key="WSGI_LOG" value="F:\Appl\flask\wfastcgi.log" />
    </appSettings>
</configuration>

After some trials and errors, I've discovered that I can enter in IIS Manager > Handler Mappings > select my "PythonHandler", edit the "Executable (optional)", and it checks if the path is correct and asks me to activate the handler, then I can answer "yes" and eventually it all works as expected.

So far so good. Now, I would like to setup it in IIS not as a separate website, but as a new application under another, existing, website. Is it even possible or is there a documentation saying that it is not permitted? We can close this question or mark it as duplicated, but till now I was not able to find a previous answer to this specific point. Anyway, I've tried to do that, but I receive a 404 page error... like if the request is not correctly forwarded to the cgi handler, hence I suspect there could be some problem in managing a Flask app at IIS application level, is it the case or what else am I doing wrong?

1 Answers1

2

The point is simply that, looking at the Basic Settings in IIS at the application level, see an image sample below,

enter image description here

the virtual path (/flask in the above case) is passed from IIS to the FastCGI handler, hence it must be managed in the Flask app, e.g. you can define the route as:

@app.route("/flask")
def hello():

and it will be correctly shown in your browser (notice that a final / would represent a different url though, so such a typo could also cause a 404 error).

The conculsion is that you absolutely can run the Flask FastCGI also at application level and not only at website level in IIS.

You also need to add the virtual path before the static file folder:

app = Flask(__name__, static_url_path='/flask/static')

Finally a suggestion: if you have a configuration like

app.config.from_object(Config())
if environ.get('FLASK_FREIGHT_SETTINGS') is not None:
 app.config.from_envvar('FLASK_FREIGHT_SETTINGS')

then iisreset /restart to make a new env var visible to IIS or - even better -

from dotenv import load_dotenv
load_dotenv()

use the python-dotenv module as above to read the environmental variables from an .env file, but I'd also enable the flag to monitor changes to file in fastcgi settings of IIS at server level, so that your flask application's python handle is reloaded when you update your app_settings.cfg (where I define a version number parameter).

  • Btw, see my follow-up answer [here](https://stackoverflow.com/a/65872279/11323942): if you want to set an environment variable, do that via the FastCGI Settings of IIS and not from the system control panel. –  Jan 24 '21 at 15:28