首页 > 解决方案 > 使用 mod_wsgi 部署 Flask:我从 error.log 收到错误“无法打开数据库文件”

问题描述

我已经安装了 mod_wsgi,目前正在部署一个测试应用程序。问题源于应用程序无法从数据库中读取。如果我不使用 mod_wsgi,则测试应用程序可以工作。

这是我的 testapp.wsgi

#! /usr/bin/python3.7

import logging
import sys
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0, '/var/www/html/TestApp/TestApp/')

from testapp import app as application
application.secret_key = "anything you wish"

这是我的 TestApp.conf 文件

<VirtualHost *:80>
        ServerName 172.28.103.40
        WSGIDaemonProcess testapp user=www-data group=www-data threads=5
        WSGIScriptAlias /testApp/var/www/html/TestApp/TestApp$

        <Directory /var/www/html/TestApp/TestApp>
                WSGIProcessGroup testapp
                WSGIApplicationGroup %{GLOBAL}
                Order deny,allow
                Allow from all
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log
        LogLevel warn
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

我的数据库目录

drwxrwxrwx 2 www-data      www-data  4096 Nov  1 11:23 db

我的数据库文件

-rwxrwxrwx 1 www-data      www-data 237568 Nov  1 11:23 shieldcampus.db

我意识到不需要完整的 777 访问权限,但我想看看将权限设置为 777 是否可行,但它没有。

错误日志中的确切错误是

[Thu Nov 14 10:53:35.501047 2019] [wsgi:error] [pid 3760] [client 172.19.90.16:60233]     con = sqlite3.connect("db/testapp.db")
[Thu Nov 14 10:53:35.501062 2019] [wsgi:error] [pid 3760] [client 172.19.90.16:60233] OperationalError: unable to open database file

我注意到的一件事是错误日志中的 IP 地址不是我用来访问我的站点的 IP 地址。

我的站点 IP 是 172.28.103.40 此调用与 WSGI 一起使用的 IP 是 172.19.90.16:60233

这可能是正在发生的事情的线索吗?如果没有,我错过了断开连接。作为 mod_wsgi 的新手,在使用 mod_wsgi 设置数据库时,我可能会遗漏一些东西。我在文档上没有看到任何内容。

这也是一个本地服务器,不是公共的。不确定这是否会有所作为。

编辑:烧瓶应用程序代码

from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
    return "Hello world!"
if __name__ == "__main__":
    app.run()

标签: flaskmod-wsgi

解决方案


推荐阅读