【问题标题】:Installing Django with mod_wsgi in centos, ImportError: No module named oscentos中使用mod_wsgi安装Django,ImportError: No module named os
【发布时间】:2012-08-24 00:21:55
【问题描述】:

我有,这个 httpd.conf

NameVirtualHost *:80
<VirtualHost *:80>
    ServerName www.hello2.ch
    DocumentRoot /home/django_www/hello
    WSGIScriptAlias / /var/www/django_www/hardi/django.wsgi
    ErrorLog /var/www/django_www/hardi/error_log
    CustomLog /var/www/django_www/hardi/access_log common
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot /var/www/html
</VirtualHost>

vi /etc/hosts

127.0.0.1       localhost.localdomain localhost
128.178.131.89  www.hello2.ch
128.178.131.89  ohtersite.ch

python 2.5、mod_wsgi-2.5、/root/epd-5.1.0/lib/libpython2.5.so.1.0:已安装!

ldd /root/epd-5.1.0/bin/python

libpython2.5.so.1.0 => /root/epd-5.1.0/lib/libpython2.5.so.1.0 (0x00002b1bb7333000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x0000003da0600000)
libdl.so.2 => /lib64/libdl.so.2 (0x0000003d9fe00000)
libutil.so.1 => /lib64/libutil.so.1 (0x0000003dadc00000)
libm.so.6 => /lib64/libm.so.6 (0x0000003da0200000)
libc.so.6 => /lib64/libc.so.6 (0x0000003d9fa00000)
/lib64/ld-linux-x86-64.so.2 (0x0000003d9f600000)

回显 $LD_LIBRARY_PATH

/root/epd-5.1.0/lib/

但是当我尝试加载网页时,我得到以下错误日志:

'import site' failed; use -v for traceback
[Wed Aug 29 11:52:57 2012] [notice] suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)
[Wed Aug 29 11:52:57 2012] [notice] Digest: generating secret for digest authentication ...
[Wed Aug 29 11:52:57 2012] [notice] Digest: done
[Wed Aug 29 11:52:57 2012] [notice] mod_python: Creating 4 session mutexes based on 256 max processes and 0 max threads.
[Wed Aug 29 11:52:57 2012] [notice] Apache/2.2.3 (Red Hat) configured -- resuming normal operations
[Wed Aug 29 11:55:00 2012] [error] [client 66.249.72.187] Symbolic link not allowed or link target not accessible: /var/www/html/app/webroot/w3
[Wed Aug 29 11:58:30 2012] [error] [client 66.249.72.187] Symbolic link not allowed or link target not accessible: /var/www/html/app/webroot/w3
[Wed Aug 29 12:01:58 2012] [error] [client 66.249.72.187] Symbolic link not allowed or link target not accessible: /var/www/html/app/webroot/w3
[Wed Aug 29 12:05:30 2012] [error] [client 66.249.72.187] Symbolic link not allowed or link target not accessible: /var/www/html/app/webroot/w3
[Wed Aug 29 12:09:00 2012] [error] [client 66.249.72.187] Symbolic link not allowed or link target not accessible: /var/www/html/app/webroot/w3
[Wed Aug 29 12:12:29 2012] [error] [client 66.249.72.187] Symbolic link not allowed or link target not accessible: /var/www/html/app/webroot/w3
[Wed Aug 29 12:14:06 2012] [error] [client 178.33.137.225] Symbolic link not allowed or link target not accessible: /var/www/html/app/webroot/w3
[Wed Aug 29 12:15:59 2012] [error] [client 66.249.72.187] Symbolic link not allowed or link target not accessible: /var/www/html/app/webroot/w3
Could not find platform independent libraries <prefix>
Could not find platform dependent libraries <exec_prefix>
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
'import site' failed; use -v for traceback
Could not find platform independent libraries <prefix>
Could not find platform dependent libraries <exec_prefix>
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
....
'import site' failed; use -v for traceback
[Wed Aug 29 12:17:47 2012] [notice] caught SIGTERM, shutting down
Could not find platform independent libraries <prefix>
Could not find platform dependent libraries <exec_prefix>
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
'import site' failed; use -v for traceback
[Wed Aug 29 12:17:47 2012] [notice] suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)
[Wed Aug 29 12:17:47 2012] [notice] Digest: generating secret for digest authentication ...
[Wed Aug 29 12:17:47 2012] [notice] Digest: done
[Wed Aug 29 12:17:47 2012] [notice] mod_python: Creating 4 session mutexes based on 256 max processes and 0 max threads.
[Wed Aug 29 12:17:47 2012] [notice] Apache/2.2.3 (Red Hat) configured -- resuming normal operations
[Wed Aug 29 12:18:01 2012] [error] Traceback (most recent call last):
[Wed Aug 29 12:18:01 2012] [error]   File "/var/www/django_www/hardi/django.wsgi", line 1, in <module>
[Wed Aug 29 12:18:01 2012] [error]     import os
[Wed Aug 29 12:18:01 2012] [error] ImportError: No module named os

关于如何使 Django 与 mod_wsgi 一起工作的任何想法,以及如何使第一个 VirtualHost 条目与 www.hello2.ch 网页以及第二个 VirtualHost 条目中的所有其他条目相匹配?

【问题讨论】:

  • 当你在命令行中输入python 时,python shell 会打开吗?
  • 嗯。好的,在 shell 中尝试import os。如果您遇到相同的导入错误,那么问题出在您的 python 安装上,而不是 mod_wsgi 或 django。
  • 只是一个提示(与您的问题无关),但不要将 python2.5 与 mod_wsgi 一起使用。由于内存分配问题,python2.5 中存在一个错误,导致此处和此处出现错误 500 页面。
  • /var/www/html/app/webroot/w3 如何融入这一切?
  • @Jingo:所以,我最好安装另一个版本的python并删除2.5..?事实上,我在服务器上托管了一个静态网站,我不认为这种更改会造成任何麻烦。

标签: django centos mod-wsgi


【解决方案1】:

在 mod_wsgi.so 上运行“ldd”。

问题可能在于它正在寻找不同的 Python 版本。也可能是因为您的 Python 不在标准位置,所以它不知道在哪里可以找到安装。

在后一种情况下,在您的 Apache 配置中在 VirtualHost 之外的全局范围内添加:

WSGIPythonHome /root/epd-5.1.0

该值应与运行时从 Python 打印的 sys.prefix 的值相对应。

import sys
print sys.prefix

【讨论】:

  • 现在,我在重新启动 apache 时收到此错误:/ --> 启动 httpd: httpd: /etc/httpd/conf/httpd.conf 第 202 行的语法错误:无法加载 /etc/httpd /modules/mod_wsgi.so 进入服务器:libpython2.5.so.1.0:无法打开共享对象文件:没有这样的文件或目录
  • 根据code.google.com/p/modwsgi/wiki/… 的文档,将 LD_RUN_PATH 环境变量设置为包含 Python 库的目录,因此可以在运行时找到,因为不在标准位置。 LD_RUN_PATH=/root/epd-5.1.0/lib make
【解决方案2】:

根据您遇到的错误,似乎有几件事可能正在发生。但是,我不会将所有这些都扔到 cmets 中,而是继续发布作为答案。

在我的 django.wsgi 文件中,我有以下两行更改 PATH 环境变量以让 python 从我的 django 项目中导入包:

import sys
sys.path.append('/path/to/mysite.com/app/mydjangoproject')

您还需要确保将以下内容添加到 httpd.conf 中的 VirtualHost 中,以便您将 django 用于:

<Directory /path/to/mysite.com/app/mydjangoproject>
    Order allow,deny
    Allow from all
</Directory>

编辑:

关于您在错误消息中出现的 cmets 中提到的符号链接,我不太确定该怎么做。如果您只是尝试使用它来允许 apache 访问 DocumentRoot 之外的资源,您需要为包含符号链接的 Directory 添加 Options FollowSymLinks 指令

【讨论】:

  • 我只是想删除符号链接,它指向一个不存在的文件。
  • 我会说,一定要继续。听起来,如果它指向 @ nothing,那么它最初创建的任何意图都不再相关。
  • [root@lts5srv1 ~]# ldd /usr/lib64/httpd/modules/mod_wsgi.so --> libpython2.5.so.1.0 => 未找到 ...
猜你喜欢
  • 2017-03-03
  • 1970-01-01
  • 2012-09-14
  • 2012-04-21
  • 2015-01-15
  • 1970-01-01
  • 1970-01-01
  • 2011-04-04
  • 2015-11-24
相关资源
最近更新 更多