How do I use Flask routes with Apache and mod_wsgi?

﹥>﹥吖頭↗ 提交于 2019-12-02 20:43:42

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!