DjangoVirtualbox
Running a Django development environment from !VirtualBox
Please note that this recipe is not for beginners as it assumes you are already familiar installing and configuring a !VirtualBox VPS and the relevant software :)
Aim
The goal of this recipe is to run a virtual Django development machine (the guest machine). The main advantages of doing so is the possibility to emulate some workflow encountered while running and working on some Django code on a VPS. Also, this avoids you to clutter your main machine (the host machine) with dev code, Django framework, an apache server, etc...
Your Django project will be available from the host if you point your browser to http://localhost:8000, just like with a default locally installed Django dev setup.
Prerequisites
You need the following already working
- a guest machine with a running GNU/Linux distro
- Django installed on the guest
- Apache installed on the guest
- Guest network configured as NAT
Configuring Apache
On the Guest:
- Apache needs to work on port 8080, add 'Listen 8080' in your /etc/apache2/ports.conf, ie:
NameVirtualHost *:80
Listen 80
Listen 8080
- Also make sure that your !VirtualHost config is configured to handles requests on port 8080, for example in your /etc/apache2/site-enabled/000-default:
<VirtualHost *:80 *:8080>
- Restart Apache and test if http://localhost:8080 works as expected from inside the guest.
On the Host:
- forward the port 8080 of the host to the port 8080 of the guest machine, in a terminal, type:
VBoxManage setextradata "name of the guest" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/Apache/HostPort" 8080
VBoxManage setextradata "name of the guest" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/Apache/GuestPort" 8080
VBoxManage setextradata "name of the guest" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/Apache/Protocol" TCP
- Restart the guest machine (full stop-restart) and test if it works by pointing to http://localhost:8080 and can reach the Apache server running in the guest.
Configuring Django
On the guest:
- Make sure that Django is using the right apache port and location for your 'MEDIA_' stuff, in the settings.py file of your Django app, you should set something such as:
MEDIA_URL = 'http://locahost:8080/mydjangoapp/'
- Start the Django dev server so it listens to 0.0.0.0:8000. We use 0.0.0.0 and not the default 127.0.0.1 to allow incoming connections from the host to the guest:
./manage.py runserver 0.0.0.0:8000
- Test that your Django app is running fine from inside the guest at http://localhost:8000 and uses correctly the media folder at http://locahost:8080/mydjangoapp/
On the host:
- Test that your Django app is running fine at http://localhost:8000 and uses correctly the media folder at http://locahost:8080/mydjangoapp/
Hints
- What's the name of my guest machine(s)?!?
vboxmanage list vms
- to make your life easier you can also forward ssh so you can ssh into your guest and start it headless.
VBoxManage setextradata "name of the guest" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/HostPort" 2222
VBoxManage setextradata "name of the guest" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/GuestPort" 22
VBoxManage setextradata "name of the guest" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/Protocol" TCP
Then, you can restart/start the guest and ssh into it with:
ssh username@localhost:2222
- At anytime you can query what kind of forwarding rules are active:
VBoxManage getextradata "name of the guest" enumerate