Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Running Django Apps on Heroku

Running Django Apps on Heroku

An introduction to running full Django sites on the Heroku platform.

Randall Degges

October 23, 2011
Tweet

More Decks by Randall Degges

Other Decks in Programming

Transcript

  1. Hosting Yourself vs PaaS Pros • Cheaper. • Teaches your

    more. • More flexible. Cons • Requires time. • Requires knowledge. • Requires maintenance. • Requires constant revisions (architecture). • Less time for coding. Pros • Simple. • Scalable. • Managed. • More time for coding. Cons • Costly. • You're betting on the company.
  2. Getting Started (on Ubuntu) $ sudo aptitude install python-software-properties curl

    $ sudo apt-add-repository 'deb http://toolbelt.herokuapp.com/ubuntu ./' $ curl http://toolbelt.herokuapp.com/apt/release.key | sudo apt-key add - $ sudo aptitude -y update $ sudo aptitude -y install libpq-dev python-pip heroku-toolbelt ruby git $ sudo pip install -U virtualenv $ heroku login
  3. Hello, #pyladies $ mkdir helloladies && cd helloladies $ git

    init $ echo "Django==1.3" > requirements.txt $ echo "psycopg2==2.4.2" >> requirements.txt $ echo "env" > .gitignore $ virtualenv --no-site-packages env $ . env/bin/activate $ pip install -r requirements.txt $ django-admin.py startproject helloladies $ git add --all $ git commit -m "woot"
  4. Useful Information # View your processes: $ heroku ps #

    View your logs: $ heroku logs # Run custom stuff: $ heroku run python manage.py syncdb $ heroku run python manage.py shell # Deploy new code: $ git push heroku master
  5. Heroku + gunicorn • Real programmers use real web servers.

    • Let's use gunicorn! $ echo "gunicorn==0.13.4" >> requirements.txt # settings.py - INSTALLED_APPS 'gunicorn', $ echo 'web: python helloladies/manage.py run_gunicorn -b "0.0.0.0:$PORT" -w 3' > Procfile $ git add --all; git commit -m "use gunicorn"; git push heroku master
  6. Queue Processing with Celery $ pip install django-celery django-kombu $

    pip freeze > requirements.txt # settings.py - INSTALLED_APPS 'djcelery', 'djkombu', # settings.py import djcelery djcelery.setup_loader() BROKER_BACKEND = 'djkombu.transport.DatabaseTransport' CELERY_RESULT_DBURI = DATABASES['default'] $ echo 'worker: python helloladies/manage.py celeryd -E -B --loglevel=INFO' >> Procfile $ git add --all; git commit -m 'use celery'; git push heroku master $ heroku run python helloladies/manage.py syncdb $ heroku scale worker=1 # Verify it's working: $ heroku ps