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 startproject

Deploying your sample Project:

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

python runserver

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

if you open 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.


chdir           =   #same level as 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 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

Need any Help in your Project?Let's Talk

Latest Comments
Related Articles
Django Raw Sql Queries Vamsi Popuri

When your model query API don't go well or you want more performance, you can use raw sql queries in django. The Django Object Relational ...

Continue Reading...
How To Add A Custom Managers In Django Swetha Naretla

Django Custom Managers - A Manager is the interface through which database query operations are provided to Django models. At least one Manager exists for ...

Continue Reading...
Google plus API integration with django Nikhila Mergu

Using GPlus integration, we can get the user verified email id, general information, friends in a less span of time, and user can also share ...

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