django - 使用 mod_wsgi 在 apache 上部署 django 应用程序时出现“没有名为 'encodings' 的模块”错误

标签 django apache mod-wsgi django-wsgi

版本列表:

Centos 7 --- django 2.1.7 --- Apache 2.4.6 --- Python 3.6

查看编辑 5 对于最近的错误日志

在此处遵循本教程:https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-apache-and-mod_wsgi-on-centos-7

我可以运行 sudo systemctl start httpd当我转到网址时,我得到一个内部服务器错误(底部的日志)

我的文件结构

home
└── user
    └── projects
        └── myapp
            ├── app
            │   ├── <All Code for Webapp including static dir>  
            ├── env (virtualenv)
            ├── manage.py
            ├── new
            │   ├── settings.py
            │   ├── urls.py
            │   └── wsgi.py
            └── requirements.txt

我希望有人能在我的 httpd 文件夹中的 django.conf 中看到一个错误。并希望这些是我正在寻找的端点

编辑:此文件位于此处:/etc/httpd/conf.d/django.conf

django.conf
Alias /static /home/user/projects/myapp/app/static
<Directory /home/user/projects/myapp/app/static>
    Require all granted
</Directory>

<Directory /home/user/projects/myapp/new>
    <Files wsgi.py>
        Require all granted
    </Files>
</Directory>

WSGIApplicationGroup %{GLOBAL}
WSGIDaemonProcess myapp python-path=/home/user/projects/myapp python-home=/home/user/projects/myapp/env 
WSGIProcessGroup myapp
WSGIScriptAlias / /home/user/projects/myapp/new/wsgi.py

我不确定这些是否指向所有正确的地方,并希望有人可以再看我一眼。

而且我还没有碰过 wsgi.py 并且想知道我是否遗漏了那里的任何逻辑。

我的wsgi.py
import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'new.settings')

application = get_wsgi_application()

EDIT2:我能够运行服务器,但出现 500 内部服务器错误,当我检查 error_logs 时,我得到了这个

更新日志
[Mon Mar 11 10:40:39.865611 2019] [core:notice] [pid 9371] SELinux policy enabled; httpd running as context system_u:system_r:httpd_t:s0
[Mon Mar 11 10:40:39.868149 2019] [suexec:notice] [pid 9371] AH01232: suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)
[Mon Mar 11 10:40:39.918015 2019] [auth_digest:notice] [pid 9371] AH01757: generating secret for digest authentication ...
[Mon Mar 11 10:40:39.919681 2019] [lbmethod_heartbeat:notice] [pid 9371] AH02282: No slotmem from mod_heartmonitor
[Mon Mar 11 10:40:39.948303 2019] [mpm_prefork:notice] [pid 9371] AH00163: Apache/2.4.6 (CentOS) mod_wsgi/3.4 Python/2.7.5 configured -- resuming normal operations
[Mon Mar 11 10:40:39.948370 2019] [core:notice] [pid 9371] AH00094: Command line: '/usr/sbin/httpd -D FOREGROUND'
[Mon Mar 11 10:40:42.878806 2019] [mime_magic:error] [pid 9376] [client ip] AH01512: mod_mime_magic: can't read `/home/user/projects/myapp/new/wsgi.py'
[Mon Mar 11 10:40:42.879459 2019] [mime_magic:error] [pid 9376] [client ip] AH01512: mod_mime_magic: can't read `/home/user/projects/myapp/new/wsgi.py'
[Mon Mar 11 10:40:42.905048 2019] [:error] [pid 9372] (13)Permission denied: [remote ip] mod_wsgi (pid=9372, process='myapp', application='<url>|'): Call to fopen() failed for '/home/user/projects/myapp/new/wsgi.py'

编辑3

在这个日志文件中它说 Apache/2.4.6 (CentOS) mod_wsgi/3.4 Python/2.7.5 configured当我运行 python3.6 而不是 2.7.5 时,也许改变它会有所帮助?

编辑 4

所以我将我的 WSGIDaemonProcess 更改为
WSGIDaemonProcess myapp python-path=/home/user/projects/myapp python-home=/home/user/projects/app/env/lib/python3.6/site-packages user=<user>

现在我收到这些错误
[Tue Mar 12 10:38:09.111397 2019] [mime_magic:error] [pid 18804] [client ip] AH01512: mod_mime_magic: can't read `/home/user/project/myapp/new/wsgi.py'
ImportError: No module named site
ImportError: No module named site
ImportError: No module named site

所以看起来我需要卸载 mod_wsgi 并重新安装以使用 python3.6 而不是 2.7 编译

我怎样才能成功地做到这一点?我卸载 mod_wsgi 并尝试使用 sudo pip3.6 install mod_wsgi 进行 mreinstall现在它无法识别新安装的 mod_wsgi 并且不会启动服务器,因为 invalid command 'WSGIDaemonProcess'这是 bc 新下载没有配置 httpd?我该怎么做?

编辑 5

成功安装 mod_wsgi 用正确的 python 版本编译并能够运行服务器,将 SELinux 设置为允许摆脱 cant find '/route/to/wsgi/py .现在我没有收到重复的模块名称“编码”错误。
[Wed Mar 13 15:20:46.133597 2019] [core:notice] [pid 4403] AH00094: Command line: '/usr/sbin/httpd -D FOREGROUND'
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'
[Tue Mar 12 14:11:57.520271 2019] [core:notice] [pid 866] AH00052: child pid 891 exit signal Aborted (6)
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'

目前我的权限都在用户 apache 和组 apache 下,似乎它们都在正确的权限下,我也尝试将 root 用户/组更改为 apache,然后运行 ​​chmod -R 777在 Myapp 目录中以确保它具有适当的权限。

项目下的权限:
home/user/projects# ls -l

total 0
drwxrw-r-x. 7 apache apache 109 Mar 12 12:48 myapp

home/user/projects/myapp# ls -l

myapp下的权限:
total 12
drwxrwxr-x. 7 apache apache 4096 Mar  7 13:18 app
drwxr-xr-x. 5 apache apache   56 Mar 12 12:48 env
-rwxrwxr-x. 1 apache apache  535 Mar  5 13:33 manage.py
drwxrwxr-x. 3 apache apache  110 Mar  7 14:27 new
-rw-rw-r--. 1 apache apache  869 Mar  7 14:42 requirements.txt

最佳答案

根据 mod_wsgi 的文档:

user=name | user=#uid Defines the UNIX user name or numeric user uid of the user that the daemon processes should be run as. If this option is not supplied the daemon processes will be run as the same user that Apache would run child processes, as defined by the User directive, and it is not necessary to set this to the Apache user yourself.



我想 apache 用户无法访问您的主目录。尝试添加 user=myUser WSGIDaemonProcess 或 chown 的指令您的目录到 apache 用户。另一种可能的方法是将您的应用程序放在 apache 用户拥有的目录中。

编辑

我真的应该学会阅读。根据文档,看起来您应该使用 python3.X 解释器重新编译 mod_wsgi 以运行 python3.X 应用程序(编译和应用程序环境使用相同的版本)

If you want to use a different version of Python, you will need to reinstall mod_wsgi, compiling it for the version you want. It is not possible for the one mod_wsgi instance to run applications for both Python 2 and 3 at the same time.



编辑 2

回答您的评论之一:yum 不会删除您通过 pip 安装的版本是正常的。无法猜测您是否通过其他包管理器 AFAIK 安装了包。我认为您应该将位于 pip 安装 mod_wsgi 的目录中的一些 .so(我认为是 mod_wsgi[version].so)文件(我认为在您的 venv/global site_packages 中)复制到 Apache/Httpd 加载它的文件夹中模块文件(它类似于 alpine/ubuntu 上的 /usr/lib/apache2/modules 所以我猜它是 CentOS 的 /usr/lib/httpd/modules,但可能会有所不同,抱歉我不能给你更具体的帮助)从这个文件夹中移出旧的 mod_wsgi。所以。在执行此操作之前不要忘记关闭 httpd,然后重新启动它。

来源:https://modwsgi.readthedocs.io/en/develop/configuration-directives/WSGIDaemonProcess.html (部分python-home)

关于django - 使用 mod_wsgi 在 apache 上部署 django 应用程序时出现“没有名为 'encodings' 的模块”错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55066782/

相关文章:

ios - 带有 IOS : getting multipart data (json + image) via POST 的 Django

python - 安排常规事件 : Cron/Cron alternatives (including Celery)

java - 如何使用 Apache PDFBox 使 PDF 文本可选择?

python - 使用 pyenv 和 virtualenv 在 apache 后面部署 Flask 应用程序时找不到 numpy、pandas 模块

Django STATIC_URL 值在生产中无法识别

python - 在共享虚拟主机服务器上使用 wsgi 和 virtualenv 部署 Django 项目,无需 root 访问权限

java - 使用 Apache 从 FTP 下载中截断数据

apache - Centos7 启用 SSL - 连接超时

python - modwsgi 和 mysql。查询仅显示触及 wsgi 文件后的实际结果

django - 模板不存在