Skip to content

Instantly share code, notes, and snippets.

@mortenege
Created March 4, 2018 07:01
Show Gist options
  • Save mortenege/0d33fe3ba88b124f27060a921aa3eba8 to your computer and use it in GitHub Desktop.
Save mortenege/0d33fe3ba88b124f27060a921aa3eba8 to your computer and use it in GitHub Desktop.
Django backend with Vagrant

Django backend with Vagrant

This post will cover how to configure our vagrant guest machine [1, 2, 3, 4] to run a django server instead of our previous prop backends. We will continue using Vagrant and introduce virtualenv for our Django project.

Step 1

We create a virtual environment and install django. We start off by installing python virtualenv and set up an environment in our vagrantfolder so we will have it ready on boot.

$ pip install virtualenv
~/vagrant$ mkdir django
~/vagrant$ cd django
~/vagrant/django$ virtualenv venv --always-copy
~/vagrant/django$ source venv/bin/activate
(venv)~/vagrant/django$

Now we can install django into this virtual environment.

(venv)$ pip install django

Step 2

Now we can either import a django project to this folder or create one from scratch. For the purpose of transparancy we will create one from scratch. A good tutorial on django can be found here. Navigate to django-folder in the vagrantfolder and start a new project.

(venv)$ cd /vagrant/django
(venv)$ django-admin startproject mysite
(venv)& python mysite/manage.py runserver 127.0.0.1:8080

NOTE that we are still building on top of our previous guest machine, in which case SimpleHTTPServer and simple-websocket-server are still running. Stop these services by running supervisorctl stop all. NOTE also that if we cannot manage to bind Django to any ports, it is possible we need to run as root or sudo.

Navigate again to 127.0.0.1:4567 to see that our django setup is working.

Step 3

Configure our supervisord.conf to include our new Django setup. (Remember to delete any other programs that are connecting to the same port)

[program:DjangoMysite]
environment=PATH="/vagrant/django/venv/bin"
command=python mysite/manage.py runserver 127.0.0.1:8080
directory=/vagrant/django
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=/tmp/djangomysite.out.log

Conclusion

We have successfully configured a Django backend for our Vagrant guest environment. It is still running behind Apache Reverse Proxy and is managed by Supervisor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment