Setting up and deploying Python Flask to Dreamhost

I have a few applications I have written that run on Flask. Every time I would go to deploy a new application I would always seem to forget one of the minor details and end up with the ugly An error occurred importing your

The following are the steps I use to deploy Flask to Dreamhost using Passenger. I haven’t tested it, but this should be roughly the same steps if you want to deploy directly to your domain instead of using a subdomain.

TL;DR; (Quick Setup)

  • add a new subdomain on the dreamhost web panel
  • cd
  • git clone .
  • virtualenv .
  • . bin/activate
  • easy_install flask
  • git submodule init
  • git submodule update
  • Replace in with your folder path.
  • touch tmp/restart.txt
  • DONE!

Part 1 - Setup Domain

  • Go to Manage Domains after logging into the Web Panel
  • Add New Domain / Sub-Domain
    • Domain to host: I like to use subdomains a lot of testing and the different Flask apps I run, so I tend to use Obviously swap out with your own names.
    • (Optional) Check remove WWW
    • If you have more than one user account make sure to pick the correct one.
    • Web directory should fill out automatically for you
    • Passenger (Ruby/Python apps only): Check this box
  • Fully host this domain!
  • SSH into your web server and wait a minute or two for the folder to appear
  • cd into

Part 2 - Install Flask with Passenger

1. Setup a virtualenv

First we need to setup the virtual environment
$ virtualenv .
$ ls
You should now have a directory that looks something like this:
lib public
Next activate the virtual environment by using
$ . bin/activate
The command line should now show up something like this:
( $

2. Install Flask

$ easy_install flask

3. Setup passenger

Create a file named with the following:

NOTE: Make sure to hand type this, as pasting the tabbed lines from the browser can translate to spaces which will evaluate incorrectly.

import sys, os
INTERP = os.path.join(os.environ['HOME'], '', 'bin', 'python')
if sys.executable != INTERP:
    os.execl(INTERP, INTERP, *sys.argv)

from flask import Flask application = Flask(__name__)

@application.route('/') def index(): return 'Hello Passenger'

NOTE: Don’t forget to change to match the setup you are using.

4. Start the server

Add a folder called tmp with a file named restart.txt
$ mkdir tmp
$ touch tmp/restart.txt
To restart the Flask application after making changes you need to update the restart.txt file. This can easily achieved by doing a touch tmp/restart. (For my projects I add a post-receive hook into my git repo to touch the file for me).

The directory should now look something like this:

lib public

5. Test

At this point you should be able to hit the server address from the browser and see ‘Hello passenger’

Part 3 - Deploying a real app

1. Clone sample project from github

$ pwd
$ git clone myappname

1. Adjust

Now that the basic stuff is in place a real application can be setup.

Edit so it now contains

import sys, os
INTERP = os.path.join(os.environ['HOME'], '', 'bin', 'python')
if sys.executable != INTERP:
    os.execl(INTERP, INTERP, *sys.argv)

sys.path.append('myappname') from import app as application

Note: Make sure if you copy this to tab os.excl… since it will copy as spaces and cause a runtime error.

Restart Flask

$ touch tmp/restart.txt

Other Resources:

I am by no means an expert at Python or Flask, I just like to code in it during my free time. Feel free to correct me on my style of code. Please post any questions or comments.

Author Avatar

Matt Carrier

I am a pixel connoisseur who enjoys the finer details of a Bresenham line. When not staring at arrays of smoothly rendered pixels I enjoy playing guitar and consuming coffee.