Hosting Django Application with Nginx and UWSGI

Reading Time : ~ .

Setting up Django:

Django is a python based web- application development framework. Setting up a sample app and running it as easy as pie. In here we will use

1. VIRTUALENV:  a tool to create isolated Python environments. If it is not previously installed you can install it using 

sudo apt-get install python-virtualenv

2. PIP: a tool for installing python packages which if not previously available can be installed using 

sudo apt-get install python-pip

Since we now have all the required packages for creating a Django Application. we need to

Create Virtualenv

virtualenv [virtualenv-name]

Activate the virtualenv and install Django in it

source /bin/activate
pip install django

Start a Django project

django-admin.py startproject

Deploying your sample Project:

navigate into the project and you can find manage.py file. There you need to execute

python manage.py runserver

IP-Address and Port are not mandatory and by default, it runs on 127.0.0.1:8000

if you open http://127.0.0.1:8000 in a browser you will have Django welcome page which is enough for our deployment

Installing Nginx:

Nginx is a web server and like every other web server, it has it Pros and Con's. Nginx was an answer to concurrency issue(handling thousands of concurrent connections) faced in apache and raised to fame. 

sudo apt-get install nginx

Nginx configuration:

Nginx configuration can be any directory based on the operating system by default we can save configurations in /etc/nginx/sites-enabled folder.

*** Best practice is to the main config create a file in /etc/nginx/sites-enabled folder and create a symlink from her in a sites-enabled folder. in cases of disabling a website temporarily, we delete the symlink and can create again when necessary.. etc.,

Installing uWSGI:

uWSGI is a gateway between your web server (Nginx in this case) and web application (your sample Django project). there are many WSGI modules available like gunicorn which serve the same purpose.

sudo pip install uwsgi

installs the uWSGI in a global environment and generate a simple configuration file as below tailored to your needs at the same level as env.

[uwsgi] 

chdir           =   #same level as manage.py file
module          =  # generally .wsgi 
home            =  # virtualenv (full path)

# process-related settings
master          = true
processes       = 8
socket          =
chmod-socket    = 666
vacuum          = true
harakiri        = 600
max-requests    = 50000

the uWSGI config file above is of type *.ini and running this command in a shell will run your Django project using uWSGI.( without python manage.py runserver which can only be used for development purposes)

uwsgi --ini [uwsgi-file].ini

this will generate a sock which can be configured in Nginx to serve the application.

Sample Nginx Configuration:

upstream [upstream-name] {
    server unix://[full-path -to-sock-file]
}

server {
    listen      80;
    server_name ;
    charset     utf-8;
    client_max_body_size 75M;

    location / {
        uwsgi_pass  [upstream-name];
        include     uwsgi_params;  #by default will be provided by nginx
    }

}

 What have we done:

Django Application ------------------> uWSGI   ----------------->  Nginx

(web application) ------------------> (gateway)  ------------------> (Webserver)

Now you can visit your given IP-Address and can find the sample welcome page which we previously visited.

Common mistakes:

* WSGI path should be inputted corrected, if not uWSGI can't find your python application

** Socket file location in the uWSGI and Nginx config file should be same, in the latter case we will simply find a 502 error page.

Additional Tips:

Running in a shell is different from daemonizing the command you can visit my previous post which explains how to run a command as a daemon process.

if you're running multiple applications then having all uWSGI configurations in a single folder is the best way to handle. simply symlink all the config files to a folder say /etc/uwsgi/vassals and then modify the command in supervisor configuration to 

/usr/local/bin/uwsgi --emperor /etc/uwsgi/vassals

The same approach can be followed in any VPS or your own Debian based PC(like ubuntu). 

    By Posted On
SENIOR DEVELOPER at MICROPYRAMID

Need any Help in your Project?Let's Talk

Latest Comments
Related Articles
How to Create your own e-commerce shop using Django-Oscar. Shirisha Gaddi

Oscar is an open-source ecommerce framework for Django. Django Oscar provides a base platform to build an online shop. Oscar is built as a highly ...

Continue Reading...
Integration Of Linkedin API in python django Nikhila Mergu

Using Linkedin integration, we can get the user verified email id, general information, work history in a less span of time, and a user can ...

Continue Reading...
How to customize Django Oscar Models, Views and URL's Chaitanya Kattineni

In order to customize models, views and urls, you need to fork an Oscar core app in which model/view resides. Then you should be able ...

Continue Reading...

Subscribe To our news letter

Subscribe to our news letter to receive latest blog posts into your inbox. Please fill your email address in the below form.
*We don't provide your email contact details to any third parties