【发布时间】:2020-06-13 21:53:29
【问题描述】:
在使用 Flask 的开发模式下,我能够使用 firebase 管理模块成功地向 Firestore 读取和写入数据。但是,一旦我使用 mod_wsgi 在 Apache 2 上部署应用程序,一切正常(firebase 应用程序初始化不会引发任何错误),直到我开始获取文档,代码被卡住并且页面永远加载。
__init__.py中出现问题的Flask函数:
@app.route('/users/<user>')
def login(user=None):
if not user:
abort(404)
userRef = db.collection('users').document(user)
print("1") # This runs.
userDoc = userRef.get() # The code gets stuck here. No error message is ever returned or stored in the error log.
print("2") # This never runs.
flaskapp.wsgi
#!/usr/local/bin/python3.8
import sys
import os
import logging
logging.basicConfig(stream=sys.stderr)
os.chdir("/var/www/html/example.com/FlaskApp/")
sys.path.insert(0,"/var/www/html/example.com/FlaskApp/")
from __init__ import app as application
example.com.conf
LoadModule wsgi_module "/home/username/.local/lib/python3.8/site-packages/mod_wsgi/server/mod_wsgi-py38.cpython-38-x86_64-linux-gnu.so"
WSGIPythonHome "/usr/local"
<VirtualHost *:80>
# Admin email, Server Name (domain name), and any aliases
ServerAdmin username@example.com
ServerName example.com
ServerAlias www.example.com
# Index file and Document Root (where the public files are located)
DirectoryIndex index.html index.php
DocumentRoot /var/www/html/example.com/public_html
WSGIScriptAlias / /var/www/html/example.com/FlaskApp/flaskapp.wsgi
<Directory /var/www/html/example.com/FlaskApp/>
Require all granted
</Directory>
Alias /static /var/www/html/example.com/FlaskApp/static
<Directory /var/www/html/example.com/FlaskApp/static/>
Require all granted
</Directory>
# Log file locations
LogLevel warn
ErrorLog /var/www/html/example.com/log/error.log
CustomLog /var/www/html/example.com/log/access.log combined
</VirtualHost>
您知道如何让 Firebase get() 在已部署的 Flask 应用中工作吗?感谢您的帮助!
【问题讨论】:
-
您的应用部署在哪里?看起来它可能缺少服务帐户身份验证凭据。
-
应用部署在 Apache 服务器上。似乎找到了 Firebase 凭据,因为它能够运行 db = firestore.client() 而不会返回任何错误。
-
这听起来像一个 Compute Engine 实例,检查它的 access scopes,如果尚未启用 Datastore 访问,还要检查它的服务帐户角色。
-
嗨@TomLamensia,我面临同样的问题。你有没有找到解决这个问题的方法?
-
@sambam 最后,我在 Linux 服务上使用 Gunicorn 在端口 3000 上运行了 Flask 应用程序。然后在我的 Apache conf 文件中,我添加了一个从特定端点到端口 3000 的代理。这样我就可以准确地选择哪些端点对于 Apache 应该是静态的,哪些端点应该调用 Flask 应用程序。
标签: apache flask google-cloud-firestore mod-wsgi wsgi