This is Part 3 of my tutorial for Flask Webserver Deployment. In the last section we prepared our server for serving a Flask application. We created a small Flask test application and finally started up the Flask development server. In this part we will use a more production ready server called Gunicorn to run the application.

Content of this Tutorial



Gunincorn 'Green Unicorn' is a Python WSGI HTTP Server for Unix. It is ported from Ruby's Unicorn project and it is supposed to be "simply implemented", "light on server resources" and "fairly speedy".

We can easily install Gunicorn via Pythons package installer pip. Make sure to change to the virtual environment we created in the last part of the tutorial and install it:

$ cd /home/apps
$ source /venv/bin/activate
(venv) $ pip install gunicorn

WSGI Script

Gunicorn expects a Python module with a WSGI object in it. So we create an new Python module named containing the following line of code:

from app import app as application

This module only opens up our app module and imports Flasks WSGI app object.

Now we can start our server by calling:

(venv) $ gunicorn wsgi -b127.0.0.1:80
[2016-04-28 23:22:43 +0000] [1476] [INFO] Starting gunicorn 19.4.5
[2016-04-28 23:22:43 +0000] [1476] [INFO] Listening at: (1476)
[2016-04-28 23:22:43 +0000] [1476] [INFO] Using worker: sync
[2016-04-28 23:22:43 +0000] [1479] [INFO] Booting worker with pid: 1479



We got our Gunicorn server working but it is a bit inconvenient. We need to manually activate our virtual environment and start up the Gunicorn server. Actually we want our server to startup automatically. To achieve this we will use a process control system called supervisor. It is a Python2 application so in order to use it we will leave our virtual environment. Make sure you have installed the Python2 packages python and python-pip.

(venv) $ deactivate
$ apt-get install python python-pip
$ pip install supervisor


We need a startup script that does the following things for us:

  1. activate our virtual environment
  2. start the Gunicorn server

We create a new file called in /home/apps with the following lines of code:


# activate virtualenv
cd /home/apps
source venv/bin/activate

# run gunicorn
exec gunicorn wsgi -b0.0.0.0:80

Supervisor comes with a config template which we will copy in our actual supervisord.conf file.

$ echo_supervisord_conf > /etc/supervisord.conf
$ vim /etc/supervisord.conf

We add our flasktest app here by adding the following text at the bottom of /etc/supervisord.conf:

[program: flasktest]

Run it

We start the supervisor daemon by calling:

$ supervisord

This should automatically startup our flasktest app. Now we can use supervisorctl to start and stop our application:

$ supervisorctl start flasktest
flasktest: started

$ supervisorctl status flasktest
flasktest                           RUNNING   pid 1504, uptime 0:00:49

$ supervisorctl stop flasktest
flasktest: stopped


To automatically startup the supervisord daemon and with it our application on system start we need an init script. Luckily somebody had the idea of writing it for us and putting it on GIT. We can find init scripts for different Linux distributions on

In my next post we will put our Gunicorn application server behind a Nginx proxy server for increased savety reasons.