目标 WSGI 脚本无法作为 Python module.Flask.Apache 加载
Posted
技术标签:
【中文标题】目标 WSGI 脚本无法作为 Python module.Flask.Apache 加载【英文标题】:Target WSGI script cannot be loaded as Python module.Flask.Apache 【发布时间】:2022-01-02 17:15:11 【问题描述】:我在烧瓶中有一个为 python3.8 构建的应用程序。我正在尝试使用 apache 部署它并且遇到了问题。
我使用以下命令安装了 wsgi:sudo apt-get install libapache2-mod-wsgi-py3
因为它是专门用于这个单一应用程序的服务器,所以我使用 sudo pip3 全局安装了我的 python 模块,而不是在 virtualenv 中,所以我所有需要/安装的模块都位于/usr/local/lib/python3.8/dist-packages/
。
这是我的 .wsgi 文件。 #! /usr/bin/python
import logging
import sys
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0, '/home/admin/imm-migration/')
from easyucs_api import app as application
这是我的 apache 配置
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /home/admin/imm-migration
ErrorLog $APACHE_LOG_DIR/error.log
CustomLog $APACHE_LOG_DIR/access.log combined
<Directory /home/admin/imm-migration>
# set permissions as per apache2.conf file
Options FollowSymLinks
AllowOverride None
Require all granted
</Directory>
WSGIDaemonProcess migration python-path=/home/admin/imm-migration:/usr/local/lib/python3.8/dist-packages
WSGIScriptAlias / /home/admin/imm-migration/easyucs_api.wsgi
WSGIApplicationGroup %GLOBAL
WSGIProcessGroup migration
WSGIPassAuthorization On
</VirtualHost>
在运行服务器时出现以下错误
[Wed Nov 24 09:15:39.409792 2021] [core:notice] [pid 7271:tid 140562056457152] AH00094: Command line: '/usr/sbin/apache2'
[Wed Nov 24 09:22:04.219227 2021] [mpm_event:notice] [pid 7271:tid 140562056457152] AH00491: caught SIGTERM, shutting down
[Wed Nov 24 09:26:33.603552 2021] [mpm_event:notice] [pid 2541:tid 139881431014336] AH00489: Apache/2.4.29 (Ubuntu) mod_wsgi/4.5.17 Python/3.6 configured -- resuming normal operations
[Wed Nov 24 09:26:33.603677 2021] [core:notice] [pid 2541:tid 139881431014336] AH00094: Command line: '/usr/sbin/apache2'
[Wed Nov 24 09:26:40.524523 2021] [wsgi:error] [pid 2543:tid 139881310197504] [remote 10.81.0.135:63663] mod_wsgi (pid=2543): Target WSGI script '/home/admin/imm-migration/easyucs_api.wsgi' cannot be loaded as Python module.
[Wed Nov 24 09:26:40.524706 2021] [wsgi:error] [pid 2543:tid 139881310197504] [remote 10.81.0.135:63663] mod_wsgi (pid=2543): Exception occurred processing WSGI script '/home/admin/imm-migration/easyucs_api.wsgi'.
[Wed Nov 24 09:26:40.525657 2021] [wsgi:error] [pid 2543:tid 139881310197504] [remote 10.81.0.135:63663] Traceback (most recent call last):
[Wed Nov 24 09:26:40.525756 2021] [wsgi:error] [pid 2543:tid 139881310197504] [remote 10.81.0.135:63663] File "/home/admin/imm-migration/easyucs_api.wsgi", line 8, in <module>
[Wed Nov 24 09:26:40.525811 2021] [wsgi:error] [pid 2543:tid 139881310197504] [remote 10.81.0.135:63663] from easyucs_api import app as application
[Wed Nov 24 09:26:40.525859 2021] [wsgi:error] [pid 2543:tid 139881310197504] [remote 10.81.0.135:63663] File "/home/admin/imm-migration/easyucs_api.py", line 10, in <module>
[Wed Nov 24 09:26:40.525898 2021] [wsgi:error] [pid 2543:tid 139881310197504] [remote 10.81.0.135:63663] from werkzeug.middleware.dispatcher import \\
[Wed Nov 24 09:26:40.525938 2021] [wsgi:error] [pid 2543:tid 139881310197504] [remote 10.81.0.135:63663] File "/usr/local/lib/python3.8/dist-packages/werkzeug/__init__.py", line 2, in <module>
[Wed Nov 24 09:26:40.525974 2021] [wsgi:error] [pid 2543:tid 139881310197504] [remote 10.81.0.135:63663] from .test import Client as Client
[Wed Nov 24 09:26:40.526015 2021] [wsgi:error] [pid 2543:tid 139881310197504] [remote 10.81.0.135:63663] File "/usr/local/lib/python3.8/dist-packages/werkzeug/test.py", line 30, in <module>
[Wed Nov 24 09:26:40.526084 2021] [wsgi:error] [pid 2543:tid 139881310197504] [remote 10.81.0.135:63663] from .sansio.multipart import Data
[Wed Nov 24 09:26:40.526133 2021] [wsgi:error] [pid 2543:tid 139881310197504] [remote 10.81.0.135:63663] File "/usr/local/lib/python3.8/dist-packages/werkzeug/sansio/multipart.py", line 2, in <module>
[Wed Nov 24 09:26:40.526173 2021] [wsgi:error] [pid 2543:tid 139881310197504] [remote 10.81.0.135:63663] from dataclasses import dataclass
[Wed Nov 24 09:26:40.526236 2021] [wsgi:error] [pid 2543:tid 139881310197504] [remote 10.81.0.135:63663] ModuleNotFoundError: No module named 'dataclasses'
之前我在尝试为 Django 应用程序安装 apache 时遇到了同样的错误。我通过在根目录中安装 python 包解决了这个问题,即(usr/local/lib/python3.8/dist-packages/ 早些时候它在家里/admin/.local/lib/python3.8/site-packages) 作为 apache 仅访问来自 root 用户的文件..
不知道这里出了什么问题.. 任何帮助将不胜感激。
【问题讨论】:
【参考方案1】:我已经解决了这个问题。这些是我做的事情
我已经卸载了系统 mod_wsgi 包。
sudo apt-get remove libapache2-mod-wsgi-py3
并使用 pip 安装了 mod_wsgi
sudo -H pip3 install mod_wsgi
如果您收到缺少 Apache httpd 服务器包的错误,您可能需要运行 sudo apt-get install apache2-dev
问题是libapache2-mod-wsgi-py3
包不适用于 Python 3.8,但适用于 Python 3.6。
【讨论】:
以上是关于目标 WSGI 脚本无法作为 Python module.Flask.Apache 加载的主要内容,如果未能解决你的问题,请参考以下文章
WSGI 脚本无法作为 Python 模块加载——500 内部服务器错误
mod_wsgi 解析 WSGI 脚本文件失败 |处理 WSGI 脚本时发生异常 |无法导入“站点”模块
当我触摸 wsgi 脚本时,带有 mod_wsgi 的烧瓶应用程序不会重新加载