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

How to Make Stuff

How to Make Stuff

Or: Full-Stack Web Production with Python, Django, Compass, and Amazon Web Services.

Avatar for Alex Cabrera

Alex Cabrera

October 26, 2011

Other Decks in Programming

Transcript

  1. HOW TO MAKE STUFF. - or - Full-Stack Web Production

    with Python, Django, Compass, and Amazon Web Services
  2. HATE! HATE! HATE! This is a highly opinionated way of

    building web applications, and by no means the only way to do so. There are a million other ways of doing this; the only difference is this one doesn’t suck.
  3. SPECIALISTS DON’T MAKE ANYTHING • If you can’t build a

    full application, you are a specialist. Specialists can’t build web apps on their own. • If you can’t deploy your app, it doesn’t matter what you build. We’re all ops people now. • If you can’t organize or collaborate, you can’t keep up with the competition.
  4. THE PROBLEM IS INTIMIDATING • Front End: HTML5, CSS3, JavaScript.

    LessCSS, SASS, HAML, jQuery, Dojo, MooTools, Modernizr, Backbone, CoffeeScript... • Languages: PHP, Python, Ruby, CLR, Java, Scala... • Frameworks: Sinatra, Cake, Rails, .NET, Django... • Databases: MySQL, PostgreSQL, MongoDB, SQL Server, Oracle... • Servers: Apache, lighttpd, nginx, IIS [really?] , WSGI, mod_php, Rack, Mongrel, Tomcat...
  5. WE NEED SOMETHING THAT JUST WORKS • Complete: Everything required

    to build and launch a modern web application • Flexible: Swap out parts as needed, customize to liking • Organized: Follow a methodology that lets you bring in others because your project is logically structured • Scalable: Ensure people can use your app in the event it doesn’t suck ™
  6. The slide where we get people grumbling about how I

    didn’t pick their components and thus have no idea what I’m talking about. • HTML: Open, works everywhere, what else are you going to use? Flash? • jQuery: Heavily tested, works pretty much everywhere, lots of support. • SASS: Writing and maintaining CSS sucks. SASS allows for indented syntax, functions, math, mixings, and compression. • Compass: Expansive library for SASS, use CSS3 now. • Python: Like Ruby, for adults. • Django: Smart template system, great ecosystem, scales well, highly decoupled. • South: Dead-simple migrations for the default ORM • PostgreSQL: Like MySQL, but good.
  7. DEVELOP LIKE A BOSS. • Use a package management system.

    On a Mac, this means Homebrew. • Mirrors your production environment • Allows you to work on multiple projects simultaneously • Easy to tear down and rebuild (you’ll be doing this a lot)
  8. PRODUCTION HOSTING, LOCALLY Use a proxy server to handle static

    media locally. Much faster. > brew install nginx > wget https://raw.github.com/gist/1309395/78399aa0051752e8475054169230009c46767b7e/ gistfile1.txt /usr/local/etc/nginx/sites-available > ln -s /usr/local/etc/nginx/sites-available/active.lo /usr/local/etc/nginx/sites-enabled/ active.lo > wget https://raw.github.com/gist/1309399/c044a4aa0bec8772e4432abd754ebfd48d34efb1/link- nginx-django-static.sh /usr/local/bin/link-nginx-django-static > chmod +x /usr/local/bin/link-nginx-django-static
  9. DON’T REINVENT THE WHEEL (I already did) Clone this Django

    project skeleton: https://github.com/alexcabrera/django-project-skeleton • Python packaging best practices • Includes requirements file for use with pip • Configuration templates for production hosting on AWS • Logical separations of components for collaborative development
  10. IT WORKS! > git clone [email protected]:alexcabrera/django-project-skeleton.git exampleproj > cd exampleproj

    > createdb exampleproj > psql exampleproj -c “create user exampleproj with password ‘exampleproj’;” > psql exampleproj -c “grant all privileges on database exampleproj to exampleproj;” > mkvirtualenv exampleproj > pip install -r requirements.txt > cd exampleproj > python manage.py syncdb > link-nginx-django-static > python manage.py runserver
  11. OCD PROJECT STRUCTURE • SASS files in ui-source/sass Open terminal

    buffer, run `compass watch` in that directory. • HTML templates in project/templates Reference static file directory using {{ STATIC_URL }} • Deployment configuration in project/deploy Configuration template for gunicorn included • Environment settings in project/conf Sensible (and insecure) defaults for development and production (on AWS) included
  12. I FOR ONE WELCOME THE NEW AMAZONIAN OVERLORDS • Sign

    up for a AWS account Free to play with, cheap if you grow. • Set up an EC2 instance You could do worse than an EBS-backed Ubuntu Server AMI • Set up a S3 bucket Reflect this change in project/conf/production.py • SSH in, Configure Server apt-get install nginx, postgresql-server, supervisord
  13. THE ONE TRUE CONFIGURATION • Create a user for deployed

    applications Keep all apps in /home/deploy • Use SSH keys so you don’t need passwords • Modify configuration boilerplates, symlink Templates for nginx and supervisord are in project/deploy • Create project/settings_local.py, make git ignore it from conf.production import *
  14. WHAT NOW? • Create a staging environment to run your

    deploy branch • Write scripts to automate deployment using Fabric, Puppet, or both • Swap out any pieces that don’t work for you: use another ORM, a different app server (uwsgi), a different template system. • Add people to your project on GitHub. Collaborate. • Scale by adding new instances, moving database server
  15. THANKS. See it in action at http://droptype.com Follow us on

    Twitter @droptype Follow me on Twitter @_alexcabrera Special Thanks to Joonas Bergius (@prometheus) for figuring out a good chunk of this stuff.