Python3, Apache, mod-wsgi 'FileNotFoundError' 未定义

Python3, Apache, mod-wsgi 'FileNotFoundError' is not defined

我已经从源代码安装了 mod-wsgi,使用 Python3(Amazon Linux 2 VM 上的 Apache 2.4.46)。

每次我重新加载 wsgi 守护程序时,我的 Apache 错误日志中都会出现以下错误:

Exception ignored in: <generator object path at 0x7f128e93fdd0>
Traceback (most recent call last):
  File "/usr/lib64/python3.7/importlib/resources.py", line 190, in path
NameError: name 'FileNotFoundError' is not defined

我认为这是在守护程序关闭时发生的,因为我在关闭 Apache 时也看到了错误。

我运行我的项目在python3的虚拟环境中。 这是我的 Apache 配置:

<VirtualHost *:443>
    <Directory /var/www/html/project/>
         Options +ExecCGI +FollowSymlinks -SymLinksIfOwnerMatch
         Require all granted
    </Directory>
    ErrorLog /home/ec2-user/apache_project_errors.log
    ServerName project.projtds.net
    DocumentRoot /var/www/html/project/
    WSGIDaemonProcess project threads=1 processes=2 python-home=/var/www/html/project/venv home=/var/www/html/project
    WSGIScriptAlias / /var/www/html/project/project.wsgi process-group=project
    WSGIScriptReloading On
</VirtualHost>

我的 .wsgi 相当简单:

from project import create_app
application = create_app()

所有这些调用烧瓶应用程序(project.py):

from flask import Flask


def create_app():
    app = Flask(__name__, instance_relative_config=False)

    @app.route("/")
    def home():
        import sys
        string = f"Python version {sys.version} Version info {sys.version_info}"
        return string

    return app

目前,我只检查 python 版本,因为该错误是 python2 解释器检查 python3 代码的典型错误...但是 sys.version 确实如此显示为 3.7,我可以 运行 python3 在我的项目中进行特定调用。

我检查了 wsgi module 在 Apache conf 中的加载方式 (/etc/httpd/conf.modules.d/wsgi.conf):
LoadModule wsgi_module modules/mod_wsgi.so

我在同一 Apache 服务器下 运行ning 的其他非 wsgi 项目中没有看到错误。 我不知道如何修复错误。它不会阻止我 运行ning 我的项目,但我很想知道错误的确切来源和原因。

有没有办法获取 mod-wsgi 调试日志?这是我需要在我的项目中做的事情吗? (烧瓶、.wsgi 等)

我缺少一个参数:

    <Directory /var/www/html/project/>
         Options +ExecCGI +FollowSymlinks -SymLinksIfOwnerMatch
         Require all granted
    </Directory>

应该是:

    <Directory /var/www/html/jiraiop/>
     Options +ExecCGI +FollowSymlinks -SymLinksIfOwnerMatch
         Require all granted
         WSGIApplicationGroup %{GLOBAL}
    </Directory>

https://modwsgi.readthedocs.io/en/develop/configuration-directives/WSGIApplicationGroup.html

Any WSGI applications in the global application group will always be executed within the context of the first interpreter created by Python when it is initialised, of the process handling the request.

我认为这是确保解释器是 python3 所必需的。不知道为什么没有它它会遇到困难但是......这解决了它。