11

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', 'your_email@example.com'),
('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]
pnuts
  • 58,317
  • 11
  • 87
  • 139
lightningmanic
  • 2,025
  • 5
  • 20
  • 41

2 Answers2

12

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!

lightningmanic
  • 2,025
  • 5
  • 20
  • 41
  • 2
    Thanks for coming back to post the solution! Never knew about `ALLOWED_HOSTS`, looks like it's a new security feature: http://git.io/9Ytk6A – Matt Feb 27 '13 at 18:26
  • Well, it's not your IP address you should add there, but a valid HTTP-Host. When accessing your server by using its IP address, this is the same, but you probably want to set this to a FQDN in production environments. See the [Django documentation about it](https://docs.djangoproject.com/en/1.5/ref/settings/#allowed-hosts). – gertvdijk May 02 '13 at 08:33
  • This answer gives one of the possible error [Matt's answer](https://stackoverflow.com/a/15100463/6709630) gives the logging solution that helps you to find all errors possible! – freezed Oct 24 '18 at 16:01
9

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.

freezed
  • 1,269
  • 1
  • 17
  • 34
Matt
  • 8,758
  • 4
  • 35
  • 64
  • 3
    Not sure why it wasn't writing anything at first - but after a few other changes and some permission modifications, I was able to get the log to spit stuff out. Thanks! – lightningmanic Feb 27 '13 at 14:14
  • That is the speedest way to find: `sudo mkdir /var/log/django; sudo touch /var/log/django/error.log; sudo chown : /var/log/django/error.log; chmod 600 /var/log/django/error.log` then adding your config and voilà ! – freezed Oct 24 '18 at 15:52