s.spirit
s.spirit

Reputation: 343

Set path to UWSGI correctly

Im trying to setup my uwsgi and faced with a problem. When I set path to my project in my project folder, UWSGI saw my wsgi.py but doesn't saw any other apps, cause they are on one level up... But if I try to set path just to my project folder, UWSGI doesn't saw my wsgi.py... How can I set my path correctly?

You can see my project hierarchy and UWSGI settings, where it saw my wsgi.py, but not my apps (No module named menuItem as example), below.

Hierarchy:

root --reddish ---env ---reddish ----article ----menuItem ----myProject -----wsgi.py -----settings.py

UWSGI settings:

[uwsgi]
virtualenv=/root/reddish/env/
chdir=/root/reddish/reddish/myProject/
module=wsgi:application
env=DJANGO_SETTINGS_MODULE=settings
master=True
plugins=python27
vacuum=True
socket=/tmp/%n.sock
pidfile=/tmp/%n.pid
daemonize=/var/log/uwsgi/%n.log

Upvotes: 4

Views: 15449

Answers (3)

dmitryro
dmitryro

Reputation: 3516

If your manage.py is in reddish, create a directory 'settings' in the same directory your manage.py seats, create default settings file like this ./settings/settings.py

include:

try:

    from development_settings import *

except ImportError:

    from default_settings import *

and then, after setting your configs in settings/development_settings.py and, as a fallback, in default_settings.py, edit manage.py to be:

import os
import sys

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings.settings")

    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)

and make reddish/wsgi.py to be:

import os

from django.core.wsgi import get_wsgi_application

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

application = get_wsgi_application()

So you'll have a directory structure like:

     +--conf/uwsgi_params
     |
     +--logs/uwsgi.log 
     |
/root/reddish-__init__.py  # run touch __init__.py if does not exist
             |
             +uwsgi.ini
             |
             +manage.py
             |
             +reddish/__init__.py # run touch __init__.py if does not exist
             |       |
             |       +urls.py
             |       |    
             |       +wsgi.py
             |             
             +settings/__init__.py # run touch __init__.py if does not exist
                      |
                      +settings.py # listed below
                      |
                      +development_settings.py # your development settings
                      |
                      +default_settings.py # your fallback settings

At that point you should be able to get rid of myProject/settings.py in favor of settings/development_settings.py Also remember, that in this case the application name in settings/development_settings.py and settings/default_settings.py

should include lines:

ROOT_URLCONF = 'reddish.urls'
WSGI_APPLICATION = 'reddish.wsgi.application'

If you're using nginx as the reverse proxy, your nginx config server section should have:

root /root/reddish/;

location /media {
    alias /root/reddish/media;  #create this directory if does no exist
}

location /static {
    alias /root/reddish/static; #create this directory if does not exist
                                #and use with ./manage.py collectstatic
}

The uwsgi_params file used with nginx (in /root/conf) should include:

uwsgi_param QUERY_STRING $query_string;
uwsgi_param REQUEST_METHOD $request_method;
uwsgi_param CONTENT_TYPE $content_type;
uwsgi_param CONTENT_LENGTH $content_length;
uwsgi_param REQUEST_URI $request_uri;
uwsgi_param PATH_INFO $document_uri;
uwsgi_param DOCUMENT_ROOT $document_root;
uwsgi_param SERVER_PROTOCOL $server_protocol;
uwsgi_param REMOTE_ADDR $remote_addr;
uwsgi_param REMOTE_PORT $remote_port;
uwsgi_param SERVER_ADDR $server_addr;
uwsgi_param SERVER_PORT $server_port;
uwsgi_param SERVER_NAME $server_name;

and then if you use socket rather than clustering set in your nginx conf

location / {
       uwsgi_pass unix:///tmp/uwsgi.sock; # or /tmp/%n.sock-adjust uwsgi.ini
       include /root/conf/uwsgi_params;
       proxy_set_header X-Forwarded-Protocol $scheme;
       uwsgi_modifier1 30;
       proxy_set_header HTTP_AUTHORIZATION $http_authorization;
       proxy_set_header  X-Real-IP  $remote_addr;
       proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
       proxy_set_header  Host $http_host;
       proxy_redirect  off;
       uwsgi_read_timeout 900;
}; 

Upvotes: 2

e4c5
e4c5

Reputation: 53774

Always avoid running your uWSGI instances as root. You can drop privileges using the uid and gid options:

http://uwsgi-docs.readthedocs.io/en/latest/WSGIquickstart.html#security-and-availability

That ideally means you should move the app out of the root folder as well. But for the moment, you need to fix your path

virtualenv=/root/reddish/env/
chdir=/root/reddish/reddish/

Another point is not to setup your virtualenv as part of your project files. They should ideally be kept separately.

Both these paths should ideally be outside the /root/ folder.

update: The fact that you might need to use different virtualenvs for each project doesn't really matter. In fact there are situations where you might need two virtualenvs for the same project! Consider this: Suppose you are working on django 1.9 at the moment. Django 1.10 is around the corner and may want to upgrade. Then you create a new virtualenv for django 1.10 if you put it inside your project now you have another 30-40 MB of code there that doesn't belong to you. That makes version control and backups difficult.

Virtualenvs are not really part of your project but it's the python installation and third party libraries that your project depend on.

Upvotes: 7

doniyor
doniyor

Reputation: 37934

you need to change to redish and from there point to your settings.

chdir=/root/reddish/reddish
env=DJANGO_SETTINGS_MODULE=myProject.settings

Upvotes: 2

Related Questions