1

I've hit a wall deploying a small Flask app to Azure. The app runs fine locally and on Heroku, but returns an internal server error on Azure. Here's the log:

logs.txt

StdErr: 
2018-06-15 22:14:38.239395: Unhandled exception in wfastcgi.py: Traceback (most recent call last):
  File "D:\home\python353x86\wfastcgi.py", line 791, in main
    env, handler = read_wsgi_handler(response.physical_path)
  File "D:\home\python353x86\wfastcgi.py", line 633, in read_wsgi_handler
    handler = get_wsgi_handler(os.getenv("WSGI_HANDLER"))
  File "D:\home\python353x86\wfastcgi.py", line 603, in get_wsgi_handler
    handler = getattr(handler, name)
AttributeError: module 'app' has no attribute 'wsgi_app'
2018-06-15 22:14:38.255034: Running on_exit tasks
2018-06-15 22:14:38.270645: wfastcgi.py 3.0.0 closed

It appears to be a problem with the WSGI configuration. This is my first Azure deployment and I've never had to think much about WSGI before. Per a tutorial, I was using Green Unicorn on Heroku, where it was referenced in requirements.txt and in the Procfile:

Procfile

web: gunicorn app:app

requirements.txt

Flask==0.12.2
Flask-Cors==3.0.3
gunicorn==19.7.1

However Azure doesn't use a Procfile, and it's not clear to me whether gunicorn would even work in an Azure environment.

My questions are:

  1. Is Green Unicorn viable? If so, how do I configure it?
  2. If not, what are my other options? Per posts like this, I've tried adding a wsgi_app = app.wsgi_app line beneath the app declaration but that returns an error that leads me down yet another rabbit hole: TypeError: wsgi_app() missing 2 required positional arguments: 'environ' and 'start_response'

Here's the rest of the backend:

app.py

# Dependencies ------------------------
from flask import Flask
from flask_cors import CORS, cross_origin
from flask import render_template
from flask import request
import sqlite3
import json
import os

# Config ------------------------------
app = Flask(__name__)
CORS(app)

# Routes ------------------------------
@app.route("/")
def home():
    # Do stuff

web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="WSGI_HANDLER" value="app.wsgi_app()" />
    <add key="PYTHONPATH" value="D:\home\site\wwwroot" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
    <handlers>
  <add name="Python FastCGI" path="handler.fcgi" verb="*"     modules="FastCgiModule" 
      scriptProcessor="D:\home\python353x86\python.exe|    D:\home\python353x86\wfastcgi.py" 
          resourceType="Unspecified" requireAccess="Script" />
    </handlers>
    <rewrite>
      <rules>
        <rule name="Configure Python" stopProcessing="true">
          <match url="(.*)" ignoreCase="false" />
          <conditions>
        <add input="{REQUEST_URI}" pattern="^/static/.*"     ignoreCase="true" negate="true" />
          </conditions>
      <action type="Rewrite" url="handler.fcgi/{R:1}"     appendQueryString="true" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

At this point I'm just blindly copy/pasting possible solutions, so I'd really appreciate any help. Thanks.

DJK
  • 175
  • 8

1 Answers1

2

Try defining wsgi_app:

app = Flask(__name__)

# define for IIS module registration.
wsgi_app = app.wsgi_app

if __name__ == '__main__':
    app.run()
George Sibble
  • 173
  • 1
  • 5