Productivity Sync Just another WordPress weblog

June 21, 2009

Django-Postgresql in a ubuntu command line VBox setup.

Filed under: Uncategorized — admin @ 3:54 pm

Last Friday I set up a VBox running the command line (alt-CD install) of Ubuntu 9.04 such that I could do some Django development and simulated deployment in the comfort of my home system.  I’m sure I did some things wrong but it mostly works (although shared Django hosting in the VM is still not completely happy).  Anyway before I forget what I did I’m slapping it in my blog.

  • Install Ubuntu-9.04-alternate-i386.iso using vbox
  • set the vbox to mount and boot from the iso
  • hit f4 of the isolinux boot loader and selected command line install
  • do the install dance.

note: default networking is a NAT through your host network.  Its ok getting the guest to talk to the world but getting the world to talk to the guest isn’t so cool with the NAT.

  • install Django and postgres and stuff
  • apt-get install ssh vim ipython libapache2-mod-wsgi pytho-psycopg2 postgresql
  • apt-get install apache2-doc postgresql-doc-8.3 (if you want too)
  • Set up host and VBox for Bridged adapter on tap1
  • shut down the Vbox guest and hit settings/network/adapter 2 and selected bridged adapter and name it tap1
  • on host : apt-get install bridge-utils uml-utilities
  • create a vboxbridge.sh scrip with the following content in it (to run from root):

root@mgross-desktop:~# cat vboxbridge.sh
#!/bin/sh
# set PATH for the case we are called via sudo or su root

PATH=/sbin:/usr/bin:/bin:/usr/bin:/usr/sbin

# create a tap
tunctl -t tap1 -u mgross
ip link set up dev tap1

# create the bridge
brctl addbr br0
brctl addif br0 tap1

# set the IP address and routing
ip link set up dev br0
ip addr add 10.1.1.1/24 dev br0
ip route add 10.1.1.0/24 dev br0

  • run script.
  • start vbox
  • scp Django-1.0.2-final.tar.gz 10.1.1.2:
  • install Django on guest (do default install)
  • Set up host for sshfs / fusefs access to the vbox gest for easy coding using host tools.
  • apt-get install sshfs
  • sudo mkdir /media/guest
  • sudo chow mgross /media/guest
  • sudo addusr mgross fuse
  • sshfs 10.1.1.2:/home/mgross /media/guest
  • On guest Configure Apache.
  • edit /etc/apache2/sites-available and add one line per shared site from your guest:
WSGIScriptAlias /mysite /home/mgross/sites/apache/mystie.wsgi
  • add the *.wsgi file to get the Django serving…
import os
import sys
os.environ['DJANGO_SETTINGS_MODULE']='mysite.settings'
sys.path.append('/home/mgross/sites/')
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
  • set up postgress on guest
  • sudo -u postgres psql postgres
  • sudo -u postgres createuser -P mgross
  • sudo -u postgres createdb -U mgross mysite
  • edit /etc/postgresql/8.3/main/pg_hba.conf and change the “local” config to:

local all all md5 <– was local all all ident sameuser

  • Fix ups that seem to work fine from the Django test server but fall over when running on the production.
  • set up sim links in /var/www/media so that adim pages work
cd /var/www/media/
ln -s /usr/local/lib/python2.6/dist-packages/django/contrib/admin/media/css
ln -s /usr/local/lib/python2.6/dist-packages/django/contrib/admin/media/cs
ln -s /usr/local/lib/python2.6/dist-packages/django/contrib/admin/media/js
ln -s /usr/local/lib/python2.6/dist-packages/django/contrib/admin/media/img

  • Settings file for Django tutorial mysite test site:

# Django settings for mysite project.

DEBUG = True
TEMPLATE_DEBUG = DEBUG

ADMINS = (
# (‘Your Name’, ‘your_email@domain.com’),
)

MANAGERS = ADMINS

DATABASE_ENGINE = ‘postgresql_psycopg2’           # ‘postgresql_psycopg2’, ‘postgresql’, ‘mysql’, ‘sqlite3’ or ‘oracle’.
DATABASE_NAME = ‘mysite’             # Or path to database file if using sqlite3.
DATABASE_USER = ‘mgross’             # Not used with sqlite3.
DATABASE_PASSWORD = ‘mgross’         # Not used with sqlite3.
DATABASE_HOST = ”             # Set to empty string for localhost. Not used with sqlite3.
DATABASE_PORT = ”             # Set to empty string for default. Not used with sqlite3.

# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# If running in a Windows environment this must be set to the same as your
# system time zone.
TIME_ZONE = ‘America/Los_Angeles’

# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = ‘en-us’

SITE_ID = 1

# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True

# Absolute path to the directory that holds media.
# Example: “/home/media/media.lawrence.com/”
MEDIA_ROOT = ”

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash if there is a path component (optional in other cases).
# Examples: “http://media.lawrence.com”, “http://example.com/media/”
MEDIA_URL = ”

# URL prefix for admin media — CSS, JavaScript and images. Make sure to use a
# trailing slash.
# Examples: “http://foo.com/media/”, “/media/”.
ADMIN_MEDIA_PREFIX = ‘/media/’

# Make this unique, and don’t share it with anybody.
SECRET_KEY = ‘9$s*d_msxvb6dla2126u!wjb=ohkv8uk9%i02)i7%vmcn@@zbz’

# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
‘django.template.loaders.filesystem.load_template_source’,
‘django.template.loaders.app_directories.load_template_source’,
#     ‘django.template.loaders.eggs.load_template_source’,
)

MIDDLEWARE_CLASSES = (
‘django.middleware.common.CommonMiddleware’,
‘django.contrib.sessions.middleware.SessionMiddleware’,
‘django.contrib.auth.middleware.AuthenticationMiddleware’,
)

ROOT_URLCONF = ‘mysite.urls’

TEMPLATE_DIRS = (
# Put strings here, like “/home/html/django_templates” or “C:/www/django/templates”.
# Always use forward slashes, even on Windows.
# Don’t forget to use absolute paths, not relative paths.
‘/home/mgross/sites/mysite/html’,
)

INSTALLED_APPS = (
‘django.contrib.auth’,
‘django.contrib.contenttypes’,
‘django.contrib.sessions’,
‘django.contrib.sites’,
‘django.contrib.admin’,
‘mysite.polls’,
)

  1. tips and tricks (and some refrence URL’s I used.)
    1. cat /var/log/apache2/error.log is your friend
    2. /etc/init.d/apache2 restart will get tiersome
    3. This install isn’t perfect because it still has some warts to work out with the paths.  (reverse fails..)
    4. URLs:
      1. http://www.virtualbox.org/wiki/Advanced_Networking_Linux
      2. http://docs.djangoproject.com/en/dev/howto/deployment/modwsgi/
      3. http://ubuntu.wordpress.com/2005/10/28/how-to-mount-a-remote-ssh-filesystem-using-sshfs/
      4. http://ianlawrence.info/random-stuff/set-up-django-apache-and-postgresql-on-ubuntu-feisty
      5. http://hocuspokus.net/2008/05/install-postgresql-on-ubuntu-804
      6. http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango

Powered by WordPress