Django app deployment on nGINX

你。 提交于 2019-12-06 03:36:17

问题


I want to deploy Django application on nGINX server. I'm using uWSGI. I looked up in many tutorials but none worked. Django application runs perfectly as a standalone app. What is the simplest way to have the same app running on nGINX??

I'm stuck here and want a solution.. :-(

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

my site-enabled n conf.d and all are in /etc/nginx/

I did install uWSGI but couldn't find any folder named uwsgi which has apps-installed folder/file in it


回答1:


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

STEP 1. Create a file say uwsgi.ini in your Django Project Directory. i.e besides 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()

STEP 2. Under /etc/nginx/sites-available add .conf file

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;
                  }

       }

STEP 3. In nginx.conf, pass the request to your Django application

Under the server { } block,

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

STEP 4. Run the uwsgi.ini

> uwsgi --ini uwsgi.ini

Now any request to your nGINX will pass the request to your Django App via uwsgi.. Enjoy :)




回答2:


The simplest way (and quite efficient at that) would be to use Gunicorn unless you need to stick to uWSGI. They have nice documentation and it's quick and quite easy to deploy.

I have few websites (including production) and something like this works:

website_gunicorn.conf.py (place anywhere you like):

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

corresponding NGINX config (partial, include in main config):

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;
    }
}

Then you should be able to start like this (of course after installing Gunicorn - pip install gunicorn):

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

and NGINX should connect to the socket and serve the website (static files will be served directly by NGINX saving you some memory).

For more details see Gunicorn docs on deployment and configuration. Note that I have daemon=False in Gunicorn config. This is because I use Supervisor to control it. You may or may not want to get rid of that line.




回答3:


Try to stay away from distro-related howtos, they can easily push you on the wrong direction.

Follow the quickstart here:

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

(for follow, i mean, 'understand' not copy&paste ;)

Start with a simple configuration where nginx forward all of the requests to uWSGI.

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



来源:https://stackoverflow.com/questions/12329807/django-app-deployment-on-nginx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!