Deploying a Django Application on nGINX

I want to deploy a Django application on an nGINX server. I am using uWSGI. I searched in many textbooks, but no one worked. The Django application works great as a standalone application. What is the easiest way to run the same application on nGINX?

I am stuck here and want a solution ..: - (

my www folder is in /usr/share/nginx/www

my site is n-enabled conf.dand everyone is in/etc/nginx/

I installed uWSGI, but could not find a folder called uwsgi, in which there is an installed program folder / file

+5
source share
3 answers

Once you have created the dJango application. Just follow these steps:

1. , , uwsgi.ini Django Project Directory. manage.py

[uwsgi]
# set the http port
http = :<port_no>

# change to django project directory
chdir = <project directory>

# add /var/www to the pythonpath, in this way we can use the project.app format
pythonpath = /var/www

# set the project settings name
env = DJANGO_SETTINGS_MODULE=<project_name>.settings

# load django
module = django.core.handlers.wsgi:WSGIHandler()

2. /etc/nginx/sites-available .conf

server {
listen 84;
server_name example.com;
access_log /var/log/nginx/sample_project.access.log;
error_log /var/log/nginx/sample_project.error.log;

# https://docs.djangoproject.com/en/dev/howto/static-files/#serving-static-files-in-production
location /static/ { # STATIC_URL
    alias /home/www/myhostname.com/static/; # STATIC_ROOT
    expires 30d;
                  }

       }

3. nginx.conf Django

{}

location /yourapp {
           include uwsgi_params;
           uwsgi_pass <your app address, eg.localhost>:<portno>;
                   }

4. uwsgi.ini

> uwsgi --ini uwsgi.ini

nGINX Django uwsgi. :)

+12

( ) - Gunicorn, uWSGI. .

( ), - :

website_gunicorn.conf.py( ):

import multiprocessing
daemon = False
bind = "unix:/tmp/gunicorn.sock"
workers = multiprocessing.cpu_count() * 2 + 1
timeout = 60

NGINX (, ):

upstream gunicorn {
    server unix:/tmp/gunicorn.sock fail_timeout=0;
}

server {
    listen 80;
    server_name example.com;
    access_log /var/log/access.log combined;
    error_log /var/log/error.log error;

    keepalive_timeout 5;

    # path for static files
    root /path/to/your/static/files;

    location @django {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_ignore_client_abort off;
        proxy_buffering off;
        proxy_redirect off;
        proxy_pass   http://gunicorn;
        proxy_read_timeout 60;
    }         

    location / {
        try_files $uri @django;
    }
}

(, Gunicorn - pip install gunicorn):

gunicorn_django -c /path/to/website_gunicorn.conf.py

NGINX - ( NGINX, ).

. Gunicorn . , daemon=False Gunicorn. , Supervisor, . .

+1

howtos, .

:

http://projects.unbit.it/uwsgi/wiki/Quickstart

(for later, I mean, "understand" not to copy and paste;)

Start with a simple configuration where nginx redirects all requests to uWSGI.

Serving static files is another matter, and it does not depend on the application server, so you can follow the official Django docs.

+1
source

All Articles