How to host a Flask app on Openshift

Note: This article may no longer be relevant as Red Hat has recently changed the openshift stack with docker and kubernetes.

Openshift free tier is an excellent way to host your python web app for staging or testing, and you can even host a low to medium traffic production site. Openshift provides several options (cartridges) for hosting including python, php, node.js, etc. but python being my favorite language and Flask being a minimalist and flexible framework, this combination is what I usually end up with.

Create an Openshift account

In case you haven’t already, head over to Openshift and sign up for a free tier. You will be able to host at most three apps for each account. Openshift apps are hosted on domain and you’ll have to setup a subdomain first which will be part of your app url. For instance, if I register subdomain, I can create the following apps with that:

Install the necessary tools

You will need the following:

  1. Git: Your app resides in a git repository, so you’ll need git installed on your machine to push changes. App is deployed automatically once you push.
  2. Python and Flask: Obviously, you are going to need them if you are building a Flask app.
  3. Openshift rhc tool: This ruby based tool is optional, only use it if you don’t want to use their online portal for creating apps or you aren’t familiar with ssh. Personally, I didn’t want to install ruby on my machine just for this one purpose.

Pull the remote repo

Once you create a python app, Openshift will provide you a git repository url as follows:

Openshift git url

Now open your command line and pull this starter repo to your local machine:

git clone <YOUR_SOURCE_URL> myFlaskApp

Now, the remote repository will be cloned in the myFlaskApp folder. Browse it to see the scaffolding.

Add your flask app

The scaffolding structure will be as follows:
wsgi/..         => your python source files go here.
wsgi/static..   => your static folders viz css, img, fonts, et al. go here.

If the wsgi/ folder doesn’t exist, you’ll have to create it. Just modify the and add Flask and SQLAlchemy as your app dependencies along with your app name. This tells openshift to make sure that dependency packages are available whenever you push any code changes.

from setuptools import setup

	  author='Prahlad Yeri',

Now, create a text file named application in the wsgi/ folder with the following contents:

import os

virtenv = os.environ['OPENSHIFT_PYTHON_DIR'] + '/virtenv/'
os.environ['PYTHON_EGG_CACHE'] = os.path.join(virtenv, 'lib/python2.7/site-packages')
virtualenv = os.path.join(virtenv, 'bin/')
	execfile(virtualenv, dict(__file__=virtualenv))
except IOError:

from myFlaskApp import app as application       

This is a configuration file that tells openshift where your Flask app script resides. Now create a python file called, this will be your HelloWorld script:

import flask
from flask import Flask
from flask import request

app = Flask(__name__)

def home():
	return "Hello World"

if __name__ == "__main__":

The last part of the code ( is there so that you may test the Flask app by running this script on your local machine before pushing these changes.

Push your changes

All that is left to be done now is committing your changes and pushing them to openshift:

git add .
git commit -m "Initial commit for myFlaskApp"
git push origin master

Voila! You are done

Wasn’t it almost as easy as deploying a php script on your web host? If everything goes right, your Flask app will be hosted on Visit your app link and check it out.

Few important things

Refer to this Openshift tutorial for more details.

Just remove the ssh:// from the beginning and the other things after the domain, so the SSH host url becomes:


[ python  flask  openshift  how-to  ]