Provision & deploy a simple Django stack on Debian 6 using Fabric & Cuisine
Warning: This post is for people who know what Python, Django, and Debian are.
It turns out that most people ever only need a single VPS node (or less) for their Django site. When it comes to small sites, using Chef and Puppet for deployment is kind of overkill. In our previous iteration at Dolbeau I implemented Chef with the Deploy Resource. It worked alright, except for the fact that my deploys took way too long, not to mention it was super overcomplicated and scary when it failed. I wanted to convert my huge ass Chef deployment into a tiny Fabric script (with the help of Cuisine), so I did!
You can find it in a Github repo called deploy-django.
The stack is composed of:
- Nginx
- Gunicorn
- Django
- Memcached
- Postgresql
Supervisord is basically optional, but you need to edit the fabfile. Right now I personally just restart guncorn using a kill -HUP during the deploy process. This seems to work fine for my purposes. I’ve included an example init script in the provision folder.
To use the fabfile, you first need to install the requirements:
pip install -r requirements.txt
Then, edit the deploy_settings.py file:
APP_NAME = the name of your python app
APP_REPO = the git repo
APP_REPO_BRANCH = the branch you want to deploy
APP_HOSTS = a list of hosts
REMOTE_USER, REMOTE_GROUP = the linux user that should own the deploy directory on the server
DJANGO_STATIC_ROOT = the directory name of your static_root in your app
DJANGO_MEDIA_ROOT = the directory name of your media_root in your app
DEBIAN_PACKAGES = a list of debian packages to install, the default list contains a bunch of things that you may not need
DEPLOY_TREE = defines the deploy directory tree. The default deploy tree creates a /srv/<APP_NAME> for your app.
DEPLOY_TREE = {
APP_NAME: {
'releases': {},
'shared': {
'logs': {},
'media_root': {},
DJANGO_STATIC_ROOT: {}
}
}
}
Then, you edit the server config files in the provision directory. The provision directory is structured as if it is the root directory of your server’s disk and it contains basic config files for your stack. You will need to edit these files (and rename some of them) before trying to deploy, they are NOT templates.
/etc/
rc.local
init.d/myapp
gunicorn/myapp.conf.py
nginx/
nginx.conf
sites-available/myapp
postgresql/8.4/main/postgresql.conf
supervisor/conf.d/myapp.conf
/srv/myapp/shared/settings_local.py
Then you can try to initialize, provision and deploy:
fab initialize
This will set up the required user and inject your public ssh-key for passwordless login.
fab provision
This will install the packages and set up the server config files
fab deploy
This will deploy your app!
It should all work… if it doesn’t then feel free to file an issue or fork the repo.