Python

Django 不在 CentOS 上執行,但簡單的 wsgi 範例有效

  • July 19, 2010

我的 django.wsgi

import os
import sys

sys.path.append('/var/www/django')
os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp.settings'
os.environ['PYTHON_EGG_CACHE'] = '/var/www/django/myapp/.python-eggs'

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

http.conf

<VirtualHost 127.0.0.1:80>
   DocumentRoot /var/www/default
   ServerName localhost
   WSGIScriptAlias /myapp /var/www/django/myapp/apache/django.wsgi
   ErrorLog /var/www/django/myapp/logs/error_log
</VirtualHost>

Apache 錯誤說明如下

當我去 localhost/myapp 然後它給出了一些伺服器錯誤

[Fri Jun 25 12:42:42 2010] [error] [client 127.0.0.1] File does not exist: /var/www/default/favicon.ico
[Fri Jun 25 12:42:45 2010] [error] [client 127.0.0.1] File does not exist: /var/www/default/favicon.ico
[Fri Jun 25 12:42:48 2010] [info] [client 127.0.0.1] mod_wsgi (pid=3585, process='', application='localhost|/myapp'): Loading WSGI script '/var/www/django/myapp/apache/django.wsgi'.
[Fri Jun 25 12:42:48 2010] [error] [client 127.0.0.1] mod_wsgi (pid=3585): Target WSGI script '/var/www/django/myapp/apache/django.wsgi' cannot be loaded as Python module.
[Fri Jun 25 12:42:48 2010] [error] [client 127.0.0.1] mod_wsgi (pid=3585): Exception occurred processing WSGI script '/var/www/django/myapp/apache/django.wsgi'.
[Fri Jun 25 12:42:48 2010] [error] [client 127.0.0.1] Traceback (most recent call last):
[Fri Jun 25 12:42:48 2010] [error] [client 127.0.0.1]   File "/var/www/django/myapp/apache/django.wsgi", line 8, in <module>
[Fri Jun 25 12:42:48 2010] [error] [client 127.0.0.1]     import django.core.handlers.wsgi
[Fri Jun 25 12:42:48 2010] [error] [client 127.0.0.1]   File "/opt/python2.6/lib/python2.6/site-packages/django/core/handlers/wsgi.py", line 1, in <module>
[Fri Jun 25 12:42:48 2010] [error] [client 127.0.0.1]     from threading import Lock
[Fri Jun 25 12:42:48 2010] [error] [client 127.0.0.1]   File "/opt/python2.6/lib/python2.6/threading.py", line 13, in <module>
[Fri Jun 25 12:42:48 2010] [error] [client 127.0.0.1]     from functools import wraps
[Fri Jun 25 12:42:48 2010] [error] [client 127.0.0.1]   File "/opt/python2.6/lib/python2.6/functools.py", line 10, in <module>
[Fri Jun 25 12:42:48 2010] [error] [client 127.0.0.1]     from _functools import partial, reduce
[Fri Jun 25 12:42:48 2010] [error] [client 127.0.0.1] ImportError: /opt/python2.6/lib/python2.6/lib-dynload/_functools.so: failed to map segment from shared object: Permission denied

ldd 的輸出:

linux-gate.so.1 =>  (0x0098c000)
       libpython2.6.so.1.0 => /usr/lib/libpython2.6.so.1.0 (0x00423000)
       libpthread.so.0 => /lib/libpthread.so.0 (0x005cd000)
       libdl.so.2 => /lib/libdl.so.2 (0x00110000)
       libutil.so.1 => /lib/libutil.so.1 (0x006d8000)
       libm.so.6 => /lib/libm.so.6 (0x00778000)
       libc.so.6 => /lib/libc.so.6 (0x00a46000)
       /lib/ld-linux.so.2 (0x002e5000)

我嘗試了 import sys 並沒有迅速出現錯誤,唯一的一行是

[Mon Jun 28 13:33:24 2010] [info] [client 127.0.0.1] mod_wsgi (pid=19384, process='', application='python.localhost|/application'): Loading WSGI script '/var/www/application/application.wsgi'.

什麼平台?為作業系統安裝了哪個確切版本的 Python?/opt/python2.6 中安裝了哪個確切版本的 Python?當您對已安裝的 mod_wsgi Apache 模組 .so 文件執行“ldd mod_wsgi.so”時會得到什麼?您是否偶然在配置中設置了 WSGIPythonHome?

總體而言,這看起來有點像您可能正在為不同版本選擇 Python 共享庫或安裝的 Python lib 目錄,並且存在不兼容性。

還可以返回並獲得一個 hello world WSGI 腳本而不是 Django,當它工作時添加到它:

import threading

所以你可以看看你是否可以創建一個獨立的測試來調查獨立於 Django 發生的事情。

對於 hello world 範例,請參見:

http://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide

最後,為了確保您沒有在同一個 Apache 實例中載入 mod_python 以確保良好的衡量標準。

引用自:https://serverfault.com/questions/154695