search by tags

for the user

adventures into the land of the command line

starting a flask web application on gunicorn

i first started using apache to serve my python (flask) webapps, because it was the only thing i was familiar with. but i wanted to learn something new so i thought i’d learn to use gunicorn.

from their page: “Gunicorn ‘Green Unicorn’ is a Python WSGI HTTP Server for UNIX. It’s a pre-fork worker model ported from Ruby’s Unicorn project.”

so how does one go from using apache to serve their python wsgi webapps, to using gunicorn? like this.

install gunicorn and flask

$ pip install gunicorn
$ pip install flask

set up your flask app, similar to how it would be done in apache. create an application directory and a wsgi file

$ sudo mkdir -p /var/www/myapp
$ sudo vim /var/www/myapp/myapp.wsgi

#paste this inside

import sys
PROJECT_DIR = '/var/www/myapp/'
from index import app as application

‘index’ is the name of the python file that is the entry point for your app.

make sure the app’s directories and especially the log directories have appropriate ownership and permissions, as this as i’ve found when i forgot to do it, stop everything in its tracks. it’s sometimes worth touching or creating all the logfiles first, setting up their permissions, and then starting gunicorn.

create a python script and place it in the same directory as the wsgi file

$ vim /var/www/myapp/

# paste in this for example

from flask import Flask, render_template

app = Flask(__name__)

def index():
    return render_template('index.html')

def upload():
    return render_template('upload.html')

if __name__ == '__main__':

make sure you have some templates

$ sudo mkdir -p /var/www/myapp/templates
$ sudo echo '200' >> /var/www/myapp/templates/index.html
$ sudo echo '200' >> /var/www/myapp/templates/upload.html

run gunicorn

$ gunicorn index:app

you can bind to a specific ip and port, it runs on port 5000 by default

$ gunicorn -b index:app

you can view the above in a browser at or or

depending on what you have in your /etc/hosts file

you can specify the number of worker threads, default is 1

$ gunicorn -w 10 -b index:app

you can run the worker threads in different modes, the default mode is synchronous, but there are also asynchronous, tornado (for the tornado framework) and asyncIO modes.

if you want to use asynchronous mode, you’ll need to install either greenlet or eventlet

$ pip install eventlet
$ gunicorn -w 10 -k eventlet -b index:app

you’ll notice that the gunicorn process runs in the foreground so as soon as you ctrl+c or log out of the terminal the gunicorn process will stop and so will your app. i’ll talk about how to run it as a daemon using a utility called supervisor in my next post.

but as you can see its pretty straight forward to getting started