Question

Been having ongoing issues with an Apache / Mod_wsgi stack and finally decided to cut my losses and start fresh on the server side of things

Have gotten Nginx setup to proxy requests to uwsgi. With the uwsgi protocol was fairly easy.

However uwsgi has been plain stubborn, tried reading docs / tutorials / asking in IRC... Keep getting the same error of wsgi module for Django not finding settings, yet when I run the same code through a console I don't get any errors.

Pasted all relevant scripts below - assuming that I'm missing something obvious though I have no idea what.

Running on Ubuntu 11.10 w/ Upstart script to boot

Upstart Script

description "uWSGI starter"
start on (local-filesystems
and runlevel [2345])
stop on runlevel [016]
respawn
exec /usr/local/sbin/uwsgi \
--uid www-data \
--socket 127.0.0.1:5050 \
--master \
--logto /var/log/uwsgi_main.log \
--logdate \
--optimize 2 \
--processes 4 \
--harakiri 120 \
--vhost \
--no-site

nginx server config

server {
    listen   80;
    server_name .DOMAIN.com;
    access_log /home/USER/virtualenv/logs/nginx/access.log;
    error_log /home/USER/virtualenv/logs/nginx/error.log;
    client_max_body_size 10m;
    keepalive_timeout 120;

    location /media/ {
        root /home/USER/virtualenv/PROJECT;
    }
    location /static/ {
        root /home/USER/virtualenv/PROJECT;
    }

    location / {
        uwsgi_pass uwsgi_main;
        include uwsgi_params;
        uwsgi_param UWSGI_PYHOME /home/USER/virtualenv;
        uwsgi_param UWSGI_SCRIPT deploy.deploy;
        uwsgi_param UWSGI_CHDIR /home/USER/virtualenv/PROJECT;
        root /home/USER/virtualenv;
    }
}

deploy.py

import os, site, sys

prev_sys_path = list(sys.path)
site.addsitedir('/home/USER/virtualenv/lib/python2.7/site-packages')

sys.path.append('/home/USER/virtualenv/PROJECT')
sys.path.append('/home/USER/virtualenv/')

new_sys_path = [p for p in sys.path if p not in prev_sys_path]
for item in new_sys_path:
    sys.path.remove(item)
sys.path[:0] = new_sys_path

os.environ['DJANGO_SETTINGS_MODULE'] = 'PROJECT.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

Error

Fri Nov 25 22:24:25 2011 - WSGI application 0 (mountpoint=DOMAIN.com|) ready on interpreter 0x679920 pid: 9552
Traceback (most recent call last):
  File "/home/USER/virtualenv/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 250, in __call__
    self.load_middleware()
  File "/home/USER/virtualenv/lib/python2.7/site-packages/django/core/handlers/base.py", line 39, in load_middleware
    for middleware_path in settings.MIDDLEWARE_CLASSES:
  File "/home/USER/virtualenv/lib/python2.7/site-packages/django/utils/functional.py", line 276, in __getattr__
    self._setup()
  File "/home/USER/virtualenv/lib/python2.7/site-packages/django/conf/__init__.py", line 42, in _setup
    self._wrapped = Settings(settings_module)
  File "/home/USER/virtualenv/lib/python2.7/site-packages/django/conf/__init__.py", line 89, in __init__
    raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'settings' (Is it on sys.path?): No module named settings
DOMAIN.com [pid: 9552|app: 0|req: 1/1] 184.96.159.248 () {48 vars in 1083 bytes} [Fri Nov 25 22:24:25 2011] GET / => generated 0 bytes in 284 ms$
Fri Nov 25 22:24:26 2011 - WSGI application 0 (mountpoint=DOMAIN.com|) ready on interpreter 0x679920 pid: 9553
Traceback (most recent call last):
  File "/home/USER/virtualenv/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 250, in __call__
    self.load_middleware()
  File "/home/USER/virtualenv/lib/python2.7/site-packages/django/core/handlers/base.py", line 39, in load_middleware
    for middleware_path in settings.MIDDLEWARE_CLASSES:
  File "/home/USER/virtualenv/lib/python2.7/site-packages/django/utils/functional.py", line 276, in __getattr__
    self._setup()
  File "/home/USER/virtualenv/lib/python2.7/site-packages/django/conf/__init__.py", line 42, in _setup
    self._wrapped = Settings(settings_module)
  File "/home/USER/virtualenv/lib/python2.7/site-packages/django/conf/__init__.py", line 89, in __init__
    raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
Was it helpful?

Solution

First of all you do not need all of those sys.path mess, your nginx vars already set the virtualenv for you.

I suggest you to go in static mode, as dynamic virtualhosting mode is very complex and often hard to debug. Simply change your command line and remove the three uwsgi_param entry from nginx:

/usr/local/sbin/uwsgi
--uid www-data
--socket 127.0.0.1:5050
--master
--logto /var/log/uwsgi_main.log
--logdate
--optimize 2
--processes 4
--harakiri 120
--virtualenv /home/USER/virtualenv/
--pythonpath /home/USER/virtualenv/
--chdir /home/USER/virtualenv/PROJECT
--env DJANGO_SETTINGS_MODULE=PROJECT.settings
--module "django.core.handlers.wsgi:WSGIHandler()"

should be enough (and obviously you can throw away the wsgi script)

OTHER TIPS

My guess is you run the django app in python virtualenv. If so, you need to activate virtualenv in upstart script as well: exect /path/to/virtualenv/activate && /usr/local/sbin/uwsgi

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top