简体   繁体   English

mod_wsgi:无法统计Python主目录和ImportError:没有名为“ encodings”的模块

[英]mod_wsgi: Unable to stat Python home and ImportError: No module named 'encodings'

I am trying to construct a web site on Apache 2.4 using Django and mod_wsgi on Python 3.5.2. 我正在尝试使用Django和Python 3.5.2上的mod_wsgi在Apache 2.4上构建一个网站。 But when apache daemon httpd is started, following error message is output on /var/log/httpd/error_log. 但是当启动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. [2016年9月16日星期五17:44:57.145900] [wsgi:warn] [pid 20593](13)权限被拒绝:mod_wsgi(pid = 20593):无法统计Python home /home/ec2-user/.pyenv/versions/ 3.5.2。 Python interpreter may not be able to be initialized correctly. Python解释器可能无法正确初始化。 Verify the supplied path and access permissions for whole of the path. 验证提供的路径和整个路径的访问权限。
Fatal Python error: Py_Initialize: Unable to get the locale encoding 致命的Python错误:Py_Initialize:无法获取区域设置编码
ImportError: No module named 'encodings' ImportError:没有名为“编码”的模块

So, I explored some articles about similar problems, eg 因此,我浏览了一些有关类似问题的文章,例如

but the causes of error message above are not yet resolved. 但是上述错误消息的原因尚未解决。
Please indicate some points to be checked or documents to read and so on. 请指出一些要检查的点或要阅读的文件等。

My development environments are as follows. 我的开发环境如下。

(1) Host and OS (1)主机和操作系统

  • Hosting Service: Amazon AWS EC2 托管服务:Amazon AWS EC2
  • OS: Amazon Linux AMI release 2016.03 操作系统:Amazon Linux AMI版本2016.03

(2) Django Project (2)Django项目

I made Django project named testprj in the directory /home/ec2-user/django-sites with user account of ec2-user . 我使用用户帐户ec2-user在目录/ home / ec2-user / django-sites中创建了名为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

Database which the testprj uses is already set up. testprj使用的数据库已经建立。 So, development server provided in Django is successfully started with no error as bellow. 因此,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) Environments of Python (3)Python环境

I installed Python 3.5.2 by pyenv install 3.5.2 . 我通过pyenv install 3.5.2安装了Python pyenv install 3.5.2 And then I set pyenv virtualenv 3.5.2 django-sites . 然后我设置pyenv virtualenv 3.5.2 django-sites And I set local of /home/ec2-user/django-sites to env django-sites as bellow. 我将/ home / ec2-user / django-sites的本地设置为env 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

And I installed following modules through pip command. 我通过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 server (4)Web服务器

Web server which I use is Apache 2.4 as bellow. 我使用的Web服务器是Apache 2.4。

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

User and Group of executing Apache are both apache as next lines in /etc/httpd/conf/httpd.conf. 用户和执行的Apache组都阿帕奇如/etc/httpd/conf/httpd.conf中下一行。

User apache
Group apache 

(5) Additinal conf file for the Django project testprj (5)Django项目testprj的附加conf文件

I intend to configure such that the URL 我打算进行配置,以使URL

http://[MyEC2 domain]/testprj/

can access a top page of Django project located at /home/ec2-user/django-sites/testprj. 可以访问位于/ home / ec2-user / django-sites / testprj的Django项目的首页。
[MyEC2 domain] above is like 上面的[MyEC2域]就像

ec2-XX-XX-XX-XX.us-west-2.compute.amazonaws.com

So, I wrote testprj.conf in /etc/httpd/conf.d as bellow. 所以,我在下面的/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]$

Best regards. 最好的祝福。

A couple of things to check. 需要检查的几件事。

  • The pyenv tool doesn't install Python with a shared library by default. pyenv工具默认情况下不使用共享库安装Python。 That could result in problems as mod_wsgi wants a shared library. 可能会导致问题,因为mod_wsgi需要共享库。 You need to explicitly tell pyenv to build Python with a shared library. 您需要明确告诉pyenv使用共享库构建Python。

  • A home directory on many Linux systems is not readable to other users. 许多Linux系统上的主目录对其他用户不可读。 When mod_wsgi is being initialised, it is running as the Apache user and will not be able to see inside of the home directory. 初始化mod_wsgi时,它以Apache用户身份运行,并且无法在主目录内部看到。 There isn't an issue when the mod_wsgi module is loaded by Apache as Apache is running as root at that point. 当Apache以root身份运行时,Apache加载了mod_wsgi模块时没有问题。

There are other issues with your configuration where don't follow best practices, but the above, especially the second item is likely the cause of your problem. 您的配置还有其他一些问题,这些问题没有遵循最佳实践,但是上述问题(尤其是第二项)可能是导致问题的原因。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 mod_wsgi:ImportError:没有名为&#39;encodings&#39;的模块 - mod_wsgi: ImportError: No module named 'encodings' Django mod_wsgi:ImportError:没有名为“ home”的模块 - Django mod_wsgi: ImportError: No module named 'home' Django Python mod_wsgi:ImportError:没有名为“ django”的模块 - Django Python mod_wsgi: ImportError: No module named 'django' Django + mod_wsgi 致命的 Python 错误:Py_Initialize:没有名为 Encodings 的模块 - Django + mod_wsgi Fatal Python Error: Py_Initialize: No module named Encodings Apache virtualenv和mod_wsgi:ImportError:没有名为&#39;django&#39;的模块 - Apache virtualenv and mod_wsgi : ImportError : No module named 'django' Django + mod_wsgi + apache2:ImportError:未命名模块<project> - Django + mod_wsgi + apache2: ImportError: No module named <project> Python,Django mod_wsgi,virtualenv错误:ImportError:没有名为os的模块(路径不正确) - Python, Django mod_wsgi, virtualenv errror: ImportError: No module named os (paths are not correct) 在Anaconda内部的Python上使用mod_wsgi配置Django时出错:“ImportError:没有名为django.core.wsgi的模块” - Error while configuring Django with mod_wsgi on Python inside Anaconda: “ImportError: No module named django.core.wsgi” Django/mod_wsgi/Apache - mod_wsgi 没有使用为它编译的 Python 版本 - “ModuleNotFoundError: No module named 'math'” - Django/mod_wsgi/Apache - mod_wsgi is not using the Python version it was compiled for - “ModuleNotFoundError: No module named 'math' ” WSGIDaemon进程语法错误-Mod_wsgi django ImportError:没有名为django.core.wsgi的模块 - WSGIDaemon Process syntax error - Mod_wsgi django ImportError: No module named django.core.wsgi
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM