Tips / Nginx

How to setup Django with uWSGI and Nginx using CentOS/RHEL 6.x

Django is a Python framework that helps you to build rapid development and clean design applications. uWSGI is a WSGI implementation and we will install uWSGI so it creates a Unix socket, and serves data to the web server using the WSGI protocol. Nginx on the other side, is a free/open-source high-performance HTTP server/proxy. Today we will cover the installation of Django, uWSGI and Nginx so you can start testing your apps.


    • This Django-uWSGI-Nginx tutorial has been tested using CentOS 6.x Linux using a virtualenv.
    • is the example domain name, substitute with your own domain.
    • port 8000 will be the default port (django default port), but you can change it if your server is listening into other ports.

Install virtualenv

Make sure you are in a virtualenv for all the installation process:

yum install python-virtualenv* -y
virtualenv uwsgi-tuto

You should see this kind of output:

New python executable in uwsgi-tuto/bin/python
Installing setuptools............done.
Installing pip...............done.
cd uwsgi-tuto
source bin/activate

Install Django

Install Django into your virtualenv and create a test project:

pip install Django

Expected output:

Downloading/unpacking Django
Downloading Django-1.5.1.tar.gz (8.0Mb): 8.0Mb downloaded
Running egg_info for package Django
warning: no previously-included files matching '__pycache__' found under directory '*'
warning: no previously-included files matching '*.py[co]' found under directory '*'
Installing collected packages: Django
Running install for Django
changing mode of build/scripts-2.6/ from 644 to 755
warning: no previously-included files matching '__pycache__' found under directory '*'
warning: no previously-included files matching '*.py[co]' found under directory '*'
changing mode of /root/uwsgi-tuto/bin/ to 755
Successfully installed Django
Cleaning up...
(uwsgi-tuto) startproject mysite
cd mysite

Install uwsgi

pip install uwsgi

You should see this kind of output:

Downloading/unpacking uwsgi
Downloading uwsgi-1.9.11.tar.gz (634Kb): 634Kb downloaded
Running egg_info for package uwsgi
Installing collected packages: uwsgi
Running install for uwsgi
using profile: buildconf/default.ini
detected include path: ['/usr/local/include', '/usr/lib/gcc/x86_64-redhat-linux/4.4.7/include', '/usr/include']
Patching "bin_name" to properly install_scripts dir
detected CPU cores: 1
*** uWSGI is ready, launch it with /root/uwsgi-tuto/bin/uwsgi ***
Successfully installed uwsgi
Cleaning up...

Create a test app

pico -w

Paste this inside the new file:

# file 
def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return "Hello World"

Run the new app:

uwsgi --http :8000 --wsgi-file


*** Starting uWSGI 1.9.11 (64bit) on [Mon May 27 10:35:06 2013] ***
compiled with version: 4.4.7 20120313 (Red Hat 4.4.7-3) on 27 May 2013 10:32:41
os: Linux-2.6.32-358.6.1.el6.x86_64 #1 SMP Tue Apr 23 19:29:00 UTC 2013
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 1
current working directory: /root/uwsgi-tuto/mysite
detected binary path: /root/uwsgi-tuto/bin/uwsgi
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 7608
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
uWSGI http bound on :8000 fd 4
spawned uWSGI http 1 (pid: 6040)
uwsgi socket 0 bound to TCP address (port auto-assigned) fd 3
Python version: 2.6.6 (r266:84292, Feb 22 2013, 00:00:18) [GCC 4.4.7 20120313 (Red Hat 4.4.7-3)]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x1747e70
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 72720 bytes (71 KB) for 1 cores
*** Operational MODE: single process ***
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x1747e70 pid: 6039 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (and the only) (pid: 6039, cores: 1)

Alright, our server is up and running uWSGI with python… leave that command running and go to your browser, digit: http://YOUR.IP.ADDRESS:8000/, if everything is ok you should see a ‘Hello World’ message at the top of the screen.

Testing Django

python runserver YOUR.IP.ADDRESS:8000
Validating models...

0 errors found
May 27, 2013 - 07:40:53
Django version 1.5.1, using settings 'mysite.settings'
Development server is running at http://YOUR.IP.ADDRESS:8000/
Quit the server with CONTROL-C.

Now point to http://YOUR.IP.ADDRESS:8000/, you should see a welcome message like this:

It worked!
Congratulations on your first Django-powered page.

CTRL+C to shut off the previous command, and now let’s try to run it using uWSGI:

uwsgi --http :8000 --module mysite.wsgi

Configuring Nginx

Now, let’s configure Nginx to work with these apps. Use the uwsgi_params file, located at: /etc/nginx/uwsgi_params or /usr/local/nginx/uwsgi_params.

First, let’s locate thje uwsgi_params file:

(uwsgi-tuto)[]locate uwsgi_params

Check your current directory:

Copy uwsgi_params into your curren location:

(uwsgi-tuto)[]cp /etc/nginx/uwsgi_params . -v
`/etc/nginx/uwsgi_params' -> `./uwsgi_params'

(uwsgi-tuto)[]ls -alh
total 24K
drwxr-xr-x 3 root root 4.0K May 27 10:48 .
drwxr-xr-x 6 root root 4.0K May 27 10:33 ..
-rw-r--r-- 1 root root 249 May 27 10:24
drwxr-xr-x 2 root root 4.0K May 27 10:43 mysite
-rw-r--r-- 1 root root 124 May 27 10:34
-rw-r--r-- 1 root root 623 May 27 10:48 uwsgi_params

Nginx virtual config file:

This is the template I used and checked it’s working fine, but you can tune this configuration depending on your needs and app specific design:

### example.conf nginx configuration

# the upstream component nginx needs to connect to
upstream django {
server; # for a web port socket (we'll use this first)

# configuration of the server
server {

# the port your site will be served on
listen 80;

# the domain name used for the test, replace it with your own domain.

# the default charset
charset utf-8;

# Set max upload size
client_max_body_size 100M; # adjust according to your needs

# Django media & static files
location /media {
alias /root/uwsgi-tuto/mysite/mysite/media;

location /static {
alias /root/uwsgi-tuto/mysite/mysite/static;

# All the other stuff is handled directly by Django
location / {
uwsgi_pass django;
include /root/uwsgi-tuto/mysite/uwsgi_params; # the uwsgi_params file you installed

Finally, let’s run our Django app with uWSGI and Nginx

uwsgi --socket :8001 --module mysite.wsgi &

Now, you should be able to see your Django content using (if you configured nginx to listen in 80 port).

Need more help? Check out the official documentation for each one of this tools:

  1. Nginx:
  2. Django:
  3. uWSGI:

Popular search terms:

  • uwsgi virtualenv centos 7
  • centos nginx uwsgi django
  • centos6 5 nginx uwsgi django chkconfig
  • centos6 nginx uwsgi examle set-up

Esteban Borges

Linux Geek, Webperf Addict, Nginx Fan. CTO @Infranetworking

  • pembo13

    What’s the standard way for starting up uwsgi long-term? Surely one isn’t manually starting uwsgi on each server boot.

  • Dylan

    I cannot stop uwsgi now, every incoming request interrupts the command line??? any ideas on how to stop this, ctrl+C broke the foreground process but the background processes are still coming in!

  • Agustin Landivar

    in the part

    Nginx virtual config file:This is the
    template I used and checked it’s working fine, but you can tune this
    configuration depending on your needs and app specific design

    which file does that need to go in, the nginx.conf, of i need to create the available and enabled site folders and mysite.conf files ?