Brett Hoerner's blog
Setting up a Django environment on Ubuntu
written on Wednesday, October 8, 2008
This is a brief overview of how I organize and serve my Django projects on my Ubuntu server. I think the most important point to take from this is the layout of the domain environments and use of virtualenv.
I'm currently using git inside of my virtual environments to track projects they use (including my own code) and I simply pull and checkout a specific rev to do a "deploy". Obviously if this were a larger or more important site I would recommend using capistrano or something else to handle deployment, but the organization I outline should still be completely valid in that case.
I assume here that you already have the packages from my previous entry installed.
Install Apache and mod_wsgi
# Install Apache with the prefork-mpm
$ sudo apt-get install -y apache2 apache2-mpm-prefork \
apache2-prefork-dev apache2-utils
# Install Python development headers for mod_wsgi
$ sudo apt-get install -y python-dev
# Download and install mod_wsgi from source
$ wget http://modwsgi.googlecode.com/files/mod_wsgi-2.4.tar.gz
$ tar -zxpf mod_wsgi-2.4.tar.gz
$ cd mod_wsgi-2.4/
$ ./configure
$ make
$ sudo make install
$ cd ~
$ rm -rf mod_wsgi-2.4*
Install nginx
I use nginx as a media server and reverse proxy. You can stick to plain Apache, but serving simple static files will hold a full Apache child (which means more memory per request). You can install lighttpd, Perlbal, or your favorite light webserver.
$ sudo apt-get install -y nginx
Install sqlite
I use to sqlite for this site because of memory constraints and no need for the advanced features something like PostgreSQL offers.
$ sudo apt-get install -y libsqlite3-dev sqlite3
Install memcached
Even with very little allocated memory, memcached can really cut down on calls to the database, template rendering, etc. Of course you'll need to make use of it in your app to gain any benefit.
$ sudo apt-get install -y memcached
$ sudo vim /etc/memcached.conf
# Change "-m 64" to the size you want to allocate, e.g. "-m 24"
$ sudo /etc/init.d/memcached restart # Restart so the change takes affect
Install virtualenv
I like to give each of my domains its own virtual python environment. The benefit is that everything is self contained (e.g. you can safely upgrade the version of Django that foo uses when bar isn't ready yet).
$ sudo apt-get install -y python-setuptools python-virtualenv
Setup your web environment
For the domains and apps themselves I start with a simple prefix like /a and create environments such as /a/bretthoerner.com, so that Apache and nginx won't need read/write access to my home directory.
Keep in mind that you'll need to adjust permissions of certain directories inside /a so that www-data (the default user Apache runs as) can write to your sqlite database, etc.
# Create the top-level prefix for domains
$ sudo mkdir /a
$ sudo chown USER:USER /a
$ cd /a
# Create a single virtual environment for one of your domains
$ virtualenv --no-site-packages --unzip-setuptools DOMAIN
$ cd DOMAIN
$ . bin/activate
# Install IPython for that virtual environment
$ easy_install ipython
# And so forth for the Python dependencies that DOMAIN will need
# (you can use easy_install or pyinstall if you like, just be sure
# to activate inside of the virtual environment first!)
Now it's just a matter of configuring nginx (or lighttpd or Perlbal or ...) to serve your media and reverse proxy actual requests back to Apache. I'll try to clean up my configs and post them soon.