如何正确设置 Django+mod_wsgi 部署的 python 路径和权限?

发布于 2024-08-20 21:05:30 字数 2153 浏览 7 评论 0原文

我遇到的问题是我的 wsgi 文件无法正确导入 wsgi 处理程序。

/var/log/apache2/error.log 报告:

导入错误:没有命名的模块 django.core.handlers.wsgi

谷歌搜索这会带来几个结果,主要是处理权限错误,因为 www-data 无法读取某些文件和/或 pythonpath 不正确。有些解决方案含糊不清或者在我的情况下不起作用。

背景信息..

我的/usr/lib目录..

/usr/lib/python2.4
/usr/lib/python2.5
/usr/lib/python2.6
/usr/lib/python-django

默认的python版本是2.5.2。如果我以普通用户身份打开解释器,我可以毫无问题地 import django.core.handlers.wsgi

如果我切换到 www-data,Python 版本是相同的,并且我可以毫无问题地导入 django.core.handlers.wsgi 模块。

在我的 bashrc 中,我将 PYTHONPATH 设置为我的主目录,其中包含我所有的 django 站点...

export PYTHONPATH=/home/meder/django-sites/:$PYTHONPATH

所以目录结构是:

django-sites/
   test

test 是由 django-admin createproject

我的虚拟主机:

<VirtualHost *:80>
    ServerName beta.blah.com
    WSGIScriptAlias / /home/meder/django-sites/test/apache/django.wsgi
    Alias /media /home/meder/django-sites/test/media/
</VirtualHost>

/home/meder/django-sites/test/apache/django.wsgi 文件本身:

import os, sys

sys.path.append('/usr/local/django')
sys.path.append('/home/meder/django-sites')
sys.path.append('/home/meder/django-sites/test')
os.environ['DJANGO_SETTINGS_MODULE'] = 'test.settings'

import django.core.handlers.wsgi

application = django.core.handlers.wsgi.WSGIHandler()

最后,我的操作系统是 Debian Lenny,我从向后移植中获取了 django 1.1.1。希望这是足够的信息。

更新 #1 - 根据第一个回复,这是 ldd /usr/lib/apache2/modules/mod_wsgi.so 的结果:

meder@site:/usr/lib/apache2/modules$ ldd mod_wsgi.so
    libpython2.5.so.1.0 => /usr/lib/libpython2.5.so.1.0 (0xb7d99000)
    libpthread.so.0 => /lib/libpthread.so.0 (0xb7d81000)
    libdl.so.2 => /lib/libdl.so.2 (0xb7d7c000)
    libutil.so.1 => /lib/libutil.so.1 (0xb7d78000)
    libm.so.6 => /lib/libm.so.6 (0xb7d52000)
    libc.so.6 => /lib/libc.so.6 (0xb7c14000)
    /lib/ld-linux.so.2 (0xb7efd000)

因此它是针对 python 2.5 而不是 2.4 编译的。

The issue I'm having is my wsgi file can't import the wsgi handlers properly.

/var/log/apache2/error.log reports:

ImportError: No module named
django.core.handlers.wsgi

Googling this brings up a couple results, mostly dealing with permissions errors because www-data can't read certain files and/or the pythonpath is not correct. Some of the solutions are vague or just don't work in my circumstance.

Background information..

My /usr/lib directory..

/usr/lib/python2.4
/usr/lib/python2.5
/usr/lib/python2.6
/usr/lib/python-django

The default python version is 2.5.2. If I open the interpreter as a regular user I can import django.core.handlers.wsgi with no issues.

If I switch to www-data the python version is the same, and I can import the django.core.handlers.wsgi module no problem.

In my bashrc, I set my PYTHONPATH to my home directory which contains all my django sites...

export PYTHONPATH=/home/meder/django-sites/:$PYTHONPATH

So the directory structure is:

django-sites/
   test

test is the directory created by django-admin createproject.

My virtualhost:

<VirtualHost *:80>
    ServerName beta.blah.com
    WSGIScriptAlias / /home/meder/django-sites/test/apache/django.wsgi
    Alias /media /home/meder/django-sites/test/media/
</VirtualHost>

The /home/meder/django-sites/test/apache/django.wsgi file itself:

import os, sys

sys.path.append('/usr/local/django')
sys.path.append('/home/meder/django-sites')
sys.path.append('/home/meder/django-sites/test')
os.environ['DJANGO_SETTINGS_MODULE'] = 'test.settings'

import django.core.handlers.wsgi

application = django.core.handlers.wsgi.WSGIHandler()

Finally, my OS is Debian Lenny and I grabbed django 1.1.1 from backports. Hope that's enough information.

Update #1 - per the first reply here's the result of ldd /usr/lib/apache2/modules/mod_wsgi.so:

meder@site:/usr/lib/apache2/modules$ ldd mod_wsgi.so
    libpython2.5.so.1.0 => /usr/lib/libpython2.5.so.1.0 (0xb7d99000)
    libpthread.so.0 => /lib/libpthread.so.0 (0xb7d81000)
    libdl.so.2 => /lib/libdl.so.2 (0xb7d7c000)
    libutil.so.1 => /lib/libutil.so.1 (0xb7d78000)
    libm.so.6 => /lib/libm.so.6 (0xb7d52000)
    libc.so.6 => /lib/libc.so.6 (0xb7c14000)
    /lib/ld-linux.so.2 (0xb7efd000)

So it is compiled against python 2.5 and not 2.4.

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

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

发布评论

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

评论(3

苹果你个爱泡泡 2024-08-27 21:05:30

由于我使用的是 Debian,因此 django 似乎位于 /usr/lib/pymodules/python2.5 中,而不是 /usr/lib/python2.5/site-packages

我添加

sys.path.append('/usr/lib/pymodules/python2.5') 

到我的 wsgi 文件的顶部并且做到了,尽管我觉得我应该以更正确的方式修复这个问题。

Since I'm on Debian it appears that django is in /usr/lib/pymodules/python2.5 and not /usr/lib/python2.5/site-packages.

I added

sys.path.append('/usr/lib/pymodules/python2.5') 

to the top of my wsgi file and that did it, although I feel as though I should be fixing this in a more proper manner.

土豪我们做朋友吧 2024-08-27 21:05:30

我不认为你的问题出在 sys.path 上。我总是使用 Mod_WSGI 和 Django 使用守护进程,就像这样,

# Note these 2 lines
WSGIDaemonProcess site-1 user=user-1 group=user-1 threads=25
WSGIProcessGroup site-1

Alias /media/ /usr/local/django/mysite/media/

<Directory /usr/local/django/mysite/media>
Order deny,allow
Allow from all
</Directory>

WSGIScriptAlias / /usr/local/django/mysite/apache/django.wsgi

<Directory /usr/local/django/mysite/apache>
Order deny,allow
Allow from all

如果您注意到前两行 - 您可以指定将运行它的组和用户。在您的情况下,您提到 www-data 可以导入 django 模块,但当 Apache 部署它时它不起作用 - 也许该进程没有由任何人运行,或者由其他一些用户/组运行 具有导入此模块的权限。添加 DaemonProcess 和 Group 行应该可以解决您的问题。

HTH。

[1] 供参考 - 这是 Django Mod_WSGI 文档 - http://code.google。 com/p/modwsgi/wiki/IntegrationWithDjango

I don't think your problem lies with the sys.path. I've always used Mod_WSGI with Django using the Daemonized process, like so,

# Note these 2 lines
WSGIDaemonProcess site-1 user=user-1 group=user-1 threads=25
WSGIProcessGroup site-1

Alias /media/ /usr/local/django/mysite/media/

<Directory /usr/local/django/mysite/media>
Order deny,allow
Allow from all
</Directory>

WSGIScriptAlias / /usr/local/django/mysite/apache/django.wsgi

<Directory /usr/local/django/mysite/apache>
Order deny,allow
Allow from all

If you note the first 2 lines - you can specify the group and the user which will be running this. In your case, you mention that www-data can import the django module but it doesn't work when Apache deploys it - perhaps the process is being run by nobody, or some other user/group that does not have privileges to import this module. Adding the DaemonProcess and Group lines should solve your problem.

HTH.

[1] For reference - here's the Django Mod_WSGI doc - http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango

绻影浮沉 2024-08-27 21:05:30

听起来 mod_wsgi 不是针对 Python 2.5 编译的,而是针对 Python 2.4 或 2.6 编译的。 在安装它的 mod_wsgi.so 文件上运行:

ldd mod_wsgi.so

以找出它正在使用的内容。

如果不同,您将需要从源代码重新编译 mod_wsgi,以便它使用您想要使用的版本。

Sounds like you mod_wsgi is not compiled against Python 2.5 and instead compiled against Python 2.4 or 2.6. Run:

ldd mod_wsgi.so

on the mod_wsgi.so file where it is installed to work out what it is using.

If it is different, you will need to recompile mod_wsgi from source code such that it uses the version you want to use.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文