How do I use Flask routes with Apache and mod_wsgi?

后端 未结 1 1599
盖世英雄少女心
盖世英雄少女心 2021-02-02 14:15

I\'ve got my Apache server setup and it is handling Flask responses via mod_wsgi. I\'ve registered the WSGI script via the alias:

[httpd.conf]

WSGIScrip         


        
相关标签:
1条回答
  • 2021-02-02 14:50

    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.

    0 讨论(0)
提交回复
热议问题