mod_wsgi: Unable to stat Python home and ImportError: No module named 'encodings'
mod_wsgi: Unable to stat Python home and ImportError: No module named 'encodings'
我正在尝试在 Apache 2.4 上使用 Django 和 mod_wsgi 在 Python 3.5.2 上构建一个网站。但是当 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'
所以,我研究了一些关于类似问题的文章,例如
Django + Apache + mod_wsgi permission denied
mod_wsgi: ImportError: No module named 'encodings'
但上述错误消息的原因尚未解决。
请注明要检查的点或要阅读的文件等。
我的开发环境如下
(1) 主机和 OS
- 托管服务:亚马逊 AWS EC2
- OS:亚马逊 Linux AMI 版本 2016.03
(2) Django 项目
我在目录 /home/ec2-user/django-sites 中创建了名为 testprj 的 Django 项目,用户帐户为 ec2-用户.
[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
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
。
我将 /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
我通过 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 服务器是 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 域] 就像
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]$
此致。
有几件事需要检查。
pyenv 工具默认不安装 Python 共享库。这可能会导致问题,因为 mod_wsgi 需要一个共享库。您需要明确告诉 pyenv 使用共享库构建 Python。
许多 Linux 系统上的主目录对其他用户不可读。当 mod_wsgi 被初始化时,它是 运行 作为 Apache 用户,将无法看到主目录的内部。当 Apache 加载 mod_wsgi 模块时没有问题,因为此时 Apache 是 运行 root。
您的配置存在其他未遵循最佳做法的问题,但上述情况,尤其是第二项可能是导致您出现问题的原因。
我正在尝试在 Apache 2.4 上使用 Django 和 mod_wsgi 在 Python 3.5.2 上构建一个网站。但是当 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'
所以,我研究了一些关于类似问题的文章,例如
Django + Apache + mod_wsgi permission denied
mod_wsgi: ImportError: No module named 'encodings'
但上述错误消息的原因尚未解决。
请注明要检查的点或要阅读的文件等。
我的开发环境如下
(1) 主机和 OS
- 托管服务:亚马逊 AWS EC2
- OS:亚马逊 Linux AMI 版本 2016.03
(2) Django 项目
我在目录 /home/ec2-user/django-sites 中创建了名为 testprj 的 Django 项目,用户帐户为 ec2-用户.
[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
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
。
我将 /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
我通过 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 服务器是 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 域] 就像
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]$
此致。
有几件事需要检查。
pyenv 工具默认不安装 Python 共享库。这可能会导致问题,因为 mod_wsgi 需要一个共享库。您需要明确告诉 pyenv 使用共享库构建 Python。
许多 Linux 系统上的主目录对其他用户不可读。当 mod_wsgi 被初始化时,它是 运行 作为 Apache 用户,将无法看到主目录的内部。当 Apache 加载 mod_wsgi 模块时没有问题,因为此时 Apache 是 运行 root。
您的配置存在其他未遵循最佳做法的问题,但上述情况,尤其是第二项可能是导致您出现问题的原因。