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 settings.py 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.

Join the conversation

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.