Kartik Rokde
Kartik Rokde

Reputation: 3913

Django app deployment on nGINX

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

Upvotes: 7

Views: 7469

Answers (4)

Manoj
Manoj

Reputation: 509

STEP 1. Install Nginx in your system.

sudo apt-get install nginx

STEP 2. edit your setting.py file inside your Django project

ALLOWED_HOSTS = []

to

ALLOWED_HOSTS = ['*']

STEP 3. Install gunicorn

pip3 install gunicorn

STEP 4. create a .service file and write some own configuration

    sudo vi /etc/systemd/system/gunicorn.service

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=root
Group=www-data
WorkingDirectory=<your project root directory>
ExecStart=<path of your project>/venv/bin/gunicorn --access-logfile - --workers 3 --bind unix:<path of your wsgi file>/restapi.sock rest_Api.wsgi:application
[Install]
WantedBy=multi-user.target

STEP 5. Run the given command

  sudo systemctl enable gunicorn

  sudo systemctl start gunicorn

STEP 6. create a file inside sites-available

 sudo vi /etc/nginx/sites-available/gunicorn

STEP 7. write the give configuration inside the above file i.e gunicorn

server {
    listen <port on which you want to configure>;
    server_name <ip address> or <name of the server>;

    location = /favicon.ico { access_log off; log_not_found off; }
    location <path of your static directory> {
        root <path of your root project>;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:<path of your restapi.sock file>/restapi.sock;
    }
}

STEP 8. now comment all the line inside the default file.

    sudo vi /etc/nginx/sites-available/default

STEP 9. Run these commands to test

  sudo ln -s /etc/nginx/sites-available/gunicorn1 /etc/nginx/sites-enabled/

  sudo nginx -t

STEP 10. restart the Nginx server

   sudo systemctl restart nginx

Upvotes: 3

Kartik Rokde
Kartik Rokde

Reputation: 3913

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 :)

Upvotes: 13

roberto
roberto

Reputation: 12933

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.

Upvotes: 1

kgr
kgr

Reputation: 9948

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.

Upvotes: 2

Related Questions