【问题标题】:Setting up Apache and Python WSGI to use VirtualEnv设置 Apache 和 Python WSGI 以使用 VirtualEnv
【发布时间】:2011-10-07 02:23:47
【问题描述】:

我无法让 Apache/WSGI 使用我的 VirtualEnv。我已将以下两行(服务器上的路径指向目标 virtualenv 中站点包的实际位置)添加到我的 WSGI 文件中:

import site
site.addsitedir('/sites/mysite/virtpy/lib/python2.6/site-packages')

(来自http://www.foxhop.net/django-virtualenv-apache-mod_wsgi)。但是,当我尝试在浏览器中加载 url 时,我得到 500。检查 apache 日志:

 [Sun Jul 17 11:07:11 2011] [error] [client 94.170.105.142]     app =   import_module(appname)
[Sun Jul 17 11:07:11 2011] [error] [client 94.170.105.142]   File "/usr/local/lib/python2.6/dist-packages/django/utils/importlib.py", line 35, in import_module
[Sun Jul 17 11:07:11 2011] [error] [client 94.170.105.142]     __import__(name)
[Sun Jul 17 11:07:11 2011] [error] [client 94.170.105.142] TemplateSyntaxError: Caught ImportError while rendering: No module named tagging
[Sun Jul 17 11:07:11 2011] [debug] mod_deflate.c(615): [client 94.170.105.142] Zlib: Compressed 629 to 387 : URL /

所以我猜 VirtualEnv 没有被加载。有人知道如何告诉 Apache / WSGI 使用正确的 virtualenv 吗?

更新

我按照 Ken 的建议更新了 django.wsgi,但现在我在 apache 日志中收到以下错误

[Sun Jul 17 16:46:36 2011] [info] [client 94.170.105.142] mod_wsgi (pid=11260, process='', application='igniteflow-django.com:8090|'): Loading WSGI script '/sites/igniteflow/apache/django.wsgi'.
[Sun Jul 17 16:46:36 2011] [error] [client 94.170.105.142] mod_wsgi (pid=11260): Target WSGI script '/sites/igniteflow/apache/django.wsgi' cannot be loaded as Python module.
[Sun Jul 17 16:46:36 2011] [error] [client 94.170.105.142] mod_wsgi (pid=11260): Exception occurred processing WSGI script '/sites/igniteflow/apache/django.wsgi'.
[Sun Jul 17 16:46:36 2011] [error] [client 94.170.106.142] Traceback (most recent call last):
[Sun Jul 17 16:46:36 2011] [error] [client 94.170.105.142]   File "/sites/igniteflow/apache/django.wsgi", line 5, in <module>
[Sun Jul 17 16:46:36 2011] [error] [client 94.170.105.142]     execfile(activate_this, dict(__file__=activate_this))
[Sun Jul 17 16:46:36 2011] [error] [client 94.170.105.142] IOError: [Errno 13] Permission denied: '/root/.virtualenvs/igniteflow/bin/activate_this.py'

我认为这是因为 virtualenv 在 root 中并且 apache 没有权限?我将文件夹更改为 root:www-data 但它没有解决问题。有什么建议吗?

【问题讨论】:

  • 您确定标记库在您的虚拟环境的site-packages 目录中吗?根据您设置环境的方式,一些库是从 src 安装的,而不是在 site-packages 文件夹中

标签: django apache virtualenv wsgi


【解决方案1】:

在我的 app.wsgi 文件中,我有这样的东西。您需要将其更改为您的虚拟环境所在的位置,在本示例中,我的位于 /opt/ve/ve_name/ 下。

import os
# activate virtualenv
activate_this = os.path.expanduser("/opt/ve/ve_name/bin/activate_this.py")
execfile(activate_this, dict(__file__=activate_this))

【讨论】:

  • IOError: [Errno 13] Permission denied: '/root/.virtualenvs/site1/bin/activate_this.py' 你知道如何在apache可以访问的地方安装virtualenv吗?我尝试制作文件夹 root:www-data 但没有改变
  • 看起来你正在使用 virtualenvwrapper。要使用 virtualenvwrapper 更改 ve 的安装位置,您需要更改 WORKON_HOME 的值;导出 WORKON_HOME=/opt/Envs ;确保该目录存在,并且您需要将当前环境移动到那里,除非您想从头开始创建。在生产中我不费心使用 virtualenvwrapper,我只是使用 strait up virtualenv,它允许我将我的 ve 放在我想要的任何地方,但需要更多的手动工作。
  • 谢谢!这让它工作了。我将 bashrc 中的 WORKON_HOME 设置为 /sites/.virtualenv,使用 mkvirtualenv --no-site-packages 重新创建了 virtualenv,现在它已经全部启动并运行
猜你喜欢
  • 2013-04-20
  • 2010-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多