设置 Apache 和 Python WSGI 以使用 VirtualEnv

发布于 2024-11-24 22:38:26 字数 2312 浏览 1 评论 0原文

我在让 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 但它还没有解决问题。有什么建议吗?

I'm having trouble getting Apache/WSGI to use my VirtualEnv. I have added the following two lines (path on server is pointing to the actual location of site-packages in the target virtualenv) to my WSGI file:

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

(from http://www.foxhop.net/django-virtualenv-apache-mod_wsgi). However, when I try to load the url in the browser I get a 500. Checking the apache logs:

 [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 /

So I guess that the VirtualEnv isn't being loaded. Anyone know how to tell Apache / WSGI the correct virtualenv to use?

UPDATE

I have updated django.wsgi following Ken's advice, but now I am getting the following error in the apache log

[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'

I assume this is because the virtualenv is in root and apache doesn't have permissions? I chowned the folder to root:www-data but it hasn't fixed the problem. Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

叹梦 2024-12-01 22:38:26

在我的 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))

In my app.wsgi file I have something like this. You will need to change it to put to where your virtual env is located, mine is under /opt/ve/ve_name/ in this example.

import os
# activate virtualenv
activate_this = os.path.expanduser("/opt/ve/ve_name/bin/activate_this.py")
execfile(activate_this, dict(__file__=activate_this))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文