The Django gunicorn fabfile project

Open sourcing a Fabric file to automate the setup of Django projects with Ubuntu

Since I read about Green Unicorn, better known as gunicorn, a Python WSGI HTTP server that goes along very well with Django, I knew I had to try it, and trying it I did. And I liked it. There were many steps to make a server work but overall it was cleaner than other approaches, including the Django deployment using Nginx, Apache and mod_wsgi that I described long time ago. And many reports indicate that a Django setup with gunicorn performs better than one using Apache.

But when you have to setup many servers with the same configuration, the process gets boring and repetitive, and when you're bored you make mistakes. This is the perfect scenario for automating with Python and a Fabric script.

I wanted the initial version of my fabfile to do the following:

  • Start with a clean install of Ubuntu 11.10, just with an openssh-server running.
  • Install all the Ubuntu packages needed for running a basic Django site, these include Nginx, virtualenv, some Python development tools, and PostgreSQL.
  • Create virtual environments and manage them with virtualenvwrapper.
  • Install a few Python packages in the virtual environments: Django, gunicorn, iptyhon, psycopg2, to name a few.
  • Create the configuration files to manage both staging and production sites with upstart.
  • Grab the code for a Django project from a git repository.
  • Keep the project's specific outside of version control.
  • Make everything work with just one or two commands from the shell.

And that's what the first version of this fabfile is doing. I don't have the time to explain all the details of the setup, Senko does a good job with his Django setup using Nginx and gunicorn and I was inspired by him (thanks dude), but I can give you the code I wrote and commited to GitHub.

I've called this the Django gunicorn fabfile project, so go get it and play with it.

Join the conversation

Wouldn't puppet be more

Wouldn't puppet be more suited to this task, for bootstrapping new instances as well as making it easier to keep existing instances in sync with new config changes? Otherwise you have to spin up new instances for many types of edits to your fabfile, or you have to add temporal commands to the fabfile just for migrating existing instances.

I haven't explored Puppet

I haven't explored Puppet yet; I've read about it but I still have to play with it.

Notice this fabfile has separate functions for the setup stage, which install all the required packages on a clean server, and for actually setting up or updating the Django project.

Ideally you should run the setup function just once on a new server and then you just run the update project function everytime you make changes on the project (new projects, settings updated).

Keep your comments relevant, written in good English and don't spam. Let's create useful and valuable discussions. Markdown is welcome.

Add your comment