Ubuntu virtualisation with Django

Many web applications are hosted on Linux servers, making it difficult for those who are developing their applications on MS Windows (I know, shame on us!).  It can be a bit tricky and cumbersome to maintain two different environments for development and production.

There is a simple alternative, Virtualisation (or Virtualization :-)). Developers can set up a virtual machine running the same OS and software that their production server is using.  This reduces the likelihood of a configuration conflict and simplifies development and testing.

The following steps explain how I setup a Ubuntu JeOS VM to run a Django web application on Apache/mod-python. I am not saying its the best way (I’m not an expert) but it works fine for me. Feel free to offer any suggestions as to how I could improve this setup. (BTW thanks to Gareth for helping me set this up).

Install JeOS on VMWare

For this I created a VMWare Virtual Machine for JeOS (pronounced ‘Juice’) so that I could run it from Windows Vista. JeOS is a server edition of the popular Ubuntu linux distro that is configured to run on virtual appliances. Its a straight forward process and because there is no GUI with JeOS, it runs very nicely indeed. Obviously you can choose whatever OS/Distro you want to but the following instructions use the Debian package manager (apt-get).

I used VMware Workstation ACE to create the Virtual Machine first of all.

  1. Create new Virtual Machine (CTRL+N).
  2. Choose custom configuration, select Linux\Ubuntu as your OS and give the VM a name, e.g. JeOS. Then select the amount of RAM you want, I left it a 512MB. Select number of virtual processors (I chose 2). Choose bridged networking. For JeOS 7.10 (Gutsy Gibbon) be sure to choose IDE rather than SATA for your new virtual disk as SATA doesn’t seem to be supported. I have heard reports that SATA works for the 8.04 (Hardy Heron) beta release. You need to select the amount of disk space to allocate, 8GB should be plenty. I chose to create the file space up front to increase performance but this might take a while.
  3. Download the JeOS ISO.
  4. Edit settings for your VM and set the CDROM to point to your ISO file.
  5. Now time to install JeOS so start the VM. You should be follow the onscreen Ubuntu instructions.
  6. Once you are done you should be able to login. Remember, no GUI so you have to use the shell prompt.

Logging into JeOS

Install Apache

$ sudo apt-get install apache2

If everthing is set up properly, you should be able to connect by entering your VM’s IP address in your favourite web browser on your host OS:

Connecting to Apache from Windows

Install mod-python

$ sudo apt-get install libapache2-mod-python

Install Subversion

We need to use subversion in order to get the latest version of Django’s source code.

$ sudo apt-get install subversion

Install Django

The following steps download the Django source, add it to the python path and add the django_admin.py utilities to your system path.

$ cd

$ mkdir downloads

$ svn co http://code.djangoproject.com/svn/django/trunk/ downloads/django_src

$ sudo ln -s `pwd`/downloads/django_src/django /usr/lib/python2.5/site-packages/django

$ sudo ln -s `pwd`/downloads/django_src/django/bin/django-admin.py /usr/local/bin

Create a new Django project

Creating a new Django project is pretty straight forward. Just use the django_admin.py script to create the framework and create new directories for storing static content (’media’) and Django HTML templates (’templates’).

$ mkdir ~/webapp

$ cd ~/webapp

$ django_admin.py startproject myproject

$ mkdir media templates

Now you should update your ~/webapp/myproject/settings.py file with the following properties:

MEDIA_ROOT = ‘/home/conor/webapp/media/’
MEDIA_URL = ‘http://myproject.com/media’
TEMPLATE_DIRS = ( “/home/conor/webapp/templates” )

Setup Apache Configuration

OK, we now have Apache running and we have created our skeleton Django Application. Now we need to get Apache to serve our static media and to pass dynamic requests to Django.

  1. First of all you need to create a new site file as follows
  2. $ sudo vi /etc/apache2/sites-available/myproject

    Apache site configuration

  3. Now you need to load your new settings into Apache
  4. $ sudo a2ensite myproject

  5. Now you need to remove the default site from Apache
  6. $ sudo a2dissite default

  7. Finally we need to restart Apache to reload the new configuration
  8. $ sudo /etc/init.d/apache2 restart

  9. Now if you refresh the page on your browser, you should see your Django application running.
  10. Connecting to Django Application from Windows

Thats about it! Hopefully I haven’t missed anything. Let me know if you spot any mistakes, you may get a reward (but probably not!).

6 comments ↓

#1 Vitaliy on 04.06.08 at 9:14 pm

Nice.
Have you tryed to use mysql or postgres on this virtual machine? does they work faster/slowly then on Windows natively?
(I often use Windows for development on Django - and one of the bad things - testing django application (python mange.py test) takes more time on windows than on Linux (about 10 times slower))

#2 conormcc on 04.06.08 at 11:27 pm

Not yet but I have used MySQL on previous Ubuntu VMs and it runs fine (at least for development purposes) altough I have a Quad Core PC. I do tend to use sqlite3 DBs for development as they are quick and easy to setup. If you do set up a VM, be sure to let me know how you get on.

#3 Convictus on 05.21.08 at 11:59 pm

Hello,

I had some spelling errors early on that really where a pain to overcome, but now I am pretty certain that I have corrected all of the fat fingers. My django page loads with the bottom error being:

Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined

I have looked around and this article talked about adding a loadmodule directive in your apache config. Any ideas? I am running jeos ubuntu 8.04 but followed the rest of you steps.

#4 Convictus on 05.22.08 at 4:21 pm

dang it more spelling errors.

Django_Setting_module != Django_Settings_Module

#5 conormcc on 05.22.08 at 4:34 pm

LOL. Have you got it working now?

#6 Installing VMware Tools on Ubuntu — /home/conor/ramblings on 06.19.08 at 10:40 am

[...] copy/paste between host and guest OSes.  If you create your own VM using VMWare workstation (see here for instructions) you should install the Tools. You can get the official guide from VMware here but it is missing a [...]

Leave a Comment