mod_wsgi:无法获取Python主目录和ImportError:找不到名为'encodings'的模块。

11

 我正在尝试在Apache 2.4上使用Django和Python 3.5.2的mod_wsgi构建网站。但是,当apache守护程序httpd启动时,会在/var/log/httpd/error_log中输出以下错误消息。

[Fri Sep 16 17:44:57.145900 2016] [wsgi:warn] [pid 20593] (13)Permission denied: mod_wsgi (pid=20593): Unable to stat Python home /home/ec2-user/.pyenv/versions/3.5.2. Python interpreter may not be able to be initialized correctly. Verify the supplied path and access permissions for whole of the path.
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ImportError: No module named 'encodings'

 因此,我研究了一些有关类似问题的文章,例如:

但是,上述错误消息的原因尚未解决。
 请指出需要检查的一些要点或需要阅读的文档等。

我的开发环境如下。

(1) 主机和操作系统

  • 托管服务:Amazon AWS EC2
  • 操作系统:Amazon Linux AMI release 2016.03

(2) Django项目

 我在目录/home/ec2-user/django-sites中使用用户账户ec2-user创建了名为testprj的Django项目。

[ec2-user@MyEC2 django-sites]$ pwd
/home/ec2-user/django-sites
[ec2-user@MyEC2 django-sites]$ ls -l
total 4
drwxrwxr-x 3 ec2-user ec2-user 4096 Sep 16 14:50 testprj

测试项目使用的数据库已经设置完成。因此,Django提供的开发服务器已成功启动,没有任何错误,如下所示。

[ec2-user@MyEC2 testprj]$ python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).
September 16, 2016 - 14:23:47
Django version 1.10.1, using settings 'testprj.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

(3) Python的环境

 我使用pyenv install 3.5.2安装了Python 3.5.2,然后设置了虚拟环境pyenv virtualenv 3.5.2 django-sites。 接下来,我将django-sites目录设置为本地环境,路径为/home/ec2-user/django-sites,如下所示。

[ec2-user@MyEC2 django-sites]$ pwd
/home/ec2-user/django-sites
[ec2-user@MyEC2 django-sites]$ pyenv versions
  system
  3.5.2
  3.5.2/envs/django-sites
* django-sites (set by /home/ec2-user/django-sites/.python-version)
[ec2-user@MyEC2 django-sites]$ python -V
Python 3.5.2

 我通过pip命令安装了以下模块。

[ec2-user@MyEC2 django-sites]$ pwd
 /home/ec2-user/django-sites
[ec2-user@MyEC2 django-sites]$ pip list
configparser (3.5.0)
Django (1.10.1)
mod-wsgi (4.5.7)
pip (8.1.2)
PyMySQL (0.7.9)
setuptools (27.2.0)

(4) Web服务器

 我使用的Web服务器如下所示:Apache 2.4。

[ec2-user@MyEC2 ~]$ httpd -v
Server version: Apache/2.4.23 (Amazon)
Server built:   Jul 29 2016 21:42:17

 执行 Apache 的用户和组都是 apache,如下行所示:/etc/httpd/conf/httpd.conf。

User apache
Group apache 

(5) 为Django项目testprj添加额外的配置文件

 我打算进行配置,使得URL

http://[MyEC2 domain]/testprj/

可以访问位于/home/ec2-user/django-sites/testprj的Django项目的顶级页面。
 [MyEC2 domain]上方类似于

ec2-XX-XX-XX-XX.us-west-2.compute.amazonaws.com
所以,我在/etc/httpd/conf.d目录下编写了如下的testprj.conf文件。
[ec2-user@MyEC2 conf.d]$ pwd
/etc/httpd/conf.d
[ec2-user@MyEC2 conf.d]$ cat -n testprj.conf 
 1  LoadModule wsgi_module /home/ec2-user/.pyenv/versions/django-sites/lib/python3.5/site-packages/mod_wsgi/server/mod_wsgi-py35.cpython-35m-x86_64-linux-gnu.so
 2  
 3  WSGIPythonHome /home/ec2-user/.pyenv/versions/3.5.2
 4  WSGIScriptReloading On
 5  WSGIScriptAlias  /testprj/ /home/ec2-user/django-sites/testprj/testprj/wsgi.py
 6  WSGIPythonPath /home/ec2-user/django-sites/testprj:/home/ec2-user/.pyenv/versions/django-sites/lib/python3.5/site-packages
 7  
 8  <VirtualHost *:80>
 9  
10    ServerName http://ec2-XX-XX-XX-XX.us-west-2.compute.amazonaws.com
11    SuexecUserGroup apache apache
12  
13    <Directory /home/ec2-user/django-sites/testprj/testprj>
14      <Files wsgi.py>
15      Require all granted
16      </Files>
17    </Directory>
18  
19    Alias /static/ "/home/ec2-user/django-sites/testprj/static/"
20  
21    <Directory /home/ec2-user/django-sites/testprj/static>
22      <Files *>
23      Require all granted
24      </Files>
25    </Directory>
26  
27  </VirtualHost>
28  
[ec2-user@MyEC2 conf.d]$

致以最好的问候。


另请参阅mod_wsgi: ImportError: No module named 'encodings',这是类似的问题,具有相似的解决方法。 - Lars Ericson
1个回答

6

需要检查的几个事项。

  • 默认情况下,pyenv工具不会安装带有共享库的Python。这可能会导致问题,因为mod_wsgi需要一个共享库。您需要明确告诉pyenv使用共享库构建Python。

  • 许多Linux系统上的主目录对其他用户不可读。当初始化mod_wsgi时,它正在以Apache用户身份运行,并且将无法查看主目录内部。当Apache加载mod_wsgi模块时没有问题,因为此时Apache正在以root身份运行。

您的配置存在其他不遵循最佳实践的问题,但以上问题,特别是第二个问题很可能是您的问题的原因。


谢谢您的建议。我已经考虑了你给我的两个事项中的第一个。我在我的问题中写道:“我通过pyenv install 3.5.2安装了Python 3.5.2。”但这是不正确的。实际上,我使用了命令行:CONFIGURE_OPTS=--enable-shared pyenv install 3.5.2。因此,根据您的建议,“特别是第二个事项很可能是您问题的原因。”,我会更多地研究执行权限。谢谢! - jun68ykt
8
我尝试了 sudo chmod 755 /home/ec2-user,然后它成功了! - jun68ykt
我卡在这里了:致命的Python错误:Py_Initialize:无法获取区域设置编码 ImportError:找不到名为'encodings'的模块 请帮忙。 - Divij Sehgal
使用mkvirtualenv创建了虚拟环境。 - Divij Sehgal
创建一个新问题。不要在现有问题的评论中发布您的新问题。在您的新问题中,正确地解释您的问题,以及构建Python和mod_wsgi等所采取的步骤。 - Graham Dumpleton
@jun68ykt 我有点担心改变我的HOME文件夹的访问权限。难道没有其他解决方案吗? - ThePhi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接