Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django + uwsgi + ngnix + debug off = Server Error (500)

Tags:

nginx

django

I'm trying to set up a production server that consists of Django + uwsgi + Nginx. The tutorial I'm following is located here http://www.panta.info/blog/3/how-to-install-and-configure-nginx-uwsgi-and-django-on-ubuntu.html

The production server is working because I can see the admin page when debug is on but when I turn to debug off. It displays the Server Error (500) again. I don't know what to do. Ngnix should be serving the Django request. I'm clueless right now, Can someone kindly help me, please.

my /etc/nginx/sites-available/mysite.com

server {
  listen  80;
  server_name mysite.com www.mysite.com;
  access_log /var/log/nginx/mysite.com_access.log;
  error_log /var/log/nginx/mysite.com_error.log;


  location / {
    uwsgi_pass  unix:///tmp/mysite.com.sock;
    include     uwsgi_params;
  }



  location /media/  {
    alias /home/projects/mysite/media/;
  }



  location  /static/ {
   alias  /home/projects/mysite/static/;
  }
}

my /etc/uwsgi/apps-available/mysite.com.ini

[uwsgi]
vhost = true
plugins = python
socket = /tmp/mysite.com.sock
master = true
enable-threads = true
processes = 2
wsgi-file = /home/projects/mysite/mysite/wsgi.py
virtualenv = /home/projects/venv
chdir = /home/projects/mysite
touch-reload = /home/projects/mysite/reload

my settings.py

root@localhost:~# cat /home/projects/mysite/mysite/settings.py
# Django settings for my site project.

DEBUG = False
TEMPLATE_DEBUG = DEBUG





min/css/base.css" failed (2: No such file or directory), client: 160.19.332.22, server: mysite.com, request: "GET /static/admin/css/base.css HTTP/1.1", host: "160.19.332.22"
2013/06/17 14:33:39 [error] 8346#0: *13 open() "/home/projects/mysite/static/admin/css/login.css" failed (2: No such file or directory), client: 160.19.332.22, server: mysite.com, request: "GET /static/admin/css/login.css HTTP/1.1", host: "174.200.14.200"
2013/06/17 14:33:39 [error] 8346#0: *14 open() "/home/projects/mysite/static/admin/css/base.css" failed (2: No such file or directory), client: 160.19.332.22, server: mysite.com, request: "GET /static/admin/css/base.css HTTP/1.1", host: "174.200.14.2007", referrer: "http://174.200.14.200/admin/"
2013/06/17 14:33:39 [error] 8346#0: *15 open() "/home/projects/mysite/static/admin/css/login.css" failed (2: No such file or directory), client: 160.19.332.22, server: mysite.com, request: "GET /static/admin/css/login.css HTTP/1.1", host: "174.200.14.200", referrer: "http://174.200.14.200/admin/"
like image 944
Uncle Toby Avatar asked Jun 17 '13 14:06

Uncle Toby


2 Answers

I think it's your ALLOWED_HOSTS setting (new in Django 1.5)

Try the following in your settings.py

ALLOWED_HOSTS = ['*']

This will allow everything to connect until you get your domain name sorted.

It's worth saying that when you do get a domain name sorted make sure you update this value (list of allowed domain names). As the documentation for ALLOWED_HOSTS states:

This is a security measure to prevent an attacker from poisoning caches and password reset emails with links to malicious hosts by submitting requests with a fake HTTP Host header, which is possible even under many seemingly-safe webserver configurations.

Also (a little aside) - I don't know if you have a different setup for your django settings per environment but this is what I do:

At the end of your settings.py include:

try:
    from local_settings import *
except ImportError:
    pass

Then in the same directory as settings.py create a local_settings.py file (and a __init__.py file if using a different structure than the initial template) and set your settings per environment there. Also exclude local_settings.py from your version control system.

e.g. I have DEBUG=False in my settings.py (for a secure default) but can override with DEBUG=True in my development local settings.

I also keep all my database info in my local settings file so it's not in version control.

Just a little info if you didn't know it already :-)

like image 134
Ewan Avatar answered Oct 31 '22 18:10

Ewan


I had the same issue but in my case it turned out to be that STATICFILES_STORAGE was incorrectly set as:

STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage'

This question has already an accepted answer but I'm leaving this in case someone gets here in the same situation. You can also see this similar answer for the same error.

like image 1
morpheuz Avatar answered Oct 31 '22 19:10

morpheuz