Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Apache mod_wsgi 500

I have a Django application deployed on CentOS. Here is what my httpd.conf file looks like:

WSGISocketPrefix /var/run/wsgi
<VirtualHost *:80>

    WSGIDaemonProcess safe python-path=/usr/lib/python2.6/site-packages
    WSGIProcessGroup safe
    WSGIScriptAlias / /opt/safe/safe/wsgi.py

    <Directory /opt/safe/safe/>
        Order deny,allow
        Allow from all
     </Directory>
</VirtualHost>

EDIT: This is my TEMPLATE_DIRS

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    "/opt/safe/static/templates",
    "/var/www/html/static/templates",
)

EDIT: This is my Admin/Emailing setup:

ADMINS = (
# ('Your Name', '[email protected]'),
('David', 'david@localhost'),
)

SEND_BROKEN_LINK_EMAILS = True

DEFAULT_FROM_EMAIL = 'david@localhost'
SERVER_EMAIL = DEFAULT_FROM_EMAIL

In my templates directory, I have defined a custom 500.html file. When I set my settings.py to have DEBUG = False, I cannot get anywhere on my site without seeing this custom 500.html page.

What's even more strange is the fact that there are no errors in the log files - so I am unsure of where to look or how to proceed. I know it can see the templates because of my custom 500.html file, but I am not sure what is causing the 500 Internal Server errors.

EDIT: After further configuration, I managed to get some errors output (thanks to @Matt Stevens), here is the log output:

Traceback (most recent call last):
File "/usr/lib/python2.6/site-packages/django/core/handlers/base.py", line 89, in get_response
response = middleware_method(request)
File "/usr/lib/python2.6/site-packages/django/middleware/common.py", line 55, in process_request
host = request.get_host()
File "/usr/lib/python2.6/site-packages/django/http/__init__.py", line 223, in get_host
"Invalid HTTP_HOST header (you may need to set ALLOWED_HOSTS): %s" % host)
SuspiciousOperation: Invalid HTTP_HOST header (you may need to set ALLOWED_HOSTS): [my ip address]
like image 835
lightningmanic Avatar asked Feb 26 '13 19:02

lightningmanic


2 Answers

Turns out I needed to add my IP address to the "ALLOWED_HOSTS" in my settings.py file. Thanks to the error logging, I was finally able to see that.

Actual code:

ALLOWED_HOSTS = ['my.server.ip.address']

After an Apache restart, everything is working properly now!

like image 106
lightningmanic Avatar answered Sep 23 '22 18:09

lightningmanic


You could try logging everything to a file to naildown the cause.

Add a logfile for django on the system :

sudo mkdir /var/log/django
sudo touch /var/log/django/error.log
sudo chown <user>:<user> /var/log/django/error.log
chmod 600 /var/log/django/error.log

Then add this in settings.py :

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        },
        'logfile': {
            'class': 'logging.handlers.WatchedFileHandler',
            'filename': '/var/log/django/error.log'
        },
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
        'django': {
            'handlers': ['logfile'],
            'level': 'ERROR',
            'propagate': False,
        },
    }
}

Et voilà! Just run your django server & less /var/log/django/error.log tells you what's wrong.

like image 34
Matt Deacalion Avatar answered Sep 23 '22 18:09

Matt Deacalion