<img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=338168267432629&amp;ev=PageView&amp;noscript=1">
DevOps

Creating a Virtual Development Server for Django projects: Part 2

This is the second part of our tutorial, check out Part 1 if you haven’t already! If you, the reader, have some server admin-fu and have come to realize that you already know what you are doing, then feel free to either skip this part or consider the end of …


This is the second part of our tutorial, check out Part 1 if you haven’t already!

If you, the reader, have some server admin-fu and have come to realize that you already know what you are doing, then feel free to either skip this part or consider the end of the post in the previous section. I, the author of said post, do not have any administration-fu and might be saying something professionally wrong here. Feel free to comment on my shortcomings.

Scripts and Arrangement of Files

Your home directory will be empty; I that recommend you create the “bin”, “sites”, and “envs” directories here.

  • ~/bin/
    • This folder will be automatically appended to the PATH by Ubuntu. It is so that you can call executable scripts as though you just installed them by apt-get install.
    • Of course, this will not take effect until your next restart with the VM
  • ~/sites/
    • This is where we will put all our shared folders
    • We just have to make sure that when mounting shared folders, the permissions of the mounted shared folder and its files are given to the current user
  • ~/envs/
    • This is where we will put all our per-project virtualenvs.
    • We have to put the virtualenvs inside the guest machine (obviously, right?) because that’s where we’ll be running Django’s development server. Sadly, we cannot put the env inside the project folder. Some errors about permissions happens when we create the virtualenv.

My Scripts and Stuff

Apparently, I am also the lazy type to do the same things repeatedly so I created 3 basic bash scripts. 2 independent scripts in case I need to call them independently and 1 script to call both (which is only used on startup). I am giving myself the liberty of posting here the scripts, in case you might want to copy them.

The startup script (filename: init-server):

#!/bin/bash
scriptDir=`dirname $0`
$scriptDir/init-network
 $scriptDir/init-vboxsfs

The script for initializing the network interfaces (filename: Inuit-network):

#!/bin/bash
ifconfig eth0 192.168.56.2 netmask 255.255.255.0 up
 dhclient eth1

The script for remounting the shared folders (filename: init-vboxsfs):

#!/bin/bash
declare -A vboxsfs
userId=`id -u vdev`
 sitesDir=’/home/vdev/sites’
 vboxsfs=(\
 # Format:
 # [vboxsf-name]=[mountdir-name]\
 )
for vboxsf in ${!vboxsfs[@]}; do
 mountDir=${vboxsfs[$vboxsf]}
umount $vboxsf
if [ ! -d “$sitesDir/$mountDir” ]; then
 mkdir “$sitesDir/$mountDir”
 fi
mount -t vboxsf -o uid=$userId,gid=$userId $vboxsf “$sitesDir/$mountDir”
echo “Mounted ‘$vboxsf’ vboxsf to $sitesDir/$mountDir”
 done

Workflow

I will be skipping the part where we mount the shared folders and create a virtualenv. Also, I would also like to assume that Django is already installed and your guest host-only network IP is 192.168.56.2.

  • Edit your host machine’s /etc/hosts file. Add the ip 192.168.56.2 entry with a custom domain name of your own. This can also be used for logging into your guest machine via SSH. There can be as many 192.168.56.2 entries as you want. For now, let’s assume that your host entry is:
    192.168.56.2 default.test-server.com
  • SSH into your guest machine
  • activate your project’s virtualenv
  • go to your project’s mounted directory and run: ./manage.py runserver 192.168.56.2:8000
  • On your host machine, open your browser and go to: http://default.test-server.com:8000
  • Voila!
  • This ends the post. I hope that you, the readers, found this useful.

Similar posts

Get notified about the latest in Tech

Be the first to know about new tech from the experts at Bixly!