简体   繁体   English

Django 1.6应用在Apache 2.4和python 3.3上运行mod_wsgi时出现错误500

[英]Error 500 with django 1.6 app running on apache 2.4 and python 3.3 with mod_wsgi

For the past months I've been coding a django-app for my current employer and they now want to deploy it to the server. 在过去的几个月中,我一直在为我当前的雇主编写django-app,现在他们希望将其部署到服务器上。 I know there have been a lot of threads regarding this problem but none of them helped me solving this. 我知道关于这个问题有很多线程,但是没有一个线程帮助我解决了这个问题。

I'm using Python 3.3 with Django 1.6 and mod_wsgi on a Windows 7 PC. 我在Windows 7 PC上将Python 3.3与Django 1.6和mod_wsgi一起使用。 The server is a Apache 2.4 via XAMPP. 该服务器是通过XAMPP的Apache 2.4。

Over the last couple of days I tried to publish my app to the server but it didn't work. 在过去的几天里,我尝试将我的应用发布到服务器上,但没有成功。 I think I made some progress, going from a non-functional server over a "Access denied" error to the current state where I get a server error 500. I feel like getting really close, however I'm totally stuck now and need some help. 我认为我已经取得了一些进展,从无法正常工作的服务器通过“访问被拒绝”错误变为服务器错误500的当前状态。我感觉已经很接近了,但是我现在完全陷入困境,需要一些帮助。救命。

This is part of my httpd.conf: 这是我的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>

My wsgi.py looks like this (Just the basic one that django creates when starting a new project. I tried adding new paths to wsgi.py but it didn't change anything.): 我的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()

Now when I start my server and try to access the page, I get the infamous 500 error. 现在,当我启动服务器并尝试访问该页面时,出现了臭名昭著的500错误。 The apache error log gives me the following errors: 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

I'm really stuck here and it's driving me nuts. 我真的被困在这里,这让我发疯。 Any help will be greatly apprecciated! 任何帮助将不胜感激! Thanks in advance! 提前致谢!

EDIT: 编辑:

Errorlog when omitting PythonHome: 省略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

The problem is you don't have the site-packages on your PATH. 问题是您的PATH上没有站点包。 Let's look at the line WSGIPythonHome. 让我们看一下WSGIPythonHome行。 According to the mod_wsgi documentation , this configuration directive is used for (emphasis mine): 根据mod_wsgi文档 ,此配置指令用于(强调我的):

Used to indicate to Python when it is initialised where its library files are installed . 用于在初始化时向Python指示其 文件的安装位置 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可执行文件不在Apache所运行用户的PATH中,或者系统在文件系统中的不同位置(尤其是同一主/次版本的不同安装)中安装了多个Python版本的系统中定义此文件,并且Apache在其PATH中找到的安装不是所需的

In other words, it's good if your server has multiple python installations and you're afraid it will get them mixed up. 换句话说,如果您的服务器上安装了多个python,那会很好,而且您担心它会使它们混淆。 But as you mentioned in part of your comments, you're not using a virtualenv. 但是,正如您在部分评论中提到的那样,您没有使用virtualenv。 So there's no need to use that directive. 因此,无需使用该指令。 With it, what you're actually doing is misdirecting apache to look for python libraries in the wrong location, meaning it doesn't find the module django. 有了它,您实际上正在做的是误导apache在错误的位置查找python库,这意味着它找不到django模块。 Removing it solves that problem 删除它可以解决该问题

As for the second problem that occured, it will probably take some digging, because it seems to be related to your own models, and is probably unrelated to apache. 至于发生的第二个问题,可能需要花一些时间来挖掘,因为它似乎与您自己的模型有关,并且可能与apache无关。 Running the devserver helps find those out and dealing with them easier. 运行devserver有助于发现问题并更轻松地进行处理。 Good luck! 祝好运!

Can you try replacing \\r at the end of your lines in wsgi.py with \\n? 您可以尝试将wsgi.py中行尾的\\ r替换为\\ n吗? There is no problem with your apache and mod_wsgi setup. 您的apache和mod_wsgi设置没有问题。 You can enable viewing of all the whitespace characters if you are using notepad++. 如果您使用的是notepad ++,则可以启用所有空白字符的查看。

Since the error message says that it cannot find django , I suspect that this is a virtualenv problem. 由于错误消息说它找不到django ,因此我怀疑这是一个virtualenv问题。 Have you installed django inside a virtual environment? 您是否在虚拟环境中安装了django?

If so, please ensure that the site-packages are in your path as well as mentioned in the Django docs . 如果是这样,请确保站点包和Django docs中提到的一样在您的路径

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM