Innovate anywhere, anytime withruncode.io Your cloud-based dev studio.
Django

Hosting Django Application with Nginx and UWSGI

2022-07-20

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. 

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