Hosting Django Application with Nginx and UWSGI

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

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

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

Create Virtualenv

Activate the virtualenv and install Django in it

Start a Django project

Deploying your sample Project:

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

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.

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.

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)

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

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

The article was originally published at MicroPyramid blog.

Python, Django, Android and IOS, reactjs, react-native, AWS, Salesforce consulting & development company