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 browser you will have django welcome page which is enough for our deployment.

 

Installing Nginx:

‚ÄčNginx is a webserver and like every other webserver it has it Pro's 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 operating system by default we can save configurations in /etc/nginx/sites-enabled folder .

*** Best practice is to main config create file in /etc/nginx/sites-enabled folder and create a symlink from her in sites-enabed folder. in cases of disabling a website temporarily we delete 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 global environment and generate a simple configuration file as below tailored to your needs in 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 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 cant find your python application

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

Additional Tips:

Running in 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 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
Running Django with PyPy to boost performance Dinesh Deshmukh

Running Django with PyPy to boost performance

Continue Reading...
Extract data from PDF and all Microsoft Office files in python Ashwin Kumar

The quick way to get/extract text from PDFs in Python is with the Python library "slate". Slate is a Python package that simplifies the process ...

Continue Reading...
Integrating Gitlab in django project with gitlab api for gitlab authentication and access Nikhila Mergu

This is a simple way to integrate gitlab authentication in your django apps.

We can get user verified email id, general information, git lab URL ...

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