jun*_*ykt 10 python apache django mod-wsgi
我正在尝试在 Python 3.5.2 上使用 Django 和 mod_wsgi 在 Apache 2.4 上构建一个网站。但是当 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 解释器可能无法正确初始化。验证提供的路径和整个路径的访问权限。
致命的 Python 错误:Py_Initialize:无法获取语言环境编码
ImportError:没有名为“encodings”的模块
所以,我探索了一些关于类似问题的文章,例如
但上述错误信息的原因尚未解决。
请注明需要检查的要点或需要阅读的文件等。
我的开发环境如下。
(1) 主机和操作系统
(2) Django 项目
我在/home/ec2-user/django-sites目录中创建了名为testprj 的Django 项目 ,用户帐户为ec2-user。
[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
Run Code Online (Sandbox Code Playgroud)
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.
Run Code Online (Sandbox Code Playgroud)
(3) Python的环境
我安装了 Python 3.5.2 pyenv install 3.5.2。然后我设置了
pyenv virtualenv 3.5.2 django-sites。我将 /home/ec2-user/django-sites 的 local 设置为 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
Run Code Online (Sandbox Code Playgroud)
我通过 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)
Run Code Online (Sandbox Code Playgroud)
(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
Run Code Online (Sandbox Code Playgroud)
执行Apache的用户和组都是apache作为/etc/httpd/conf/httpd.conf中的下一行。
User apache
Group apache
Run Code Online (Sandbox Code Playgroud)
(5) Django 项目 testprj 的附加 conf 文件
我打算配置这样的 URL
http://[MyEC2 domain]/testprj/
Run Code Online (Sandbox Code Playgroud)
可以访问位于 /home/ec2-user/django-sites/testprj 的 Django 项目的首页。
[MyEC2 域] 上面就像
ec2-XX-XX-XX-XX.us-west-2.compute.amazonaws.com
Run Code Online (Sandbox Code Playgroud)
所以,我在 /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
Run Code Online (Sandbox Code Playgroud)
[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
Run Code Online (Sandbox Code Playgroud)
[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.
Run Code Online (Sandbox Code Playgroud)
此致。
有几件事需要检查。
默认情况下,pyenv 工具不会安装带有共享库的 Python。这可能会导致问题,因为 mod_wsgi 需要共享库。您需要明确告诉 pyenv 使用共享库构建 Python。
许多 Linux 系统上的主目录对于其他用户来说是无法读取的。当 mod_wsgi 初始化时,它以 Apache 用户身份运行,并且无法看到主目录的内部。当 Apache 加载 mod_wsgi 模块时,不会出现问题,因为此时 Apache 是以 root 身份运行的。
您的配置还存在其他问题,不遵循最佳实践,但上述内容,尤其是第二项,可能是导致问题的原因。