Skip to main content
 首页 » 编程设计

python之使用 mod_wsgi 在 Apache2 上部署 Python Falcon

2024年12月31日28wayfarer

场景:我正在为我的 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> 

我在网络上找不到任何关于使用 Falcon、Gunicorn、mod_wsgi 组合的网络上的有用资源> 和 Apache。所以,我不知道我做错了什么。

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

请您参考如下方法:

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