Apache-2.2

嘗試在 Ubuntu Lucid Lynx 上使用 start Apache2 mod_wsgi 執行 Django,但沒有成功

  • April 15, 2011

我有一個小型vps,至少安裝了ubuntu lucid-lynx,記憶體約為 256mb,它是新安裝的,沒有什麼特別的執行。我正在嘗試將django部署到它,當我使用 manage.py 成功執行伺服器時,我無法讓wsgi工作apache :

# service apache2 start && service apache2 status
* Starting web server apache2                      [ OK ] 
Apache is NOT running.

錯誤日誌/var/log/apache2/error.log

[Thu Apr 14 21:17:29 2011] [warn] pid file /var/run/apache2.pid overwritten -- Unclean shutdown of previous Apache run?
[Thu Apr 14 21:17:29 2011] [notice] Apache/2.2.14 (Ubuntu) mod_wsgi/2.8 Python/2.6.5 configured -- resuming normal operations
[Thu Apr 14 21:17:29 2011] [alert] (11)Resource temporarily unavailable: apr_thread_create: unable to create worker thread
[Thu Apr 14 21:17:29 2011] [error] Exception KeyError: KeyError(-1216795792,) in <module 'threading' from '/usr/lib/python2.6/threading.pyc'> ignored
[Thu Apr 14 21:17:29 2011] [alert] (11)Resource temporarily unavailable: apr_thread_create: unable to create worker thread
[Thu Apr 14 21:17:29 2011] [error] Exception KeyError: KeyError(-1216795792,) in <module 'threading' from '/usr/lib/python2.6/threading.pyc'> ignored
[Thu Apr 14 21:17:31 2011] [alert] No active workers found... Apache is exiting!

我的apache配置文件/etc/apache2/httpd.conf

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

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

/usr/local/django/deals/apache/django.wsgi文件:

import os
import sys

path = '/usr/local/django/deals'
if path not in sys.path:
   sys.path.append(path)

os.environ['DJANGO_SETTINGS_MODULE'] = 'deals.settings'

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

似乎已安裝:

# dpkg -l \*apache\* |grep -E '^ii'
ii  apache2                          2.2.14-5ubuntu8.4                        Apache HTTP Server metapackage
ii  apache2-mpm-worker               2.2.14-5ubuntu8.4                        Apache HTTP Server - high speed threaded mod
ii  apache2-utils                    2.2.14-5ubuntu8.4                        utility programs for webservers
ii  apache2.2-bin                    2.2.14-5ubuntu8.4                        Apache HTTP Server common binary files
ii  apache2.2-common                 2.2.14-5ubuntu8.4                        Apache HTTP Server common files
ii  libapache2-mod-wsgi              2.8-2ubuntu1                             Python WSGI adapter module for Apache

我終於想出瞭如何解決這個問題。首先,我在將ubuntu10.04升級到10.10時遇到了麻煩,因為伺服器僅提供 128mb(256mb 可突發)並且在升級過程中用完了。也許這個問題的解決方案是簡單地升級ubuntu。但是,我確實通過按照此處所述從源安裝它來升級mod-wsgi,但這似乎沒有任何影響。

當我安裝apache2-mpm-prefork時,突破就來了,apt-get install apache2-mpm-prefork所以它會使用它而不是apache-mpm-worker,就像dimmer建議的那樣。我不確定問題是否是apache-mpm-worker導致我的其他錯誤沒有被記錄,也許下一個遇到問題的人可以嘗試跳過這一步。當我切換到apache-mpm-worker時,apache錯誤日誌給出了以下錯誤。

mod_wsgi (pid=1436): Exception occurred processing WSGI script '/usr/local/django/deals/apache/django.wsgi'.
Traceback (most recent call last):
  File "/usr/local/lib/python2.6/dist-packages/Django-1.3-py2.6.egg/django/core/handlers/wsgi.py", line 250, in __call__
    self.load_middleware()
  File "/usr/local/lib/python2.6/dist-packages/Django-1.3-py2.6.egg/django/core/handlers/base.py", line 39, in load_middleware
    for middleware_path in settings.MIDDLEWARE_CLASSES:
  File "/usr/local/lib/python2.6/dist-packages/Django-1.3-py2.6.egg/django/utils/functional.py", line 276, in __getattr__
    self._setup()
  File "/usr/local/lib/python2.6/dist-packages/Django-1.3-py2.6.egg/django/conf/__init__.py", line 42, in _setup
    self._wrapped = Settings(settings_module)
  File "/usr/local/lib/python2.6/dist-packages/Django-1.3-py2.6.egg/django/conf/__init__.py", line 89, in __init__
    raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'deals.settings' (Is it on sys.path?): No module named deals.setting

這是因為我們無法導入設置模組。wsgi 與 Django 文件的集成解釋說我需要添加包含路徑,一旦我更新/usr/local/django/deals/apache/django.wsgi了一切執行順利。

import os, sys

sys.path.append('/usr/local/django/deals')
sys.path.append('/usr/local/django')

os.environ['DJANGO_SETTINGS_MODULE'] = 'deals.settings'

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

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