cleiver dot me

Deploying a Flask application on Dreamhost with Passenger

Dreamhost does no serve Python or Ruby by default, but you can have them up and running with Passenger. Unfortunately it's not just a matter of checking the checkbox at the panel. Bellow I'll describe the steps I took in order to deploy a simple application I did to learn Flask.

For this tutorial I'll assume you have basic knowledge of things like SSH, Linux etc.

Installing Python 3

It is recommended that you make a custom install of Python so you have a better control of version and packages.

After you connect to your server through SSH, execute the commands bellow, one at a time.

In a nutshell: Inside your home directory, create a temporary folder in order to download Python. After the download, untar, configure, make and make install it. To finish, you just have to include the installation path to your bash_profile. More details and troubleshooting are available at Dreamhost documentation.

Installing and using virtualenv

With virtualenv, you can encapsulate all your packages within a single instance, so you can have multiple applications using different versions of the same packages, as long as they run on separate virtual environments. The following steps assume you have installed Python in the suggested paths.

Inside your home directory, install virtualenv using pip3. Then, enter on your application domain folder and create the virtual environment for that application. Replace example.com with your domain and example_username with your Dreamhost username. The fourth line activates the virtual environment so you can install your packages inside it. As before, more details at the specific documentation.

Deployment

When you use Passenger, by default you have to put your public files on a folder called... public. The final step is to show the webserver which interpreter it'll use when running our application. For this, you have to create a file called passenger_wsgi.py outside the public folder  (aka inside your example.com folder).

In the last line you have to import the app property (where you instantiate Flask) from the file where you coded (in this case, app.py).

After some seconds, your application may be up and running.