Django Apache and Virtualenv ImportError: No module named site

Django Apache and Virtualenv ImportError: No module named site

504 页面后来自 apache 的错误

[info] mod_wsgi (pid=): Python home /var/venv/mybox.
[info] mod_wsgi (pid=): Initializing Python.
ImportError: No module named site

这是一个几乎没有配置的应用程序。

<IfModule mod_wsgi.c>
WSGIDaemonProcess myapp python-home=/var/venv/mybox
WSGIProcessGroup myapp
WSGIScriptAlias / /var/www/html/web/myapp/wsgi.py
WSGISocketPrefix /var/run/wsgi

<Directory /var/www/html/web>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
</IfModule>

尽我所能遵循每个 post 和教程。我在 CENTOS6 上。使用 virutal env python 2.7 默认系统环境是 2.6

$ ldd /etc/httpd/modules/mod_wsgi.so
  linux-vdso.so.1 =>  (0x00007ffc06174000)

mywsgi.py

 import os,sys     
 from django.core.wsgi import get_wsgi_application     
 os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")
 sys.path.insert(0,'/var/www/html/web')
 activate_this = '/var/venv/mybox/bin/activate_this.py'
 execfile(activate_this, dict(__file__=activate_this))
 application = get_wsgi_application()

PYHTONHOME 未设置

这是取自 Documentation 写这个: WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py WSGIPythonPath /path/to/mysite.com

<Directory /path/to/mysite.com/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>

这是专门针对虚拟环境的,您需要将路径写入您的 python 虚拟环境:

WSGIPythonPath /path/to/mysite.com:/path/to/your/venv/lib/python3.X/site-packages

问题也可能出在 - PYTHONHOME

Change the location of the standard Python libraries. By default, the libraries are searched in prefix/lib/pythonversion and exec_prefix/lib/pythonversion, where prefix and exec_prefix are installation-dependent directories, both defaulting to /usr/local.

When PYTHONHOME is set to a single directory, its value replaces both prefix and exec_prefix. To specify different values for these, set PYTHONHOME to prefix:exec_prefix.

尝试清理你的 PYTHONHOME:

user$ export PYTHONHOME=

在 mod_wsgi 中使用虚拟环境的文档可以在以下位置找到:

在您的案例中最重要的部分是:

在那一节中指出:

When using a Python virtual environment with mod_wsgi, it is very important that it has been created using the same Python installation that mod_wsgi was originally compiled for. It is not possible to use a Python virtual environment to force mod_wsgi to use a different Python version, or even a different Python installation.

You cannot for example force mod_wsgi to use a Python virtual environment created using Python 3.5 when mod_wsgi was originally compiled for Python 2.7. This is because the Python library for the Python installation it was originally compiled against is linked directly into the mod_wsgi module.

所以很可能发生的事情是 mod_wsgi 是为 Python 2.6 编译的。在这种情况下,您不能强制它使用从 Python 2.7 创建的 Python 虚拟环境。当您执行此操作时,您将看到关于缺少 site 模块的错误。

您需要从系统包中卸载 mod_wsgi 并从源代码安装 mod_wsgi,针对 Python 2.7 进行编译。最简单的方法可能是使用 pip install 中描述的方法:

运行 pip install 将其安装在您的虚拟环境中,然后按照 'Connecting into Apache installation' 部分中有关配置 Apache 以使用它的说明进行操作。

已在 CentOS 7 和 Apache 2.4.6 中解决

我的整个服务器使用 Python 2.7,但我已经安装了 Python 3.6,我的 virtualenv 使用的是 Python 3.6。

使用此代码配置 djang.conf (/etc/httpd/conf.d/django.conf) 后:

<VirtualHost *:80>

WSGIDaemonProcess myProj python-home=/home/user/django-site/env python-path=/home/user/django-site
WSGIProcessGroup myProj
WSGIScriptAlias /myProj /home/user/django-site/my-project/wsgi.py


Alias /static /home/user/django-site/static
<Directory /home/user/django-site/static>
    Require all granted
</Directory>

<Directory /home/user/django-site/my-project>
    <Files wsgi.py>
        Require all granted
    </Files>
</Directory>

</VirtualHost>

并重新启动我的 apache

sudo systemctl restart httpd

我在一千行中遇到了这个错误 (/var/log/httpd/error_log)

ImportError: No module named site
ImportError: No module named site
ImportError: No module named site
ImportError: No module named site

解决方案

第一个:

sudo grep wsgi /var/log/httpd/error_log

我知道了:

[mpm_prefork:notice] [pid 62324] AH00163: Apache/2.4.6 (CentOS) PHP/7.0.33 mod_wsgi/3.4 Python/2.7.5 configured -- resuming normal operations

注意 Python 版本 (2.7.5)。根据我的 Python 3.6,我为获得 mod_wsgi 所做的是使用:

yum list *mod_wsgi*
Installed packages
mod_wsgi.x86_64                          3.4-18.el7                          @base
Disponible packages
python35u-mod_wsgi.x86_64                4.6.2-1.ius.centos7                 ius
python36u-mod_wsgi.x86_64                4.6.2-1.ius.centos7                 ius

然后我安装了包 python36u-mod_wsgi.x86_64:

sudo yum install python36u-mod_wsgi.x86_64

然后我重启了Apache服务:

sudo systemctl restart httpd

并从日志中获取了这一新行:

[Fri Mar 29 12:33:26.788716 2019] [mpm_prefork:notice] [pid 76317] AH00163: Apache/2.4.6 (CentOS) PHP/7.0.33 mod_wsgi/4.6.2 Python/3.6 configured -- resuming normal operations

一切正常! :-)

希望对您有所帮助。 C 呀!

在 Ubuntu 上,如果您想使用 python3,则必须卸载 libapache2-mod-wsgi-py 并安装 libapache2-mod-wsgi-py3(如果您获得 mod_wsgi)来自存储库。

我在 centos6.X 上使用 python 2.7,重新安装 mod_wsgi 已经为我解决了这个问题。

yum remove mod_wsgi.x86_64
yum install mod_wsgi.x86_64
/etc/init.d/httpd restart