Tuesday, July 16, 2013

Ubuntu, Django, and Heroku

Getting Ubuntu 12.04 LTS, Django, and Heroku to all play nicely together

Personally, I used VMWare Workstation 8 to host my Ubuntu machine, so I will include some instructions for that as well.

Part 1: Setup

Open: C:\ProgramData\VMware\VMware Workstation\config.ini (Windows 7)


Add the following:
prefvmx.minVmMemPct = "100" 
mainMem.useNamedFile = "FALSE" 
mainMem.partialLazySave = "FALSE" 
mainMem.partialLazyRestore = "FALSE"


Note: This will prevent a long shutdown process for your vm.


  1. Install ubuntu 12.04 LTS
  2. After boot, login (command line), and enter the following:
  3. sudo mv /etc/issue.backup /etc/issue 
    sudo mv /etc/rc.local.backup /etc/rc.local 
    sudo mv /opt/vmware-tools-installer/lightdm.conf /etc/init
    Note: This gets you past the issues with VMWare Tools Easy Install

  4. sudo shutdown -h 0
  5. Make sure settings of vm set cd / floppy to physical
  6. Install vmware tools & reboot (optional)
  7. sudo apt-get update && sudo apt-get dist-upgrade -y
  8. Reboot

Part 2: Software Install


  1. sudo apt-get install build-essential linux-headers-$(uname -r) git vim python-setuptools python-virtualenv
  2. easy_install pip
  3. sudo apt-get install rubygems1.9.1 libopenssl-ruby1.9.1
  4. sudo gem install addressable
  5. sudo gem install heroku
  6. sudo gem install foreman
  7. ssh-keygen -t rsa
  8. mkdir -p ~/projects && cd ~/projects
  9. sudo apt-get install libpq-dev python-dev
  10. sudo apt-get install postgresql-server-dev-all

note: if psycopg2 still doesn't install later on, do sudo apt-get install libconfig8



Part 3: Django, Git, and Heroku


  1. mkdir hellodjango && cd hellodjango
  2. virtualenv venv --distribute
  3. source venv/bin/activate
  4. pip install django-toolbelt
  5. django-admin.py startproject hellodjango
  6. vim Procfile. Add the following:
  7. web: gunicorn hellodjango.wsgi

  8. foreman start
  9. Test that your site is working by browsing to: localhost:5000 (or whatever the port is)
  10. pip freeze > requirements.txt
  11. vim .gitignore. Add the following:
venv
staticfiles
*.py[cod]
# C extensions
*.so
# Packages
*.egg
*.egg-info
dist
build
eggs
parts
bin
var
sdist
develop-eggs
.installed.cfg
lib
lib64
__pycache__
# Installer logs
pip-log.txt
# Unit test / coverage reports
.coverage
.tox
nosetests.xml
# Translations
*.mo
# Mr Developer
.mr.developer.cfg
.project
.pydevproject

  • Edit hellodjango/settings.py.  Add the following to the end:

# Parse database configuration from $DATABASE_URL
import dj_database_url
DATABASES['default'] =  dj_database_url.config()
# Honor the 'X-Forwarded-Proto' header for request.is_secure()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
# Allow all host headers
ALLOWED_HOSTS = ['*']
# Static asset configuration
import os
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

Edit hellodjango/wsgi.py. Replace it with:

from django.core.wsgi import get_wsgi_application 
from dj_static import Cling 
application = Cling(get_wsgi_application())


  1. git init 
  2. git add .
  3. git commit -m "my django app" 
  4. heroku create 
  5. git push heroku master 
  6. Log into heroku and rename your app 
  7. Browse to yourappname.herokuapp.com

No comments:

Post a Comment