1

I'm looking for an understandable way of configuring a Flask application to run on Azure Web Apps. I'd like to be able to have the environment as close to the original one as possible, although I understand using a ready-made service brings some requirements.

What I appreciate about my local setup is:

  • An easy way to run a server with Waitress just by running a .py script
  • Having my files as an importable "development" package, providing a situation with no path manipulations but an arbitrary location in the file system (pip install -e ./)

It is very easy to set up an application and start it with Flask. This is my most simple app.py.

from flask import Flask

app = Flask(__name__)


@app.route('/')
def main():
    return 'Hi.'


if __name__ == '__main__':
    app.run('0.0.0.0', 5000)

Now there's this guide on how to get a Python website up in Azure. I think it has loads of unnecessary steps, downloading wheels to from a separate server, modifying os.path to import libraries and using a virtualenv. Don't get me wrong: virtualenv is useful but I'm just trying strip the process down to its bare minimum. Additionally, for me at least the virtualenv activation script does not work.

Another guide, this time by Microsoft themselves says that Windows developers should move to Azure Web App for Linux instead. But for the time being, I'm stuck with a Windows app. They provide some configuration assistance.

A Python installation was provided. It is strangely located in D:\home and its interpreter cannot be started using a simple python command. Installing libraries works though: python -m pip install lib. A configuration file should be constructed from some Visual Studio template which at least my Visual studio can't find. So what I have now filling in with the first guide is

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
  <handlers>
    <add name="PythonHandler" path="*" verb="*" modules="FastCgiModule"
        scriptProcessor="D:\home\Python364x64\python.exe|D:\home\Python364x64\wfastcgi.py"
        resourceType="Unspecified" requireAccess="Script"/>
  </handlers>
</system.webServer>
</configuration>

Another line should be added somewhere based on the doc. Although, it is unclear where exactly.

<add key="WSGI_HANDLER" value="FlaskAzurePublishExample.app"/>

So what I'd like to know is:

  • What are the minimum steps required to get this minimal application running?
  • Can I run a server of my own at all? How about several using different ports?
Felix
  • 2,548
  • 19
  • 48

1 Answers1

2

For your first question, you can refer to my answer for the other existing SO thread Hosting Flask(Python) app throws CGI error to run the minimal Flask application. And there is an issue in your code, which be started up by wfastcgi.py using app.run(), not to use app.run('<ip or host for listening>', '<a port you want>'). On Azure, IIS as HTTP server for Python to run a flask app via FastCGI.

So for your second question, you can run your own server behind IIS reverse proxy, but the only port on Azure Web Site is 80 on IIS for serving Python Web App via FastCGI.

Peter Pan
  • 23,476
  • 4
  • 25
  • 43