Rolling out Django with Nginx

Austin DeVinney's picture

Software Architect


Rolling out Django with Nginx

Python is an awesome language that is very fun to learn and growing quite a bit in popularity.

Most notably, Reddit runs on a popular framework known as Pylons (now maintained as Pyramid) while several other websites are built on Django. Some of these include Disqus, BitBucket, Pinterest, and Instagram. This is going to be what I view as the path of least resistance to getting yourself up and running with a powerful, modern Django stack!


Going forward, you should have a basic understanding of your operating system and some web development experience (light, at best). We're going to be installing a full web stack, so any familiarity in the space would be helpful. That being said, this will be written from the perspective of a Linux box. I'd highly recommend you pick yourself up one from DigitalOcean or Linode. They are both excellent platforms for getting your own development environment at a reasonable price.

Beyond that, the only requirement we need going forward is to have Python installed and then to get pip, which is a package manager for Python. On Linux, you can get this done by running the following:

sudo apt-get install python 
sudo apt-get install python-pip 

Note that as of April, 2014, this will install both Python 3.3 and 2.7. This is perfectly fine.

Virtual Environments

In Python, it's a good idea to use virtualenv to create virtual environments and work within those. This allows you to install Python libraries or specific to that environment without changing your global installation. When you are in the context of that environment, it effectively becomes a development sandbox that allows you to test and break things with very minimal risk. To install virtualenv simply run:

sudo pip install virtualenv

This will install virtualenv globally to your system and allow you to start making virtual environments for you to work in. You need to not only setup but activate your environment to start working in it. Run the following:

virtualenv ~/python/test_environment 
cd ~/python 
source test_environment/bin/activate 

You will notice that you are now in the context of your environment. Simply type the command "deactivate" to exit it. Keep in mind that this is completely optional. You can install Django and various other libraries on a global scale and get by just fine.

Installing and Creating Django Project

The first thing we want to do is install Django, then create our project. If you're following the virtualenv guide, make sure you're still in the context of whatever environment you setup.

sudo pip install django startproject my_site 

This will make a directory named my_site that effectively contains all of your Django code. You can test to make sure the installation is correct by running the following command, then going to your IP address at port 8000 in a browser (if you're running it locally, That's all you need to get started with Django!

cd my_site
python runserver

If you're looking to learn more about the framework, I highly recommend The Django Book.

Configure Nginx

Nginx is very simple to get up and running and there are several guides to setting up things like LEMP stacks that could prove very useful for you. This will cover the bare-bones setup as well as a simple proxy configuration for Nginx that passes your requests to Django.

sudo apt-get install nginx 
sudo touch /etc/nginx/sites-enabled/django.conf
sudo vi /etc/nginx/sites-enabled/django.conf

You've just installed Nginx and created the Django configuration file. After doing that, we want to use the following config (Note: paths will need to be changed to match your structure):

server {
  listen 80;
  server_name your_domain_name;
  location /static/ {
    alias /path/to/environment/python/test_environment/static;
    expires 30d;
  location / {
    proxy_buffering off;
    proxy_pass http://localhost:8000;

Now, you'll want to start nginx, activate your virtual environment, and start serving up Django pages!

sudo service nginx start
cd ~/python/test_environment/my_site
source ../bin/activate
python runserver

Extending and Scaling

Please do take note that this is not a production environment. If you plan on deploying Django, you should look into running Gunicorn or uWSGI in place of the default server. The Nginx proxy can stay relatively the same between the variants.

I see you like to read printed material. You should check out Nicco's book The End of Big: