使用 mod_wsgi 在 Apache2 上部署 Python Falcon

Deploying Python Falcon on Apache2 with mod_wsgi

场景:我正在为我的 REST API 服务使用 Python Falcon 框架。该应用程序最初由 Gunicorn 运行。 现在,当我调用 https://example.com:8000/ 时,我得到了 API 逻辑指示的正确响应。

目标:我想将其部署到 Apache2 以及 mod_wsgi 我的个人开发服务器上。 API 仍将在 Gunicorn 下 运行ning,只要在该端点上发出请求,Apache 就应该与 Gunicorn 交互。

目录结构:

| - abc_service
        | - abc_service
                | - __init__.py
                | - app.py
        | - wsgi.py
        | - .abc_venv

app.py 的源代码如下所示:

import falcon
from .abc import ABC

api = application = falcon.API() # pylint: disable=invalid-name

# Creating a unique GET resource from the ABC class
test_handler_resource = ABC() # pylint: disable=invalid-name

# Mapping the HTTP GET endpoint with the unique resource
api.add_route('/abc', test_handler_resource)

# Mapping the HTTP POST endpoint with the unique resource
api.add_route('/abc/filters', test_handler_resource)

在我的wsgi.py中,我有以下内容:

from abc_service import application

/etc/apache2/sites-available/000-default.conf中Apache的配置如下:

# WSGI
WSGIDaemonProcess python-path=/home/uname/abc-service/src/abc_service/.abc_venv/lib/python3.5/site-packages
WSGIScriptAlias /abc /home/uname/abc-service/src/abc_service/wsgi.py

ProxyPass /abc http://localhost:8000/abc
ProxyPassReverse /abc http://localhost:8000/abc
<Location /abc>
AuthType None
Require all granted
# Always set these headers.
Header always set Access-Control-Allow-Origin "https://example.com"
Header always set Access-Control-Allow-Methods "POST, GET, OPTIONS, DELETE, PUT"
Header always set Access-Control-Max-Age "1000"
Header always set Access-Control-Allow-Headers "x-requested-with, Content-Type, origin, authorization, accept, client-security-token"
</Location>

我在网络上找不到任何关于使用 FalconGunicornmod_wsgi 和 [=25= 的组合的网络上相同资源的有用资源].所以,我不知道我做错了什么。

感谢您抽出时间提供帮助。谢谢!

因此,对我有用的解决方案是将 WSGIPythonHome 路径添加到 Apache 配置。

WSGIPythonHome /var/local/abc-service/src/abc_service/.venv

最终的 Apache 配置如下所示:

# At the top of the Apache Config file
WSGIPythonHome /var/local/abc-service/src/abc_service/.venv

...
...
...

# WSGI
WSGIDaemonProcess python-path=/home/uname/abc-service/src/abc_service/.abc_venv/lib/python3.5/site-packages
WSGIScriptAlias /abc /home/uname/abc-service/src/abc_service/wsgi.py

ProxyPass /abc http://localhost:8000/abc
ProxyPassReverse /abc http://localhost:8000/abc
<Location /abc>
AuthType None
Require all granted
# Always set these headers.
Header always set Access-Control-Allow-Origin "https://example.com"
Header always set Access-Control-Allow-Methods "POST, GET, OPTIONS, DELETE, PUT"
Header always set Access-Control-Max-Age "1000"
Header always set Access-Control-Allow-Headers "x-requested-with, Content-Type, origin, authorization, accept, client-security-token"
</Location>