Skip to main content
 首页 » 编程设计

python之使用 mod_wsgi 在 apache 2.4 和 python 3.3 上运行的 django 1.6 应用程序出现错误 500

2024年08月28日33arxive

在过去的几个月里,我一直在为我现在的雇主编写一个 django-app,他们现在想将它部署到服务器上。我知道有很多关于这个问题的线索,但没有一个能帮助我解决这个问题。

我在 Windows 7 PC 上使用 Python 3.3 和 Django 1.6 以及 mod_wsgi。服务器是通过 XAMPP 的 Apache 2.4。

在过去的几天里,我尝试将我的应用程序发布到服务器,但没有成功。我想我取得了一些进展,从一个非功能性服务器通过“访问被拒绝”错误到我收到服务器错误 500 的当前状态。我觉得真的很接近,但是我现在完全卡住了,需要一些帮助。

这是我的 httpd.conf 的一部分:

WSGIPythonPath "C:/TSDE_Workarea/Kmg1yh/SharCC/Workspaces/ReservationTool/PythonReDesign/trunk" 
WSGIPythonHome "C:/TSDE_Workarea/Kmg1yh/SharCC/Workspaces/ReservationTool/PythonReDesign/trunk" 
WSGIScriptAlias / "C:/TSDE_Workarea/Kmg1yh/SharCC/Workspaces/ReservationTool/PythonReDesign/trunk/PythonReDesign/wsgi.py" 
 
<Directory "C:/TSDE_Workarea/Kmg1yh/SharCC/Workspaces/ReservationTool/PythonReDesign/trunk/PythonReDesign"> 
    <Files wsgi.py> 
        #Options Indexes FollowSymLinks Includes ExecCGI 
        Order allow,deny 
        Allow from all 
        #Require all granted 
    </Files> 
    DirectoryIndex wsgi.py 
</Directory> 

我的 wsgi.py 看起来像这样(只是 django 在开始新项目时创建的基本文件。我尝试向 wsgi.py 添加新路径,但它没有改变任何东西。):

""" 
WSGI config for PythonReDesign 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, sys 
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "PythonReDesign.settings") 
 
from django.core.wsgi import get_wsgi_application 
application = get_wsgi_application() 

现在,当我启动我的服务器并尝试访问该页面时,我遇到了臭名昭著的 500 错误。 apache 错误日志给我以下错误:

[Fri Jan 10 15:08:40.017397 2014] [:error] [pid 6676:tid 1876] [client 127.0.0.1:61162] mod_wsgi (pid=6676): Target WSGI script 'C:/TSDE_Workarea/Kmg1yh/SharCC/Workspaces/ReservationTool/PythonReDesign/trunk/PythonReDesign/wsgi.py' cannot be loaded as Python module. 
[Fri Jan 10 15:08:40.017397 2014] [:error] [pid 6676:tid 1876] [client 127.0.0.1:61162] mod_wsgi (pid=6676): Exception occurred processing WSGI script 'C:/TSDE_Workarea/Kmg1yh/SharCC/Workspaces/ReservationTool/PythonReDesign/trunk/PythonReDesign/wsgi.py'. 
[Fri Jan 10 15:08:40.017397 2014] [:error] [pid 6676:tid 1876] [client 127.0.0.1:61162] Traceback (most recent call last):\r 
[Fri Jan 10 15:08:40.017397 2014] [:error] [pid 6676:tid 1876] [client 127.0.0.1:61162]   File "C:/TSDE_Workarea/Kmg1yh/SharCC/Workspaces/ReservationTool/PythonReDesign/trunk/PythonReDesign/wsgi.py", line 13, in <module>\r 
[Fri Jan 10 15:08:40.017397 2014] [:error] [pid 6676:tid 1876] [client 127.0.0.1:61162]     from django.core.wsgi import get_wsgi_application\r 
[Fri Jan 10 15:08:40.017397 2014] [:error] [pid 6676:tid 1876] [client 127.0.0.1:61162] ImportError: No module named 'django'\r 

我真的被困在这里,快把我逼疯了。任何帮助将不胜感激!提前致谢!

编辑:

省略 PythonHome 时的错误日志:

[Fri Jan 10 16:23:06.296711 2014] [:error] [pid 5288:tid 1868] [client 127.0.0.1:61683] mod_wsgi (pid=5288): Exception occurred processing WSGI script 'C:/TSDE_Workarea/Kmg1yh/SharCC/Workspaces/ReservationTool/PythonReDesign/trunk/PythonReDesign/wsgi.py'. 
[Fri Jan 10 16:23:06.296711 2014] [:error] [pid 5288:tid 1868] [client 127.0.0.1:61683] Traceback (most recent call last):\r 
[Fri Jan 10 16:23:06.296711 2014] [:error] [pid 5288:tid 1868] [client 127.0.0.1:61683]   File "C:\\Python33\\lib\\site-packages\\django\\core\\urlresolvers.py", line 339, in urlconf_module\r 
[Fri Jan 10 16:23:06.296711 2014] [:error] [pid 5288:tid 1868] [client 127.0.0.1:61683]     return self._urlconf_module\r 
[Fri Jan 10 16:23:06.296711 2014] [:error] [pid 5288:tid 1868] [client 127.0.0.1:61683] AttributeError: 'RegexURLResolver' object has no attribute '_urlconf_module'\r 
[Fri Jan 10 16:23:06.296711 2014] [:error] [pid 5288:tid 1868] [client 127.0.0.1:61683] \r 
[Fri Jan 10 16:23:06.296711 2014] [:error] [pid 5288:tid 1868] [client 127.0.0.1:61683] During handling of the above exception, another exception occurred:\r 
[Fri Jan 10 16:23:06.296711 2014] [:error] [pid 5288:tid 1868] [client 127.0.0.1:61683] \r 
[Fri Jan 10 16:23:06.296711 2014] [:error] [pid 5288:tid 1868] [client 127.0.0.1:61683] Traceback (most recent call last):\r 
[Fri Jan 10 16:23:06.296711 2014] [:error] [pid 5288:tid 1868] [client 127.0.0.1:61683]   File "C:\\Python33\\lib\\site-packages\\django\\core\\handlers\\base.py", line 101, in get_response\r 
[Fri Jan 10 16:23:06.296711 2014] [:error] [pid 5288:tid 1868] [client 127.0.0.1:61683]     resolver_match = resolver.resolve(request.path_info)\r 
[Fri Jan 10 16:23:06.296711 2014] [:error] [pid 5288:tid 1868] [client 127.0.0.1:61683]   File "C:\\Python33\\lib\\site-packages\\django\\core\\urlresolvers.py", line 318, in resolve\r 
[Fri Jan 10 16:23:06.296711 2014] [:error] [pid 5288:tid 1868] [client 127.0.0.1:61683]     for pattern in self.url_patterns:\r 
[Fri Jan 10 16:23:06.296711 2014] [:error] [pid 5288:tid 1868] [client 127.0.0.1:61683]   File "C:\\Python33\\lib\\site-packages\\django\\core\\urlresolvers.py", line 346, in url_patterns\r 
[Fri Jan 10 16:23:06.296711 2014] [:error] [pid 5288:tid 1868] [client 127.0.0.1:61683]     patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)\r 
[Fri Jan 10 16:23:06.296711 2014] [:error] [pid 5288:tid 1868] [client 127.0.0.1:61683]   File "C:\\Python33\\lib\\site-packages\\django\\core\\urlresolvers.py", line 341, in urlconf_module\r 
[Fri Jan 10 16:23:06.296711 2014] [:error] [pid 5288:tid 1868] [client 127.0.0.1:61683]     self._urlconf_module = import_module(self.urlconf_name)\r 
[Fri Jan 10 16:23:06.296711 2014] [:error] [pid 5288:tid 1868] [client 127.0.0.1:61683]   File "C:\\Python33\\Lib\\importlib\\__init__.py", line 90, in import_module\r 
[Fri Jan 10 16:23:06.296711 2014] [:error] [pid 5288:tid 1868] [client 127.0.0.1:61683]     return _bootstrap._gcd_import(name[level:], package, level)\r 
[Fri Jan 10 16:23:06.296711 2014] [:error] [pid 5288:tid 1868] [client 127.0.0.1:61683]   File "<frozen importlib._bootstrap>", line 1584, in _gcd_import\r 
[Fri Jan 10 16:23:06.296711 2014] [:error] [pid 5288:tid 1868] [client 127.0.0.1:61683]   File "<frozen importlib._bootstrap>", line 1565, in _find_and_load\r 
[Fri Jan 10 16:23:06.296711 2014] [:error] [pid 5288:tid 1868] [client 127.0.0.1:61683]   File "<frozen importlib._bootstrap>", line 1532, in _find_and_load_unlocked\r 
[Fri Jan 10 16:23:06.296711 2014] [:error] [pid 5288:tid 1868] [client 127.0.0.1:61683]   File "<frozen importlib._bootstrap>", line 584, in _check_name_wrapper\r 
[Fri Jan 10 16:23:06.296711 2014] [:error] [pid 5288:tid 1868] [client 127.0.0.1:61683]   File "<frozen importlib._bootstrap>", line 1022, in load_module\r 
[Fri Jan 10 16:23:06.296711 2014] [:error] [pid 5288:tid 1868] [client 127.0.0.1:61683]   File "<frozen importlib._bootstrap>", line 1003, in load_module\r 
[Fri Jan 10 16:23:06.296711 2014] [:error] [pid 5288:tid 1868] [client 127.0.0.1:61683]   File "<frozen importlib._bootstrap>", line 560, in module_for_loader_wrapper\r 
[Fri Jan 10 16:23:06.296711 2014] [:error] [pid 5288:tid 1868] [client 127.0.0.1:61683]   File "<frozen importlib._bootstrap>", line 868, in _load_module\r 
[Fri Jan 10 16:23:06.296711 2014] [:error] [pid 5288:tid 1868] [client 127.0.0.1:61683]   File "<frozen importlib._bootstrap>", line 313, in _call_with_frames_removed\r 
[Fri Jan 10 16:23:06.296711 2014] [:error] [pid 5288:tid 1868] [client 127.0.0.1:61683]   File "C:\\TSDE_Workarea\\Kmg1yh\\SharCC\\Workspaces\\ReservationTool\\PythonReDesign\\trunk\\PythonReDesign\\urls.py", line 6, in <module>\r 
[Fri Jan 10 16:23:06.296711 2014] [:error] [pid 5288:tid 1868] [client 127.0.0.1:61683]     from PythonReDesign.views import *\r 
[Fri Jan 10 16:23:06.296711 2014] [:error] [pid 5288:tid 1868] [client 127.0.0.1:61683]   File "C:\\TSDE_Workarea\\Kmg1yh\\SharCC\\Workspaces\\ReservationTool\\PythonReDesign\\trunk\\PythonReDesign\\views.py", line 9, in <module>\r 
[Fri Jan 10 16:23:06.296711 2014] [:error] [pid 5288:tid 1868] [client 127.0.0.1:61683]     import PythonReDesign.commons as com\r 
[Fri Jan 10 16:23:06.296711 2014] [:error] [pid 5288:tid 1868] [client 127.0.0.1:61683] ImportError: No module named 'PythonReDesign.commons'\r 
[Fri Jan 10 16:23:06.296711 2014] [:error] [pid 5288:tid 1868] [client 127.0.0.1:61683] \r 
[Fri Jan 10 16:23:06.296711 2014] [:error] [pid 5288:tid 1868] [client 127.0.0.1:61683] During handling of the above exception, another exception occurred:\r 
[Fri Jan 10 16:23:06.296711 2014] [:error] [pid 5288:tid 1868] [client 127.0.0.1:61683] \r 
[Fri Jan 10 16:23:06.296711 2014] [:error] [pid 5288:tid 1868] [client 127.0.0.1:61683] Traceback (most recent call last):\r 
[Fri Jan 10 16:23:06.296711 2014] [:error] [pid 5288:tid 1868] [client 127.0.0.1:61683]   File "C:\\Python33\\lib\\site-packages\\django\\core\\handlers\\wsgi.py", line 206, in __call__\r 
[Fri Jan 10 16:23:06.296711 2014] [:error] [pid 5288:tid 1868] [client 127.0.0.1:61683]     response = self.get_response(request)\r 
[Fri Jan 10 16:23:06.296711 2014] [:error] [pid 5288:tid 1868] [client 127.0.0.1:61683]   File "C:\\Python33\\lib\\site-packages\\django\\core\\handlers\\base.py", line 196, in get_response\r 
[Fri Jan 10 16:23:06.296711 2014] [:error] [pid 5288:tid 1868] [client 127.0.0.1:61683]     response = self.handle_uncaught_exception(request, resolver, sys.exc_info())\r 
[Fri Jan 10 16:23:06.296711 2014] [:error] [pid 5288:tid 1868] [client 127.0.0.1:61683]   File "C:\\Python33\\lib\\site-packages\\django\\core\\handlers\\base.py", line 231, in handle_uncaught_exception\r 
[Fri Jan 10 16:23:06.296711 2014] [:error] [pid 5288:tid 1868] [client 127.0.0.1:61683]     return debug.technical_500_response(request, *exc_info)\r 
[Fri Jan 10 16:23:06.296711 2014] [:error] [pid 5288:tid 1868] [client 127.0.0.1:61683]   File "C:\\Python33\\lib\\site-packages\\django\\views\\debug.py", line 69, in technical_500_response\r 
[Fri Jan 10 16:23:06.296711 2014] [:error] [pid 5288:tid 1868] [client 127.0.0.1:61683]     html = reporter.get_traceback_html()\r 
[Fri Jan 10 16:23:06.296711 2014] [:error] [pid 5288:tid 1868] [client 127.0.0.1:61683]   File "C:\\Python33\\lib\\site-packages\\django\\views\\debug.py", line 323, in get_traceback_html\r 
[Fri Jan 10 16:23:06.296711 2014] [:error] [pid 5288:tid 1868] [client 127.0.0.1:61683]     c = Context(self.get_traceback_data())\r 
[Fri Jan 10 16:23:06.296711 2014] [:error] [pid 5288:tid 1868] [client 127.0.0.1:61683]   File "C:\\Python33\\lib\\site-packages\\django\\views\\debug.py", line 281, in get_traceback_data\r 
[Fri Jan 10 16:23:06.296711 2014] [:error] [pid 5288:tid 1868] [client 127.0.0.1:61683]     frames = self.get_traceback_frames()\r 
[Fri Jan 10 16:23:06.296711 2014] [:error] [pid 5288:tid 1868] [client 127.0.0.1:61683]   File "C:\\Python33\\lib\\site-packages\\django\\views\\debug.py", line 428, in get_traceback_frames\r 
[Fri Jan 10 16:23:06.296711 2014] [:error] [pid 5288:tid 1868] [client 127.0.0.1:61683]     pre_context_lineno, pre_context, context_line, post_context = self._get_lines_from_file(filename, lineno, 7, loader, module_name)\r 
[Fri Jan 10 16:23:06.296711 2014] [:error] [pid 5288:tid 1868] [client 127.0.0.1:61683]   File "C:\\Python33\\lib\\site-packages\\django\\views\\debug.py", line 379, in _get_lines_from_file\r 
[Fri Jan 10 16:23:06.296711 2014] [:error] [pid 5288:tid 1868] [client 127.0.0.1:61683]     source = loader.get_source(module_name)\r 
[Fri Jan 10 16:23:06.296711 2014] [:error] [pid 5288:tid 1868] [client 127.0.0.1:61683]   File "<frozen importlib._bootstrap>", line 605, in _requires_frozen_wrapper\r 
[Fri Jan 10 16:23:06.296711 2014] [:error] [pid 5288:tid 1868] [client 127.0.0.1:61683] ImportError: importlib._bootstrap is not a frozen module\r 

请您参考如下方法:

问题是您的 PATH 中没有站点包。让我们看一下 WSGIPythonHome 行。 According to the mod_wsgi documentation ,此配置指令用于(强调我的):

Used to indicate to Python when it is initialised where its library files are installed. This should be defined where the Python executable is not in the PATH of the user that Apache runs as, or where a system has multiple versions of Python installed in different locations in the file system, especially different installations of the same major/minor version, and the installation that Apache finds in its PATH is not the desired on

换句话说,如果你的服务器安装了多个 python 是很好的,你担心它会把它们弄混。但是正如您在部分评论中提到的,您没有使用 virtualenv。所以没有必要使用那个指令。有了它,你实际上在做的是误导 apache 在错误的位置寻找 python 库,这意味着它没有找到模块 django。删除它可以解决该问题

至于出现的第二个问题,可能需要挖掘一下,因为它似乎与您自己的模型有关,并且可能与apache无关。运行 devserver 有助于找出这些问题并更轻松地处理它们。祝你好运!