Download django-cpserver Now at GitHub
Background
A few months ago, I got sick of trying to deploy Django sites on my cPanel server and got a VPS at Slicehost. Thanks to SuperJared, setting up Apache/mod_python behind an Nginx frontend was a snap.
I started deploying and migrating sites to the new server and kept an eye on my server resources via Munin. I had about 10 sites running on a 1GB Slice, but the Apache processes were hogging all the RAM. Restarting Apache would bring memory usage down to around 500MB, but within a couple of hours, it would be using all my available RAM, with individual proceesses using as much as 120MB.
I started asking questions and trying different options including mod_wsgi, verifying projects weren’t in debug mode, etc. Nothing made a difference.
CherryPy to the Rescue
I came across Loic d’Anterroches’ script to run Django via CherryPy and figured I’d give it a shot. Here are the results:
those drops on the left side are Apache restarts
Memory usage went from 800-900MB to ~300MB. Whoa! Each server was using 20-50MB. I ran some ab tests against it and found it actually held up quite well against Apache/mod_wsgi for low to medium traffic. As expected Apache scaled better to higher traffic. For my needs, CherryPy was more than enough.
Implementation
I reworked the original script to be more Django-like and run as a custom Django management command. In its simplest form, the server can be started via:
Advanced usage details including running on a different port, specifying <span class="caps">SSL</span> certs and spawning as a background process run by a different user are available by calling <tt>./manage.py runcpserver help</tt>.Installation
- Download django_cpserver, unzip and add it to your PYTHONPATH.
- Add django_cpserver to your INSTALLED_APPS.
Additional Resources
- I highly recommend managing your background processes via Monit.
- Multiple CherryPy servers could easily be load balanced with Nginx
- CherryPy WSGIServer