Brewing Up a Python Deployment

When my family bought me a “brew-beer-at-home” kit years ago, they didn’t know what they were getting themselves into.  My first batch tasted a bit like carbonated cereal, but once I got some experience under my belt, the obsession took off. These days, our cars stay in the driveway because our garage is filled with containers of barley, temperature-controlled fermenters, and other beer-making paraphernalia. At some point, my appreciation for all things zymurgy intersected with my love for software (especially when Python is involved) and I started developing DraughtCraft, a website used by “home-brewers” to create and share recipes for beer they’ve crafted at home.

Screen Shot 2013-04-29 at 12.22.51 PM

You’ve undoubtedly got your own passions – though they may not involve transforming gourds into alcoholic beverages. As more and more folks are using Python to publish online, you might find yourself considering it, too. Luckily, running Python applications on DreamHost is pretty simple. Here are some pointers based on my own experience.

This guide is targeted toward shared hosting customers, but it’s also applicable to VPS and dedicated machines, too!

Set Up a Virtualenv

virtualenv is a powerful tool that helps set up isolated Python environments. Always start Python deployments with a fresh virtualenv – it helps avoid headaches in the long run.

$ virtualenv my-python-app

New python executable in my-python-app/bin/python

Installing setuptools…………done.

Installing pip……………done.

To actually use the virtualenv, you’ll need to activate it:

$ source ~/my-python-app/bin/activate

(my-python-app)$

Serve Python via Passenger

There are a few ways to run Python applications on DreamHost shared accounts, including a CGI-to-WSGI adapter (very slow) and a FastCGI-to-WSGI adapter (faster, but still hard to work with).

Most Python web frameworks (like FlaskDjango and Pecan) are compatible with Passenger via WSGI support, which is what we recommend for shared hosting customers. Enabling Passenger support can be accomplished from the Manage Domains section of the DreamHost panel, and more detailed instructions for configuring your Python application can be found in the DreamHost Wiki.

If you’re a VPS or dedicated customer, you’ve got a few more options. I highly recommend serving Python applications via uWSGI or gunicorn behind the nginx HTTP server.

BYOPython (If You Like)

DreamHost’s shared accounts come with Python 2.6 installed. If you’ve got special needs (or just want a more recent version), installing and making your own is pretty easy to accomplish if you can navigate a Linux shell. To get started, enable shell access for your user account in the DreamHost panel. From there, you can download, compile, and install a custom Python into your home directory.

Store Your Data with DreamHost

Most web applications store their critical data in a database. Luckily, DreamHost shared hosting comes with unlimited MySQL5 databases. We also offer pre-optimized MySQL VPS’s, and dedicated machines for power users who want to explore other options (like PostgreSQL or MongoDB) or manage things themselves.

We Speak Hacker

At DreamHost, we embrace geek culture. Experimentation is highly recommended, and we love getting our hands dirty. If you’re looking for a step-by-step guide to Python deployment on DreamHost, take a look at Rick Copeland’s very thorough series on Python WSGI App Deployment.