【问题标题】:Deploying Python Falcon on Apache2 with mod_wsgi使用 mod_wsgi 在 Apache2 上部署 Python Falcon
【发布时间】:2018-06-26 21:13:34
【问题描述】:

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

目标:我想将它与mod_wsgi 一起部署在我的个人开发服务器上的Apache2 上。 该 API 仍将在 Gunicorn 下运行,并且每当在该端点上发出请求时,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_wsgiApache 的组合的相同资源。所以,我无法弄清楚我做错了什么。

感谢您的宝贵时间和帮助。谢谢!

【问题讨论】:

  • this Stack Overflow Question 类似的问题。但是那里没有讨论解决方案!
  • 是的,我已经回答了这个问题,但它并没有真正的帮助。
  • 如果你还在使用 gunicorn 并且只是通过 Apache 代理它,你根本不需要 mod_wsgi。

标签: python apache mod-wsgi


【解决方案1】:

因此,一个对我有用的解决方案是将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>

【讨论】:

    猜你喜欢
    • 2018-03-30
    • 2016-11-17
    • 2015-03-17
    • 1970-01-01
    • 1970-01-01
    • 2017-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多