Nginx – why django project failed deploying? – django + nginx + uwsgi

djangonginxpythonUbuntuuwsgi

I am trying to deploy a Django project through Nginx and uWSGI. If I run the project manually running python manage.py runserver :8000 it works fine, so the project itself is not the problem I guess.

I installed Django and dependencies inside a dedicated virtualenv, and put the project in. I also installed nginx and uwsgi OUTSIDE the virtualenv, and started creating config files:

/etc/nginx/sites-available/myproject (symlinked in sites-enabled):

upstream uwsgi_myproject {
    server 127.0.0.1:5678;
}

server {
    listen 80;
    server_name my.url.net;
    set $home /path/to/myvirtualenv;
    access_log /path/to/myvirtualenv/log/access_uwsgi.log;
    error_log /path/to/myvirtualenv/log/error_uwsgi.log;

    client_max_body_size 10m;
    keepalive_timeout 120;

    location / {
        uwsgi_pass uwsgi_myproject;
        include uwsgi_params;
        gzip on;
        uwsgi_param UWSGI_CHDIR $home/path/to/myproject;
        uwsgi_param UWSGI_SCRIPT uwsgi;
        uwsgi_param UWSGI_PYHOME $home;
        root $home;
    }
    location /static/  {
        alias /path/to/myvirtualenv/path/to/myproject/static/;
        expires max;
        autoindex off;
    }
    location /media_adm/  {
        alias /path/to/myvirtualenv/lib/python2.7/site-packages/grappelli/static/;
        autoindex off;
    }
}

/etc/init/uwsgi_myproject.conf:

description "uWSGI starter for myproject"

start on (local-filesystems and runlevel [2345])
stop on runlevel [016]

respawn

exec /path/to/myvirtualenv/bin/uwsgi \
--uid venvowner \
--home /path/to/myvirtualenv \
--pythonpath /path/to/myvirtualenv/path/to/myproject/ \
--socket 127.0.0.1:5678 \
--chmod-socket \
--module wsgi \
-b 8192 \
--logdate \
--optimize 2 \
--processes 2 \
--master \
--logto /path/to/myvirtualenv/log/uwsgi.log

/path/to/myvirtualenv/path/to/myproject/wsgi.py:

import os, sys, site

site.addsitedir('/path/to/myvirtualenv/lib/python2.7/site-packages')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")

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

Then I rebooted the machine and try to connect to my.url.net, but there's no response(when I installed nginx before importing myproject, it responded with "Welcome to Nginx!"). I noticed with lsof | grep LISTEN that the upstream socket hasn't been created. The logs don't output anything relevant, so I tried to execute uwsgi manually with the same parameters than uwsgi_myproject.conf and obtained this output:

current working directory: /etc/init
detected binary path: /path/to/myvirtualenv/bin/uwsgi
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 6674
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
The -s/--socket option is missing and stdin is not a socket.

EDIT: So the last thing I tried was reordering the parameters. Now the –socket parameter is successfully processed, but I still obtain this message in the output lines:

*** no app loaded. going in full dynamic mode ***

Some documents I've been reading:

Any ideas about what am I missing or overlooking?

CONTINUE: I reviewed the docs again and try a different approach: using an .ini file and run it manually to see what happens:

It is a bit ugly, but here goes:

[uwsgi]
socket = 127.0.0.1:5678
uid = venvowner
chdir = /path/to/myvirtualenv
home = /path/to/myvirtualenv
virtualenv = /path/to/myvirtualenv
pythonpath = /path/to/myvirtualenv/path/to/myproject
module = wsgi
master = true
optimize = 2
processes = 2
logto = /path/to/myvirtualenv/log/uwsgi.log

I obtained some useful logs:

*** Python threads support is disabled. You can enable it with --enable-threads ***
*** Operational MODE: preforking ***
ImportError: No module named wsgi
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
*** uWSGI is running in multiple interpreter mode ***

The workers are spawned, but the stack obviously fails. I'll take this on another day.

Best Answer

Finally, I made it work. Thanks to @DhirajThakur for his comment, which helped me to look into the right direction.

/etc/init/uwsgi_myproject.conf:

description "uWSGI starter for myproject"

start on (local-filesystems and runlevel [2345])
stop on runlevel [016]

respawn

exec /usr/local/bin/uwsgi /etc/init/myproject.ini

/etc/init/myproject.ini:

[uwsgi]
master = true
socket = 127.0.0.1:5678
uid = venvowner
chdir = /path/to/myvirtualenv
home = /path/to/myvirtualenv
virtualenv = /path/to/myvirtualenv
pythonpath = /path/to/myvirtualenv/path/to/myproject
wsgi-file = /path/to/myvirtualenv/path/to/myproject/wsgi.py
optimize = 2
processes = 2
logto = /path/to/myvirtualenv/log/uwsgi.log

Somehow, I've got the feeling that the way I did resolve the problem is a little ugly tough.