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

Django + Heroku

Jack Shedd
February 07, 2013

Django + Heroku

A brief talk on why Heroku is a better choice for my agency than whatever weird EC2, Linode, Rackspace-Server-thing.

Jack Shedd

February 07, 2013
Tweet

More Decks by Jack Shedd

Other Decks in Programming

Transcript

  1. THIS IS MESS FOUNDED IN 2007 4 DESIGNERS + 4

    DEVELOPERS WEB, MOBILE, SOCIAL, DESKTOP, IOS, ANDROID 1.25 ADMINS (ME + DAN) 64.2% DJANGO 4 SITES A MONTH WAY MORE IN “MAINTENANCE” http://www.thisismess.com
  2. SERVERS SHOULD BE SAME(ish) PREDICTABLE TO SCALE WITH CHEAP TO

    DEVELOP WITH EASY(ish) TO ADMINISTER RELIABLE(ish) FLEXIBLE(ish) BILLABLE!
  3. DEPLOYMENT MUST BE THE SAME ACROSS AS MANY PROJECTS AS

    POSSIBLE DOCUMENTED N00B-PROOF’D FORGIVING
  4. 1996 CALLED AND IT HATES YOU …BUT IT’S CHEAP …AND

    PREDICTABLE! …AND FLEXIBLE! …AND ABSOLUTELY NOTHING ELSE
  5. 2002 ISN’T MUCH HAPPIER …BUT IT’S CHEAP …AND PREDICTABLE! …AND

    FLEXIBLE! …AND (more) RELIABLE! …AND ABSOLUTELY NOTHING ELSE
  6. (n) VPS (es?) AT RACKSPACE USING FABRIC AND RELEASES AND

    SFTP AND NEW RELIC AND SENTRY AND GIT AND PUPPETMASTER
  7. IF YOUR DREAM IS… PAINFUL TO SETUP …AND UPDATE …AND

    SECURE …AND SCALE …AND TRAIN ON …AND CHANGE …AND BILL …AND TRANSFER
  8. HEROKU (and S3 and EC2 and RACKSPACE) 100% STANDARD (unless

    it’s non-standard) BILLABLE N00B PROOF RELIABLE(ish) FLEXIBLE(ish) DOCUMENTED ADMINISTRATABLE (because you don’t) CHEAPER THAN CHEAP. FREE!
  9. IT’S KINDA, WELL…DUMB IT’S JUST EC2 …AND WITHOUT IT …SO

    IT GOES DOWN WITH EC2 …AND YOU CAN’T DO NATIVE WEBSOCKETS …AND IT GETS PRICEY …AND MYSQL SUCKS ON IT (so?) …AND UWSGI HAS PROBLEMS …AND THERE’S NO PERMANENT STORAGE …AND IT USES MICROS ON EC2 …AND LOGGING IS…LESS THAN AWESOME …AND PSQL IS KINDA SLOW …AND THEIR SUPPORT SUCKS …AND YOU DEPLOY WITH GIT turns out they use linux containers or something.
  10. 1. GET GOING DOWNLOAD THE TOOLBELT INSTALL BREW https://toolbelt.heroku.com http://mxcl.github.com/homebrew/

    INSTALL GIT brew install git; CREATE AN ACCOUNT https://heroku.com INSTALL VIRTUALENV sudo easy_install virtualenv; INSTALL PIP sudo easy_install pip; INSTALL PSQL http://postgresapp.com GENERATE SSH IDENTITY http://google.com ADD PUBLIC KEY TO HEROKU https://dashboard.heroku.com/account
  11. 2. MAKE AN APP DO THE VENV INSTALL THINGS INTO

    THE VENV START A PROJECT mkdir heroku-app; cd heroku-app; git init; virtualenv .env --distribute; source .env/bin/activate; pip install Django psycopg2 dj-database-url; pip install boto django-storages South; DJANGO! DJANGO! django-admin.py startproject herokuapp .
  12. 3. PREPARE SPEC REQS SETUP PSQL SETTINGS.PY import dj_database_url DATABASES['default']

    = dj_database_url.config(default='postgres://foo@localhost/heroku') RUNSERVER python manage.py runserver; pip freeze > requirements.txt createdb -h localhost heroku; GUNICORN (they refuse to support UWSGI correctly) pip install gunicorn; “web: gunicorn herokuapp.wsgi” >> Profile .gitignore *.pyc .env .DS_Store local_settings.py
  13. 4. GIT IT? git init; git add .; git commit

    -a -m “I love squirrels.”; INIT & COMMIT
  14. 5. HEROKUEN! heroku create; heroku addons:add heroku-postgresql:dev git push heroku

    master; heroku run python manage.py syncdb; heroku open; APPIFY
  15. IT’S FREE TO DEVELOP WITH 100% FREE DURING DEV/STAGING (PRODUCTION!)

    INSTANCES SPIN DOWN! CRON A CURL http://google.com
  16. UWSGI IS WAY BETTER THAN GUNICORN USE PYTHON 2.7.2 pip

    install uwsgi; pip freeze > requirements.txt; “python-2.7.2” >> runtime.txt “web: uwsgi --http-socket :$PORT --wsgi-file wsgi.py” >> Profile
  17. STORING FILES? USE FILE STORAGES. S3 IS BUILT FOR THIS

    pip install boto django-storages; pip freeze > requirements.txt; CUSTOM STORAGES (utils.py) from storages.backends.s3boto import S3BotoStorage class StaticRootS3BotoStorage(S3BotoStorage): def __init__(self, *args, **kwargs): super(StaticRootS3BotoStorage, self).__init__(*args, **kwargs) self.location = 'static' class MediaRootS3BotoStorage(S3BotoStorage): def __init__(self, *args, **kwargs): super(MediaRootS3BotoStorage, self).__init__(*args, **kwargs) self.location = 'uploads' #### ASSETS URLS #### BASE_URL = '//bucket-name.s3.amazonaws.com/' STATIC_URL = '%sstatic/' % BASE_URL MEDIA_URL = '%suploads/' % BASE_URL #### Amazon AWS / Storage AWS_ACCESS_KEY_ID = "AKXXXXX" AWS_SECRET_ACCESS_KEY = "HZZXXX/XXXX" AWS_STORAGE_BUCKET_NAME = 'bucket-name' #### File Storage DEFAULT_FILE_STORAGE = 'herokuapp.utils.MediaRootS3BotoStorage' STATICFILES_STORAGE = 'herokuapp.StaticRootS3BotoStorage' SETTINGS.PY
  18. MICROS ARE FINE IF YOU NEED MORE PERFORMANCE heroku ps:scale

    web=2; WHATEVER WEIRD CONTAINER THING THEY USE
  19. http://getsentry.com/ YOU’RE A LOG WHY AREN’T YOU USING SENTRY? INSTALL

    RAVEN pip install raven; pip freeze > requirements.txt; SETTINGS.PY # Set your DSN value SENTRY_DSN = 'https://XXXXX:[email protected]/1111' # Add raven to the list of installed apps INSTALLED_APPS = INSTALLED_APPS + ( # ... 'raven.contrib.django.raven_compat', )
  20. PSQL? EC2! MAKE A HIGH-MEMORY INSTANCE ON EC2 http://google.com SET

    DATABASE_URL ENV heroku config:set DATABASE_URL=postgres://xxx.aws.com:xxx/xxxx THIS WORKS FOR ANYTHING HEROKU’S PSQL IS ACTUALLY PRETTY GREAT
  21. EMBRACE GIT DEPLOYS USE PROCFILE FOR STUFF “web: python manage.py

    collectstatic --noinput; uwsgi --http- socket :$PORT --wsgi-file wsgi.py” >> Profile