Tuesday, June 4, 2013

Deploy Django app with Fabric made easy

Here we go,

The last time I deployed my Django app to a production server, I felt pain in the ass. I had to do all the same things repeatedly: copy the source code, create a virtualenv for my app, config nginx, supervisor... And then I found Fabric. Fabric help me do the pre-defined actions in a script file name fabfile.py.

Follow the instructions of a tutorial (http://www.abidibo.net/blog/2012/06/29/deploy-django-applications-nginx-uwsgi-virtualenv-south-git-and-fabric-part-5/), I modified the fabfile to fit my needs. And here are the steps:

1. Install Fabric in your local environment:

(venv)trinh@trinh-pc:~$ pip install Fabric

2. Create a fabfile.py and place it inside your project's folder:


- env.user here is the user of your production server. I use the root user here to avoid using sudo or authenticating stuffs...In practical situation, please use your own user (e.g. trinh :D)
- env.hosts is a list of production servers.
- and the most important thing is I try to make the production server's directory structure is the same as my local machine to make it easy to deploy.

Fabric will user env.user and env.hosts to connect to the production server through SSH (Fabric uses Paramiko to do SSH stuffs \m/)

3. Start deploying your project:

- seting up environment:
(venv)trinh@trinh-pc:/home/projects/myproject/$ fab production setup

- deploying:
(venv)trinh@trinh-pc:/home/projects/myproject/$ fab production deploy