2014-05-23 54 views
1

我想部署我的Django應用程序到Apache服務器沒有運氣。我成功地使用了WSGI示例應用程序,並試圖託管一個空的Django項目。雖然它與manage.py runserver命令正常工作,它使用Apache時引發以下錯誤:Django與Apache和wsgi拋出ImportError

[notice] Apache/2.2.22 (Debian) PHP/5.4.4-14+deb7u9 mod_python/3.3.1 Python/2.7.3 mod_wsgi/2.7 configured -- resuming normal operations 
[error] [client x.x.x.x] mod_wsgi (pid=8300): Exception occurred processing WSGI script '/usr/local/www/django/myapp/wsgi.py'. 
[error] [client x.x.x.x] Traceback (most recent call last): 
[error] [client x.x.x.x] File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 187, in __call__ 
[error] [client x.x.x.x]  self.load_middleware() 
[error] [client x.x.x.x] File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 44, in load_middleware 
[error] [client x.x.x.x]  for middleware_path in settings.MIDDLEWARE_CLASSES: 
[error] [client x.x.x.x] File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 54, in __getattr__ 
[error] [client x.x.x.x]  self._setup(name) 
[error] [client x.x.x.x] File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 49, in _setup 
[error] [client x.x.x.x]  self._wrapped = Settings(settings_module) 
[error] [client x.x.x.x] File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 132, in __init__ 
[error] [client x.x.x.x]  % (self.SETTINGS_MODULE, e) 
[error] [client x.x.x.x] ImportError: Could not import settings 'myapp.settings' (Is it on sys.path? Is there an import error in the settings file?): No module named myapp.settings 

我wsgi.py如下:

""" 
WSGI config for myapp project. 

It exposes the WSGI callable as a module-level variable named ``application``. 

For more information on this file, see 
https://docs.djangoproject.com/en/1.6/howto/deployment/wsgi/ 
""" 

import os 
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings") 

from django.core.wsgi import get_wsgi_application 
application = get_wsgi_application() 

我在有一個wsgi.conf Apache的conf.d庫:

<VirtualHost *:80> 
ServerName myapp.example.com 
ServerAlias myapp 
ServerAdmin [email protected] 
DocumentRoot /var/www 

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

WSGIDaemonProcess myapp processes=2 threads=15 display-name=%{GROUP} 
WSGIProcessGroup myapp 

WSGIScriptAlias /myapp /usr/local/www/django/myapp/wsgi.py 
LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so 

</VirtualHost> 

WSGIPythonPath /usr/local/www/django/myapp 

[解決] 謝謝,我又從頭開始,取得了建議修改我的配置文件,現在它的工作。我無法舉出兩個建議都是正確的,但我認爲他們都是必要的,我有第三個(第四,第五......)個錯誤,在重新安裝後就消失了。

+0

什麼是您的應用程序佈局?設置文件到底在哪裏? –

+0

應用程序佈局是由django生成的。 –

+0

那麼究竟是什麼*完整的文件路徑* settings.py? –

回答

2

它看起來像你一直在使用舊指南設置apache2/wsgi。我建議使用官方指南https://code.google.com/p/modwsgi/wiki/InstallationInstructions

無論如何,您的具體問題是,wsgi應用程序沒有正確拾取python路徑。改變你的虛擬主機的conf到這樣的事情

<VirtualHost *:80> 
    ServerName myapp.example.com 
    ServerAlias myapp 
    ServerAdmin [email protected] 

    DocumentRoot /usr/local/www/django/myapp 
    WSGIDaemonProcess myapp processes=2 threads=15 display-name=%{GROUP} python-path=/usr/local/www/django/myapp:/path/to/system/python/site-packages 
    WSGIProcessGroup myapp 
    WSGIScriptAlias//usr/local/www/django/myapp/wsgi.py 

    <Directory /usr/local/www/django/myapp> 
     <Files wsgi.py> 
      Order allow,deny 
      Allow from all 
     </Files> 
    </Directory> 
</VirtualHost> 
+0

我決定重新開始,將添加您的建議修改,並看看它是如何工作的。 –

+0

仍然收到相同的錯誤。 –

+0

嘗試Graeme Dumpleton在他對你的問題的評論中提出的建議。他是mod_wsgi的開發者,所以他會比任何人都更瞭解。 – hellsgate

1

你的設置文件是在/usr/local/www/django/myapp/settings.py,但你已經設置的PYTHONPATH到/ usr /本地/網絡/ django/myapp,然後將DJANGO_SETTINGS_MODULE設置爲「myapp.settings」 - 只有當設置位於myapp/myapp/settings中時才適用。將其中一個引用刪除到「myapp」。

+0

試過了,但沒有運氣:( –