Installing Django on EC2 & EBS

This is a quick guide to setting up an Amazon EC2 instance as a Django server. There are many other guides along these lines, but this one includes a “turn-key” script to get everything up and running, as well as an implementation of the “polling” example from the Django tutorial.

Credits

I want to point out this article, which was very helpful to me when I was figuring this stuff out. Thomas handles a lot of the EC2 preliminaries so well that I don’t feel a need to re-hash them. I build on his article by adding:

  • MySQL support (in lieu of PostgreSQL)
  • Django 1.0 support (in lieu of the development version)
  • Apache/mod_python support
  • A sample Django project

Preliminaries

I’m assuming that you have already established an AWS account, are able to launch an EC2 instance, create an EBS volume, and attach that volume to the instance. If you’re unfamiliar with any of these steps, I’d direct you (again) to Thomas’ excellent article, particularly his Steps 1-7 and 11.

The procedure I present below assumes that you have:

  • A running EC2 instance based on the ami-2b5fba42 image
  • A 10GB ext3-formatted EBS volume available
  • Attached the EBS volume to the instances’ /dev/sda4
  • Not yet mounted the EBS volume

Installing Django

To get Django up and running on your EC2/EBS virtual hardware, SSH to the EC2 instance and enter these commands:

wget http://www.mlsite.net/blog/wp-content/uploads/2008/11/go
wget http://www.mlsite.net/blog/wp-content/uploads/2008/11/django.conf
chmod 744 go
./go

That’s it! (Note that the script will prompt you to create a Django admin password the first time you run it against a particular EBS device.)

To test it, point your browser to

http://ec2-aaa-bbb-ccc-ddd.compute-1.amazonaws.com/foo/admin/

where ec2-aaa-bbb-ccc-ddd.compute-1.amazonaws.com is the DNS name you SSH’d to above. The /foo/admin/ URL is the front-end to Django’s admin interface, while /foo/mypolls/ takes you to the sample polling application.

The Script

Since WordPress likes to mangle code, the script is also available as a download.

# Script to configure a base Fedora 8 image (ami-2b5fba42) as a Django server

# Install the world's greatest text editor
yum -y install emacs.i386

# Install subversion
yum -y install subversion.i386

# Retrieve project
wget http://www.mlsite.net/blog/wp-content/uploads/2008/11/djangosite.tar.gz

# Unpack project
tar -xzf djangosite.tar.gz

# Install project
mv src /mnt/www

# Cleanup
rm -f djangosite.tar.gz

# Mount the EBS device
mkdir /mnt/ebs
mount /dev/sda4 /mnt/ebs

# Determine if mysql has already been set up on this device
first_time=0; [ -d /mnt/ebs/mysql ] || first_time=1

# Install MySQL
yum -y install mysql-server.i386

# Change the MySQL data directory
sed -e 's!/var/lib/mysql$!/mnt/ebs/mysql!' -i /etc/my.cnf

# Tables should be InnoDB by default
echo -e "\n[mysqld]\ndefault-storage-engine=INNODB" >> /etc/my.cnf

# Prepare the MySQL database directory, if necessary
[ $first_time -eq 1 ] && mysql_install_db

# Start the MySQL server
service mysqld start

# Create a DB for Django, if necessary
[ $first_time -eq 1 ] && mysql -e "CREATE DATABASE django CHARACTER SET utf8"

# Install python development tools
yum -y install python-devel.i386

# Install the Python-MySQL bridge
yum -y install MySQL-python.i386

# Install Django
wget http://www.djangoproject.com/download/1.0/tarball/
tar -xzf Django-1.0.tar.gz
cd Django-1.0
python setup.py install
cd ..

# Cleanup
rm -f Django-1.0.tar.gz
rm -f -R Django-1.0

# Install Apache and mod_python
yum -y install httpd.i386
yum -y install mod_python.i386

# Configure mod_python
cp django.conf /etc/httpd/conf.d

# Create symlink for Django admin static files
ln -s /usr/lib/python2.5/site-packages/django/contrib/admin/media /var/www/html

# Start Apache
httpd -k start

# Sync Django to DB (always safe)
python /mnt/www/testsite/manage.py syncdb --pythonpath=/mnt/www

Notes and Caveats

The script installs Emacs and Subversion not because they’re needed, but because they’re useful, and you’ll probably want them around.

The script installs everything as root, and the Django site connects to the DB as root, with no password. That’s not so good, but I wanted to keep things as simple as possible.

The sample Django site and application are based on the polling app developed in the Django tutorial, but I made some pedagogical changes to the organization of the project. For instance, the app lives in its own directory, and not under the site, and the Django site is served out of a non-root URL.

The django.conf file instructs Apache to hand off requests for certain URLs to Django, as described here. Note that it turns PythonDebug on; you’d want to change that for production.

Best of luck.

Share and Enjoy:
  • Twitter
  • Facebook
  • Digg
  • Reddit
  • HackerNews
  • del.icio.us
  • Google Bookmarks
  • Slashdot
This entry was posted in Web stuff. Bookmark the permalink.

One Response to Installing Django on EC2 & EBS

  1. Pingback: Aral Balkan - Links for 2009-01-30