repoze.bfg + Gunicorn + zc.buildout.
How to run a repoze.bfg app with Gunicorn WSGI-Server and configure all using zc.buildout
So why?
- repoze.bfg rocks for building webapps.
- zc.buildout rocks for configuring controlled Python environments.
- gunicorn rocks as wsgi-server, because its simple, pure python and serves hundreds to thousands of concurrent requests to fast clients and (slow) backends.
If you try this you need an webapp. I consider you have one, othwerwise take one of the example apps from the repoze.bfg tutorials. For this example I consider you have your app as an egg in a subversion under http://foo.org/svn/myapp/trunk
Create some directory and cd into it.
Copy your webapps paste.ini file to the new directory.
Copy a bootstrap.py to the new diretory. I prefer the one from distribute.
Then create a buildout.cfg with following contents:
[buildout]
parts = gunicorn gunicornctl
extensions = mr.developer
sources = sources
always-checkout = force
auto-checkout = *
[sources]
myapp = svn http://foo.org/svn/myapp/trunk
[var]
recipe = plone.recipe.command
dir = ${buildout:directory}/var
command = mkdir ${:dir}
[gunicorn]
recipe = zc.recipe.egg:scripts
dependent-scripts = true
eggs =
myapp
eventlet
gunicorn
[gunicornctl]
recipe = collective.recipe.template
mode = 755
input = ${buildout:directory}/gunicornctl.in
output = ${buildout:directory}/bin/gunicornctl
pidfile = ${var:dir}/instance.pid
logfile = ${var:dir}/instance.log
pasteini = ${buildout:directory}/paste.ini
workers = 2
create another file gunicornctl.in containing:
#!/bin/sh
# Specify path variable
PATH=/sbin:/usr/sbin:/bin:/usr/bin
# Kill me on all errors
set -e
case "$1" in
start)
${buildout:directory}/bin/gunicorn_paster -D \
-w ${:workers} \
-p ${:pidfile} \
--log-file=${:logfile} \
${:pasteini}
;;
stop)
kill `cat ${:pidfile}`
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
;;
esac
Now run buildout with
/path/to/python2.6 bootstrap.py -d
Next run build with
./bin/buildout
Then start your app with gunicorn
./bin/gunicornctl start
Watch the logifle using
tail -f var/gunicorn.log
Take you webbrowser and connect to the address given in your paste.ini. I.e. http://localhost:8080.
And stop it later with
./bin/gunicornctl stop
Do not expose Gunicorn to the web. You clients are somewhere in the internet and slow. Let NGINX be Gunicorns fast client. Read more about deployment and how to configure it.
