How do I use Flask routes with Apache and mod_wsgi? How do I use Flask routes with Apache and mod_wsgi? flask flask

How do I use Flask routes with Apache and mod_wsgi?


In your wsgi file you are doing from service import application, which is importing only your application method.

Change that to from service import app as application and everything will work as expected.

After your comment, I thought I'd expand the answer a bit:

Your wsgi file is python code - you can have any valid python code inside this file. The wsgi "handler" that is installed in Apache is looking for the application name in this file, which it will hand off requests to. A Flask class instance - app = Flask(__name__) - provides such an interface, but since its called app and not application, you have to alias it when you import it - that's what the from line does.

You could - and this is perfectly fine - simply do this application = Flask(__name__) and then point the wsgi handler in Apache to your service.py file. If service.py was importable (that means, somewhere in PYTHONPATH), you wouldn't need an intermediary wsgi script.

Although the above works, its bad practice. The wsgi file needs permissions from the Apache process to work; and you generally separate that from the actual source code which should be somewhere else on your filesystem, with appropriate permissions.