7

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/"
ash
  • 1,065
  • 1
  • 5
  • 20
Uncle Toby
  • 377
  • 2
  • 6
  • 15
  • 1
    You need to find out what the actual python error message is in your logs that is causing the 500 – Timmy O'Mahony Jun 17 '13 at 14:49
  • I posted it timmy , its weird . Everything works fine when debug is on and when it is off , everything go bizzare – Uncle Toby Jun 17 '13 at 14:57
  • Are you using `django-compressor` or something similar to create minified static files? – Timmy O'Mahony Jun 17 '13 at 14:58
  • 2
    Make sure you have you set [`ALLOWED_HOSTS`](https://docs.djangoproject.com/en/1.5/ref/settings/#allowed-hosts) in your settings. – Alasdair Jun 17 '13 at 15:00
  • @Alasdair I don't have a domain set up but i'm setting my production server on vps and when I used the vps ip for the allowed host it doesn't work – Uncle Toby Jun 17 '13 at 15:06
  • @TimmyO'Mahony nope , i'm not using django-compressor . I think its ALLOWED_HOST . I'm running vps on production and I don't have a domain yet so I input the vps ip into the allowed_host field. and it still doesn't work – Uncle Toby Jun 17 '13 at 15:09

2 Answers2

18

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 :-)

Ewan
  • 14,592
  • 6
  • 48
  • 62
1

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.

morpheuz
  • 91
  • 1
  • 10