Skip to content

Instantly share code, notes, and snippets.

@atheiman
Last active September 19, 2018 15:02
Show Gist options
  • Save atheiman/e432f6c399adf780f6f3 to your computer and use it in GitHub Desktop.
Save atheiman/e432f6c399adf780f6f3 to your computer and use it in GitHub Desktop.
Simplified Django deploy using Apache HTTP Server.

Django Apache Deploy

Simplified Django deploy using Apache HTTP Server. We will use Vagrant and VirtualBox to test the deploy on a virtual machine.

Prerequisites

Build Your VM and Deploy a Django Site

$ git clone git@gist.github.com:/e432f6c399adf780f6f3.git ~/django-deploy
$ cd ~/django-deploy
$ vagrant up

Test out the site.

Explanation

Django Source

The source for this very minimal Django site comes from this Gist. The site is essentially the smallest, production-ready Django site (that actually does something) possible. A more detailed explanation of the code is in the README for the Gist, this deployment exercise assumes that you already have a working knowledge of Django. All that really needs to be understood for deployment is that the web server will need access to the WSGI application object. Django exposes a WSGI application object easily using django.core.wsgi.get_wsgi_application.

Vagrantfile

The Vagrantfile isn't very complex if you're at all familiar with Vagrant or just the concept of provisioning. We start with the base box (hashicorp/precise64), forward port 80 for HTTP requests, and designate a file for provisioning (bootstrap.sh).

Shell Provisioning

This shell script is run by Vagrant when the VM is created. Essentially the goals of the script are as follows:

  • Install needed software
  • Setup a Virtual Environment
  • Build the Django site from the source Gist
  • Configure an Apache VirtualHost to serve the Django site
#!/usr/bin/env bash
# Global vars
PROJ_TEMPLATE_PATH=/tmp/project_name
DJANGO_PROJECT_DIR=/opt/django_project
VIRTUAL_ENV=django_env
DJANGO_PROJECT=my_site
WSGI_RELATIVE_PATH=my_site.py
APACHE_VHOST=django_host
APACHE_SITES_AVAILABLE_DIR=/etc/apache2/sites-available
# clean up from previous provisions
rm -rf $PROJ_TEMPLATE_PATH $DJANGO_PROJECT_DIR
mkdir $PROJ_TEMPLATE_PATH $DJANGO_PROJECT_DIR
# Install OS packages
apt-get update
apt-get install -y apache2 python-pip libapache2-mod-wsgi curl
# Setup a virtualenv
pip install virtualenv
virtualenv $DJANGO_PROJECT_DIR/$VIRTUAL_ENV
$DJANGO_PROJECT_DIR/$VIRTUAL_ENV/bin/pip install django
# Create Django project from template
curl https://gist.githubusercontent.com/atheiman/e7b8bec6f67689d61c1c/raw/project_name.py > $PROJ_TEMPLATE_PATH/project_name.py
cd $DJANGO_PROJECT_DIR
$DJANGO_PROJECT_DIR/$VIRTUAL_ENV/bin/django-admin.py startproject $DJANGO_PROJECT --template=$PROJ_TEMPLATE_PATH
# Run the Django dev server
# $DJANGO_PROJECT_DIR/$VIRTUAL_ENV/bin/python $DJANGO_PROJECT/$WSGI_RELATIVE_PATH runserver 0.0.0.0:8080
# Apache HTTP Server config
a2dissite default
# Create a Django virtual host
touch $APACHE_SITES_AVAILABLE_DIR/$APACHE_VHOST
cat <<EOT >> $APACHE_SITES_AVAILABLE_DIR/$APACHE_VHOST
<VirtualHost *:80>
WSGIDaemonProcess $DJANGO_PROJECT python-path=$DJANGO_PROJECT_DIR/$DJANGO_PROJECT:$DJANGO_PROJECT_DIR/$VIRTUAL_ENV/lib/python2.7/site-packages
WSGIProcessGroup $DJANGO_PROJECT
WSGIScriptAlias / $DJANGO_PROJECT_DIR/$DJANGO_PROJECT/$WSGI_RELATIVE_PATH
ServerAdmin atheimanksu@gmail.com
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
EOT
# Enable your Django virtual host
a2ensite $APACHE_VHOST
service apache2 restart
Simplified Django deploy using Apache HTTP Server.
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "hashicorp/precise64"
config.vm.network "forwarded_port", guest: 80, host: 8000
config.vm.provision :shell, path: "bootstrap.sh"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment