Fabric file and related resources for deploying a Mezzanine project to a Webfaction shared hosting account.
Download fabfile.py
, fabsettings.py
, wsgi.py
and deploy/
to your Mezzanine project folder, replacing them if they already exist.
- Mezzanine
- Django
- Fabric
- A git repo with your project files
- A pip requirements file
- pip
- virtualenv
- supervisor
- Git app
- memcached
Note: this script can install the server pre-requisites for you.
- Copy the contents of
fabsettings.py
tolocal_settings.py
and tweak to your liking. This is the only file you have to edit, all others will be populated by Fabric. All available settings are explained infabsettings.py
. These settings are different from those provided insettings.py
by Mezzanine, so make sure you only use the ones provided byfabsettings.py
. - Run
fab prepare_webfaction
to prepare your account for hosting your projects. You only need to run this task once for each account. All subsequent projects can skip this step. - In your dev machine and in your project directory run
fab all
to setup everything for your project in the server.fab all
simply callsfab create
and thefab deploy:first=True
. It basically sets up your project environment and then deploys it for the first time. - Subsequent deployments can be done with
fab deploy
. If you usefab deploy:backup=True
, Fabric will backup your project database and static files before deploying the current version of the project. - You can setup up a cronjob for polling Twitter with
fab setup_twitter
. Make sure you defineTWITTER_PERIOD
in your deploy settings first. - If you want to wipe out all traces of the project in your server:
fab remove
. Callingfab remove:venv=True
will also delete the virtualenv associated to the project. - Get a list of all available tasks with
fab --list
.
-
How is this different from the default Mezzanine fabfile?
This fabfile is based on the one provided by Mezzanine, but includes several tweaks to make it work in a shared hosting environment.sudo
is never used since Webfaction accounts don't have this privilege.- The Webfaction API is used heavily to fully automate the deployment. This includes creating domain and site records, apps, databases and cronjobs.
- The server-wide Nginx installation is used via a static app, instead of defining custom Nginx config files.
-
How is this different from the Webfaction tutorial on installing Django?
Deploying with Fabric has several advantages over the method provided by Webfaction:- Fully automated. No need to login to the control panel at all. This was the main reason I created this fabfile, to speed up the transition from development to production of my client's projects.
- Uses Gunicorn as the application server instead of Apache.
- Installs everything according to Django best practices, instead of creating a Django App in the control panel which is hard to mantain up to date.
- Uses South and can also automate database and static files back ups.
- Uses supervisor for managing processes, wich is tidier than a cronjob for each Apache instance.
-
Why are you using a symlink to a static/php app instead of one to a static-only app?
Because by doing so you can specify expiration dates for static assets in.htaccess
in your root static directory. This prevents browsers from requesting all your assets every time. Rationale, Question in QA site. You can change the static app fromsymlink54
tosymlink_static_only
if you wish. -
How come I'm seeing three processes running for each Mezzanine project?
Gunicorn uses a master process and a configurable number of worker processes to serve a site. The docs recommend this number should depend on the amount of processor cores, however, in my tests with my 16-core Webfaction server this results in 33 processes, which quickly eats all my RAM. I've hardcoded 2 processes ingunicorn.conf.py
and all seems well. Feel free to modify this number to your needs. -
What exactly is the fabfile doing?
I recommend you take a look into the source to wrap your head around each task, but here is a quick run through them:- If you use
fab prepare_webfaction
it will install and configure all pre-requesites. This includes setting up an account-level pip, virtualenv and supervisor installation. A supervisord conf file is created and memcached is started with an allocation of 50 Mb. A git application named "git" is created in ~/webapps/git. All repos will live in there. - A full project setup with
fab create
will create a new virtualenv, set up a remote git repo and add it to your local git repo as "webfaction", create a site, database, a custom app, and a static app with the Webfaction API, and install all your project dependencies in the venv. It will create a site record in the project db and a superuser if you defineADMIN_PASS
. fab deploy
pushes all your changes to the remote repo, collect's static files and restart's the gunicorn process via supervisorctl.
- If you use
- Tested only with Python 2.7, Django 1.5, and Mezzanine 1.4.
- No support for MySQL.
- No support for Mercurial, SVN.
- You can only deploy to one Webfaction server and only to one domain.