Django with Apache 500 Error

泪湿孤枕 提交于 2020-01-15 10:30:10

问题


I have set up mode_wsgi on Apache and it works fine but when I try to deploy Django on it i get 500 Internal server error. and the following is the trace I get from Apache error logs

[Thu Nov 28 12:14:40 2013] [error] [client 127.0.0.1] mod_wsgi (pid=8212): Target WSGI          script 'H:/DEV/python/mysite/mysite/wsgi.py' cannot be loaded as Python module.
[Thu Nov 28 12:14:40 2013] [error] [client 127.0.0.1] mod_wsgi (pid=8212): Exception occurred processing WSGI script 'H:/DEV/python/mysite/mysite/wsgi.py'.
[Thu Nov 28 12:14:40 2013] [error] [client 127.0.0.1] Traceback (most recent call last):
[Thu Nov 28 12:14:40 2013] [error] [client 127.0.0.1]   File "H:/DEV/python/mysite/mysite/wsgi.py", line 13, in <module>
[Thu Nov 28 12:14:40 2013] [error] [client 127.0.0.1]     from django.core.wsgi import get_wsgi_application
[Thu Nov 28 12:14:40 2013] [error] [client 127.0.0.1]   File "C:\\Python27\\lib\\site-packages\\django\\core\\wsgi.py", line 1, in <module>
[Thu Nov 28 12:14:40 2013] [error] [client 127.0.0.1]     from django.core.handlers.wsgi import WSGIHandler
[Thu Nov 28 12:14:40 2013] [error] [client 127.0.0.1]   File "C:\\Python27\\lib\\site-packages\\django\\core\\handlers\\wsgi.py", line 11, in <module>
[Thu Nov 28 12:14:40 2013] [error] [client 127.0.0.1]     from django.core.handlers import base
[Thu Nov 28 12:14:40 2013] [error] [client 127.0.0.1]   File "C:\\Python27\\lib\\site-packages\\django\\core\\handlers\\base.py", line 12, in <module>
[Thu Nov 28 12:14:40 2013] [error] [client 127.0.0.1]     from django.db import connections, transaction
[Thu Nov 28 12:14:40 2013] [error] [client 127.0.0.1]   File "C:\\Python27\\lib\\site-packages\\django\\db\\__init__.py", line 83, in <module>
[Thu Nov 28 12:14:40 2013] [error] [client 127.0.0.1]     signals.request_started.connect(reset_queries)
[Thu Nov 28 12:14:40 2013] [error] [client 127.0.0.1]   File "C:\\Python27\\lib\\site-packages\\django\\dispatch\\dispatcher.py", line 88, in connect
[Thu Nov 28 12:14:40 2013] [error] [client 127.0.0.1]     if settings.DEBUG:
[Thu Nov 28 12:14:40 2013] [error] [client 127.0.0.1]   File "C:\\Python27\\lib\\site-packages\\django\\conf\\__init__.py", line 54, in __getattr__
[Thu Nov 28 12:14:40 2013] [error] [client 127.0.0.1]     self._setup(name)
[Thu Nov 28 12:14:40 2013] [error] [client 127.0.0.1]   File "C:\\Python27\\lib\\site-packages\\django\\conf\\__init__.py", line 49, in _setup
[Thu Nov 28 12:14:40 2013] [error] [client 127.0.0.1]     self._wrapped = Settings(settings_module)
[Thu Nov 28 12:14:40 2013] [error] [client 127.0.0.1]   File "C:\\Python27\\lib\\site-packages\\django\\conf\\__init__.py", line 132, in __init__
[Thu Nov 28 12:14:40 2013] [error] [client 127.0.0.1]     % (self.SETTINGS_MODULE, e)
[Thu Nov 28 12:14:40 2013] [error] [client 127.0.0.1] ImportError: Could not import settings 'mysite.settings' (Is it on sys.path? Is there an import error in the settings file?): No module named mysite.settings

Any help on this will be greatly appreciated. Thanks

Here are the apache conf for the site

WSGIScriptAlias / "H:/DEV/python/mysite/mysite/wsgi.py"
WSGIPythonPath "H:/DEV/python/mysite:C:/Python27/Lib/site-packages"

<Directory "H:/DEV/python/mysite">
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>

and this is the code in the wsgi file

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

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

回答1:


Add this to wsgi script:

import sys
sys.path.append('H:/DEV/python/mysite')

Put it before the os.environ part




回答2:


I think I finally find the real problem after a bit search:

WSGIPythonPath directive in apache config is wrong, you need to change the : by ; since you are on windows, so it must looks like:

WSGIPythonPath "H:/DEV/python/mysite;C:/Python27/Lib/site-packages"

That's why your wsgi didn't found your site settings, this is the correct way to handle this. With this fix you don't need to modify the auto-generated wsgi file to add your site in the sys.path (as suggeted @yuvi in his answer) because it is redundant (add the sys.path is what WSGIPythonPath do) and not a best practice.

(More on wsgi docs)

Also you should change this:

<Directory "H:/DEV/python/mysite">

by:

<Directory "H:/DEV/python/mysite/mysite">



回答3:


I just met same problem. edit apache conf file 'http.conf' and Add your site packapge path to WSGIPythonPath may be helpful. Like follows:

    WSGIPythonPath "c:/Users/mysite;c:/Users/yoursite"

to see more details on django docs

In particular, the path separator between directory names on Windows is ; and not : as on UNIX systems.



来源:https://stackoverflow.com/questions/20262164/django-with-apache-500-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!