简体   繁体   中英

uWSGI and Django - error running wsgi

WHAT IS WORKING

I'm following wsgi documentation to run django . I'm testing that it's all working before start to use nginx. I succeeded running manage.py and loading the webpage in my browser:

python manage.py runserver 0.0.0.0:8000

在此处输入图片说明

WHAT IS NOT WORKING

The problem comes when I try to run it using uwsgi:

uwsgi --http :8000 --module metrics.wsgi

I can run it without errors , but when I try to load it in my browser I get AppRegistryNotReady error from uwsgi. Any idea about what could be the reason? This is my wsgi.py file:

import os, sys, site, django.core.handlers.wsgi

SITE_DIR = '/home/ubuntu/web/metrics.com/app/'
site.addsitedir(SITE_DIR)
sys.path.append(SITE_DIR)

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

my project structure:

/ubuntu
    /www
        /metrics.com 
            /app         # here's my manage.py file
                metrics/ # here's my wsgi.py and settigs.py files

SOLUTION: An incorrect configuration in wsgi.py was making uWSGI unable to call the application. I solved it using this wsgi.py:

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "metrics.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

And running uwsgi like this:

uwsgi --http :8000 --chdir /home/ubuntu/web/metrics.com/app --module gamemetrics.wsgi

Edit : using --chdir we set the base directory to use for --module.

Edit 2 : In some cases, this can fix NGINX error: upstream prematurely closed connection while reading response header from upstream

1): Highly recommend run uwsgi in emperor mode.

/usr/bin/uwsgi --emperor /etc/uwsgi --pidfile /var/run/uwsgi.pid --daemonize /var/log/uwsgi.log

2) Example wsgi.py for your project:

import os
import sys
ADD_PATH = ['/home/ubuntu/web/metrics.com/app/',]
for item in ADD_PATH:
    sys.path.insert (0, item)
os.environ['PYTHON_EGG_CACHE']= '/tmp/your-project-eggs'
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

3) Example uwsgi config for project (put this in /etc/uwsgi) <- see i.1
------- project.yaml -------

uwsgi:
    print: Project Configuration Started
    socket: /var/tmp/metrics_uwsgi.sock
    pythonpath: /home/ubuntu/web/metrics.com
    env: DJANGO_SETTINGS_MODULE=app.settings
    module: app.wsgi
    chdir: /home/ubuntu/web/metrics.com/app
    daemonize: /home/ubuntu/web/metrics.com/log/uwsgi.log
    pidfile: /var/run/metrics_uwsgi.pid
    max-requests: 5000
    buffer-size: 32768
    harakiri: 30
    reload-mercy: 8
    master: 1
    no-orphans: 1
    touch-reload: /home/ubuntu/web/metrics.com/log/uwsgi
    post-buffering: 8192

4) Include in your nginx config

location /
    {
        uwsgi_pass unix:///var/tmp/metrics_uwsgi.sock;
        include uwsgi_params;
        uwsgi_buffers 8 128k;
     }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM