【问题标题】:Django site runs with dev server but not on Apache mod_wsgiDjango 站点在开发服务器上运行,但不在 Apache mod_wsgi 上
【发布时间】:2011-05-31 08:08:05
【问题描述】:

我有一些 Django 网站,我想用 mod_wsgi 将它们放在网络服务器 Apache2 上。

网络服务器正常工作,一个 Django 网站正常工作,但另一个没有(它显示内部服务器错误页面)。但奇怪的是网站在开发服务器上运行完美。

这是我的虚拟主机配置:

<VirtualHost *:8080>
    DocumentRoot "C:\wamp\www\mySite"
    ServerName mysite.dev.businessx.local

    ErrorLog C:\wamp\www\mySite\log\error_log
    CustomLog C:\wamp\www\mySite\log\access_log common

    <Directory "C:\wamp\www\mySite">
        Options Indexes FollowSymLinks Includes
        AllowOverride All
        Order allow,deny
        Allow from All
    </Directory>

    WSGIScriptAlias / "c:/wamp/www/mySite/django2.wsgi"
</VirtualHost>

这是 django2.wsgi:

import os, sys  

sys.path.append('c:/wamp/www/')    
os.environ['DJANGO_SETTINGS_MODULE'] = 'mySite.settings'  

import django.core.handlers.wsgi  

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

当我尝试访问该网站时,下面是最终出现在 apache 错误日志中的错误。它声明它无法导入模块 mySite.main 并且 main 是项目的子文件夹/应用程序。

[Tue May 31 09:57:49 2011] [error] [client 192.168.0.40] TemplateSyntaxError: Caught ImportError while rendering: No module named ClickF1.main
[Tue May 31 09:57:49 2011] [error] [client 192.168.0.40] mod_wsgi (pid=4976): Exception occurred processing WSGI script 'C:/wamp/www/clickf1/django2.wsgi'.
[Tue May 31 09:57:49 2011] [error] [client 192.168.0.40] Traceback (most recent call last):
[Tue May 31 09:57:49 2011] [error] [client 192.168.0.40]   File "C:\\Python27\\lib\\site-packages\\django\\core\\handlers\\wsgi.py", line 248, in __call__
[Tue May 31 09:57:49 2011] [error] [client 192.168.0.40]     response = self.get_response(request)
[Tue May 31 09:57:49 2011] [error] [client 192.168.0.40]   File "C:\\Python27\\lib\\site-packages\\django\\core\\handlers\\base.py", line 141, in get_response
[Tue May 31 09:57:49 2011] [error] [client 192.168.0.40]     return self.handle_uncaught_exception(request, resolver, sys.exc_info())
[Tue May 31 09:57:49 2011] [error] [client 192.168.0.40]   File "C:\\Python27\\lib\\site-packages\\django\\core\\handlers\\base.py", line 165, in handle_uncaught_exception
[Tue May 31 09:57:49 2011] [error] [client 192.168.0.40]     return debug.technical_500_response(request, *exc_info)
[Tue May 31 09:57:49 2011] [error] [client 192.168.0.40]   File "C:\\Python27\\lib\\site-packages\\django\\views\\debug.py", line 59, in technical_500_response
[Tue May 31 09:57:49 2011] [error] [client 192.168.0.40]     html = reporter.get_traceback_html()
[Tue May 31 09:57:49 2011] [error] [client 192.168.0.40]   File "C:\\Python27\\lib\\site-packages\\django\\views\\debug.py", line 141, in get_traceback_html
[Tue May 31 09:57:49 2011] [error] [client 192.168.0.40]     return t.render(c)
[Tue May 31 09:57:49 2011] [error] [client 192.168.0.40]   File "C:\\Python27\\lib\\site-packages\\django\\template\\__init__.py", line 173, in render
[Tue May 31 09:57:49 2011] [error] [client 192.168.0.40]     return self._render(context)
[Tue May 31 09:57:49 2011] [error] [client 192.168.0.40]   File "C:\\Python27\\lib\\site-packages\\django\\template\\__init__.py", line 167, in _render
[Tue May 31 09:57:49 2011] [error] [client 192.168.0.40]     return self.nodelist.render(context)
[Tue May 31 09:57:49 2011] [error] [client 192.168.0.40]   File "C:\\Python27\\lib\\site-packages\\django\\template\\__init__.py", line 796, in render
[Tue May 31 09:57:49 2011] [error] [client 192.168.0.40]     bits.append(self.render_node(node, context))
[Tue May 31 09:57:49 2011] [error] [client 192.168.0.40]   File "C:\\Python27\\lib\\site-packages\\django\\template\\debug.py", line 72, in render_node
[Tue May 31 09:57:49 2011] [error] [client 192.168.0.40]     result = node.render(context)
[Tue May 31 09:57:49 2011] [error] [client 192.168.0.40]   File "C:\\Python27\\lib\\site-packages\\django\\template\\debug.py", line 89, in render
[Tue May 31 09:57:49 2011] [error] [client 192.168.0.40]     output = self.filter_expression.resolve(context)
[Tue May 31 09:57:49 2011] [error] [client 192.168.0.40]   File "C:\\Python27\\lib\\site-packages\\django\\template\\__init__.py", line 579, in resolve
[Tue May 31 09:57:49 2011] [error] [client 192.168.0.40]     new_obj = func(obj, *arg_vals)
[Tue May 31 09:57:49 2011] [error] [client 192.168.0.40]   File "C:\\Python27\\lib\\site-packages\\django\\template\\defaultfilters.py", line 682, in date
[Tue May 31 09:57:49 2011] [error] [client 192.168.0.40]     return format(value, arg)
[Tue May 31 09:57:49 2011] [error] [client 192.168.0.40]   File "C:\\Python27\\lib\\site-packages\\django\\utils\\dateformat.py", line 281, in format
[Tue May 31 09:57:49 2011] [error] [client 192.168.0.40]     return df.format(format_string)
[Tue May 31 09:57:49 2011] [error] [client 192.168.0.40]   File "C:\\Python27\\lib\\site-packages\\django\\utils\\dateformat.py", line 30, in format
[Tue May 31 09:57:49 2011] [error] [client 192.168.0.40]     pieces.append(force_unicode(getattr(self, piece)()))
[Tue May 31 09:57:49 2011] [error] [client 192.168.0.40]   File "C:\\Python27\\lib\\site-packages\\django\\utils\\dateformat.py", line 187, in r
[Tue May 31 09:57:49 2011] [error] [client 192.168.0.40]     return self.format('D, j M Y H:i:s O')
[Tue May 31 09:57:49 2011] [error] [client 192.168.0.40]   File "C:\\Python27\\lib\\site-packages\\django\\utils\\dateformat.py", line 30, in format
[Tue May 31 09:57:49 2011] [error] [client 192.168.0.40]     pieces.append(force_unicode(getattr(self, piece)()))
[Tue May 31 09:57:49 2011] [error] [client 192.168.0.40]   File "C:\\Python27\\lib\\site-packages\\django\\utils\\encoding.py", line 66, in force_unicode
[Tue May 31 09:57:49 2011] [error] [client 192.168.0.40]     s = unicode(s)
[Tue May 31 09:57:49 2011] [error] [client 192.168.0.40]   File "C:\\Python27\\lib\\site-packages\\django\\utils\\functional.py", line 206, in __unicode_cast
[Tue May 31 09:57:49 2011] [error] [client 192.168.0.40]     return self.__func(*self.__args, **self.__kw)
[Tue May 31 09:57:49 2011] [error] [client 192.168.0.40]   File "C:\\Python27\\lib\\site-packages\\django\\utils\\translation\\__init__.py", line 55, in ugettext
[Tue May 31 09:57:49 2011] [error] [client 192.168.0.40]     return real_ugettext(message)
[Tue May 31 09:57:49 2011] [error] [client 192.168.0.40]   File "C:\\Python27\\lib\\site-packages\\django\\utils\\functional.py", line 55, in _curried
[Tue May 31 09:57:49 2011] [error] [client 192.168.0.40]     return _curried_func(*(args+moreargs), **dict(kwargs, **morekwargs))
[Tue May 31 09:57:49 2011] [error] [client 192.168.0.40]   File "C:\\Python27\\lib\\site-packages\\django\\utils\\translation\\__init__.py", line 36, in delayed_loader
[Tue May 31 09:57:49 2011] [error] [client 192.168.0.40]     return getattr(trans, real_name)(*args, **kwargs)
[Tue May 31 09:57:49 2011] [error] [client 192.168.0.40]   File "C:\\Python27\\lib\\site-packages\\django\\utils\\translation\\trans_real.py", line 276, in ugettext
[Tue May 31 09:57:49 2011] [error] [client 192.168.0.40]     return do_translate(message, 'ugettext')
[Tue May 31 09:57:49 2011] [error] [client 192.168.0.40]   File "C:\\Python27\\lib\\site-packages\\django\\utils\\translation\\trans_real.py", line 266, in do_translate
[Tue May 31 09:57:49 2011] [error] [client 192.168.0.40]     _default = translation(settings.LANGUAGE_CODE)
[Tue May 31 09:57:49 2011] [error] [client 192.168.0.40]   File "C:\\Python27\\lib\\site-packages\\django\\utils\\translation\\trans_real.py", line 176, in translation
[Tue May 31 09:57:49 2011] [error] [client 192.168.0.40]     default_translation = _fetch(settings.LANGUAGE_CODE)
[Tue May 31 09:57:49 2011] [error] [client 192.168.0.40]   File "C:\\Python27\\lib\\site-packages\\django\\utils\\translation\\trans_real.py", line 159, in _fetch
[Tue May 31 09:57:49 2011] [error] [client 192.168.0.40]     app = import_module(appname)
[Tue May 31 09:57:49 2011] [error] [client 192.168.0.40]   File "C:\\Python27\\lib\\site-packages\\django\\utils\\importlib.py", line 35, in import_module
[Tue May 31 09:57:49 2011] [error] [client 192.168.0.40]     __import__(name)
[Tue May 31 09:57:49 2011] [error] [client 192.168.0.40] TemplateSyntaxError: Caught ImportError while rendering: No module named mySite.main 

有人能看出问题吗?

【问题讨论】:

  • 你的项目文件夹在python路径上吗?
  • 是的,如果你指的是 c:/wamp/www/ 路径。

标签: django apache2 mod-wsgi


【解决方案1】:

它没有找到你的包裹(这是显而易见的部分)。

一直对我有用的是使用站点模块

import site

ALLDIRS = ['c:/wamp/www/']

for directory in ALLDIRS:
    site.addsitedir(directory)

我还建议你使用这个效果很好的activate_workingenv

希望对你有帮助

【讨论】:

  • 我必须把这个放在哪里?在 wsgi 文件中?
  • 是的,在 .wsgi python 文件中
  • 我将它添加到 wsgi 文件中,但它仍然给我同样的错误。我是否必须将ALLDIRS 更改为项目文件夹(C:/wamp/www/mySite/)?
  • 您也可以将该目录添加到 ALLDIRS 列表中
【解决方案2】:

这是一件非常非常简单的事情。项目文件夹名称区分大小写。我已将文件夹名称更改为原始开发名称,该项目就像一个魅力。

感谢您的帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-02
    • 1970-01-01
    • 2014-03-14
    • 2015-08-26
    • 1970-01-01
    • 2010-11-15
    • 1970-01-01
    • 2010-09-23
    相关资源
    最近更新 更多