接收到“没有模块命名编码错误".在WSGI模式下的Apache中 [英] Receiving "No module named encodings error" in Apache in WSGI mode

查看:72
本文介绍了接收到“没有模块命名编码错误".在WSGI模式下的Apache中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到没有名为'encodings'的模块"错误.在我看来,Python虚拟环境未正确加载.但是,我还包括了wsgi文件,以防万一.

I am encountering a "No module named 'encodings'" error. It appears to me that Python virtual env was not correctly loaded. However I also included wsgi file just in case.

0>我收到错误:

[Mon Mar 12 16:49:44.919934 2018] [:info] [pid 6546] mod_wsgi (pid=6546): Starting process 'abcd_server' with uid=501, gid=501 and threads=5.
[Mon Mar 12 16:49:44.920494 2018] [:info] [pid 6546] mod_wsgi (pid=6546): Python home /var/www/m.abcd.com/venv.
[Mon Mar 12 16:49:44.920556 2018] [:info] [pid 6546] mod_wsgi (pid=6546): Initializing Python.
Fatal Python error: Py_Initialize: Unable to get the locale encoding
  ModuleNotFoundError: No module named 'encodings'

1>安装:

  • Amazon EC2 linux实例(CentOS);
  • 已安装Python 3.6:sudo yum install python36
  • 已安装的wsgi:sudo yum install mod24_wsgi-python36.x86_64
  • Amazon EC2 linux instance (CentOS);
  • installed Python 3.6: sudo yum install python36
  • installed wsgi: sudo yum install mod24_wsgi-python36.x86_64

2>验证:

  • httpd:Apache/2.4.27(Amazon)
  • python3:Python 3.6.2
  • httpd使用modules/wsgi.so ldd wsgi.so:

linux-vdso.so.1 =>  (0x00007ffe71de8000)
libpython3.6m.so.1.0 => /usr/lib64/libpython3.6m.so.1.0 (0x00007f49a0ecf000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f49a0cb3000)
libdl.so.2 => /lib64/libdl.so.2 (0x00007f49a0aaf000)
libutil.so.1 => /lib64/libutil.so.1 (0x00007f49a08ac000)
libm.so.6 => /lib64/libm.so.6 (0x00007f49a05aa000)
libc.so.6 => /lib64/libc.so.6 (0x00007f49a01e6000)
/lib64/ld-linux-x86-64.so.2 (0x00007f49a1616000)

3>网站设置:

  • 创建一个新用户"python_user:python_user"(uid = 501);
  • 创建一个目录/var/www/m.abcd.com,并归python_user:python_user拥有.子目录也归python_user:python_user
  • 所有
  • 在/var/www/m.abcd.com下,创建python-src,venv,html,日志,applogs等.
  • python-src包含setup.py和应用程序源文件
  • 通过运行python3 -m venv venv(不是python 2 )(设置虚拟环境)来设置
  • venv
  • 在python-src内部,pip install -e .将应用程序依赖项添加到虚拟环境中.
  • Create a new user "python_user:python_user" (uid=501);
  • Create a directory /var/www/m.abcd.com and owned by python_user:python_user. Also subdirectories are also owned by python_user:python_user
  • Under /var/www/m.abcd.com, create python-src, venv, html, logs, applogs etc.
  • python-src contains setup.py and application source files
  • venv is setup by running python3 -m venv venv ( not python2) (setup virtual env)
  • inside python-src, pip install -e . to add app dependencies to virtual env.

4> httpd配置:

4> httpd config:

<VirtualHost *:443>
  ......
  WSGIDaemonProcess abcd_server user=python_user group=python_user threads=5 python-home="/var/www/m.abcd.com/venv"
  WSGIProcessGroup abcd_server
  WSGIScriptAlias / "/var/www/m.abcd.com/python-src/src/abcd_server.wsgi"

  <Directory "/var/www/m.abcd.com/html">
    Require all granted
  </Directory>

  <Directory "/var/www/m.abcd.com/python-src/src">
    Require all granted
  </Directory>
  <Directory "/var/www/m.abcd.com/conf">
    Require all granted
  </Directory>
  <Directory "/var/www/m.abcd.com/applogs">
    Require all granted
  </Directory>
  <Directory "/var/www/m.abcd.com/data">
    Require all granted
  </Directory>

  ......
</VirtualHost>

5> abcd_server.wsgi

5> abcd_server.wsgi

imort os
os.environ['P2SERVER_APP_CFG'] = '/var/www/m.abcd.com/conf/abcd_conf.yaml'
from p2events.app import app as application

6>(编辑)在交互式python会话中激活虚拟环境时,sys.prefix =/var/www/m.abcd.com/venv.这样看来还可以.

6> (Edit) when activated the virtual env, in the interactive python session, sys.prefix = /var/www/m.abcd.com/venv. So that seems ok.

7>(编辑,继续执行Graham Dumpleton关于wsgi版本的评论). WSGI版本似乎是问题所在.它是3.5,当前版本是4.6.2.我将按照Graham Dumpleton的建议尝试安装新的.谢谢格雷厄姆!

7> (Edit, follow up with Graham Dumpleton's comment about wsgi version). WSGI version seems the issue. It is 3.5 and the current version is 4.6.2. I will try to install the new one as Graham Dumpleton suggested. Thank you, Graham!

[Sun Mar 11 22:16:55.898442 2018] [mpm_prefork:notice] [pid 2465] AH00163: Apache/2.4.27 (Amazon) OpenSSL/1.0.2k-fips mod_wsgi/3.5 Python/3.6.2 configured -- resuming normal operations

推荐答案

这是我在评论部分的Graham Dumpleton帮助下找到的.

Here is what I found with Graham Dumpleton's help in the comments section.

问题是Amazon Linux的mod_wsgi的默认安装非常旧3.5.在使用pip-3.6 安装mod_wsgi(当前为4.6.2)并将其链接到httpd之后,问题已解决.

The issue is that the default installation of mod_wsgi from Amazon Linux is very old 3.5. After installing mod_wsgi (currently 4.6.2) using pip-3.6 and link it to httpd, the issue was resolved.

这篇关于接收到“没有模块命名编码错误".在WSGI模式下的Apache中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆