Ryan Williams.org

The random babblings of a software developer

Fork-me

Deploying Django sites with Fabric, Pip and virtualenv

As I’m doing Django project deployments on a frequent basis, I’ve taken the time to look into using Fabric and Pip to automate the process. With a bit of experimenting I believe I’ve come up with a nice clean way to do this. My fabric deployment script allows me to deploy a project to a new server and because I use virtualenv to keep things segregated from each other and the base Python system I’m free to use Pip to install anything that the project should require.

So here’s how I do it.

On my local machine I first installed Pip and Fabric.

bc(brush: plain). sudo easy_install pip
sudo pip install fabric

On the server I had to install Pip and virtualenv.

bc(brush: plain). sudo easy_install pip
sudo pip install virtualenv

Assume that goes to plan, the next step is to create a deployment script (fabfile.py). Here is my fabfile.py which is hopefully commented well enough so you can see what it’s doing. It should make more sense after reading the rest of this post.

Fabric is very simple to operate. Basically you pass the functions you want to call into the “fab” command line script.

For example the following command will execute the “staging()” then “deploy()” functions.

bc(brush: plain). fab staging deploy

The “staging()” function in my script sets up some initial settings such as the server to deploy to (config.fab_hosts) and the name of the Django settings file. But substituing this for “production” I can easily send the deployment to my live server.

When my site is first deployed on the new server which currently only has Pip and virtualenv installed on it I will need to create the virtual environment and install all the required packages using Pip.

Pip knows what to install because I pass a “requirements” file to it. This is a simple plain text file which lists the packages to install. I store this in the root of my project and name it “requirements.txt”. Here’s an example of what I use:

bc(brush: plain). Django==1.0.2-final
MySQL-python>=1.2.2
PIL==1.1.6
simplejson>=2.0.9
python-memcached>=1.44

To setup my project on the new server using my fabfile is quite simple. All I need to do is execute:

bc(brush: plain). fab staging setup

This calls the “setup()” function which does the following:

  1. Creates the initial directory to deploy to
  2. Creates a new virtual environment
  3. Clones the git repository which contains the project
  4. Creates some initial directories
  5. Copies the latest code checkout into a unique timestamped directory
  6. Calls Pip to install everything needed as specified in my projects requirements.txt file.

Once that has run its course the server is ready to deploy to using the command:

bc(brush: plain). fab staging deploy

With my fabfile this will do the following:

  1. Pull the latest changes into the server’s copy of the git repository
  2. Copy the latest code into a unique timestamped directory under the “releases” directory
  3. Symlink the new release directory to the name “current”
  4. Symlink the “uploads” directory to a shared directory outside of the main project
  5. Run syncdb and migrations
  6. Restart lighttpd

Some of this stuff may be only essential to my projects, but I hope it gives you and idea of how Fabric works and once you get it going it saves a lot of time and effort.

Posted

Comments

slafs

ago, slafs said

That’s a nice one! Could you share your django.fcgi file also ?

Regards

blenblen

ago, blenblen said

just miss the nginx configuration and the gunicorn launch and it’s perfect :)

drhodes

ago, drhodes said

thanks for the walk through o/

granite richmond va

ago, granite richmond va said

this is very nice resource for information that lunch new project for fabric and pip he write script for install .

granite countertops richmond va

ago, granite countertops richmond va said

this project for new comer can get how to make strategies their projects.

Recyclage d'ordinateurs

ago, Recyclage d'ordinateurs said

As usual, a fantastic post – keep going! I’m saying thanks from the thousands who enjoy your blog and don’t say anything!

SEO Company

ago, SEO Company said

You are so inspirational and you talk sense. That’s important. You’re intelligent and you have a lot of heart. I love your posts! Thank you!

bra vibrator

ago, bra vibrator said

I apologise, but it does not approach me. Who else, what can prompt?

crevasse

ago, crevasse said

It is a pity, that now I can not express – there is no free time. I will be released – I will necessarily express the opinion.

borax

ago, borax said

Rather useful idea from netting

coconutoil

ago, coconutoil said

In it something is. Now all became clear, many thanks for an explanation. from straightway

Katy Dental Services, Katy Dentistry

ago, Katy Dental Services, Katy Dentistry said

When looking for a roofing contractor, you should take two things into consideration. First, know the credibility of the company. You will know its credibility if they are in the business for a very long time and has gained the trust of nearly all people. Second and most importantly, you must know the type of services they provide. It must be nothing but the highest degree of quality. Doing a detailed research about the company’s roof and repair services will help you get the right one.

Add Comment

If you want to add a comment, then simply fill in the form below. All fields are required.