Apache meet Python Flask

by | Mar 31, 2016

This guide covers installing and configuring Flask to run on Apache on Ubuntu. Why would you want to do this? In a nutshell, you want to run Python code in a web environment.


  • AWS EC2 running Ubuntu Server 14


Installing the software

SSH onto your EC2 instance and run the following commands.
This will install and enable the following components to get everything:

sudo su
apt-get update
apt-get install apache2 python-pip libapache2-mod-wsgi
pip install Flask

Setting up a test program

Create a directory called Flask under /var/www

mkdir /var/www/flask

Create the file /var/www/flask/webtool.py with the following contents

from flask import Flask
app = Flask(__name__)
def hello():
 return "Hello world!"
if __name__ == "__main__":

Next create the file /var/www/flask/webtool.wsgi with following contents

import sys
from webtool import app as application

Configuring Apache

Edit the following /etc/apache2/sites-available/000-default.conf and insert the following content between the virtual host tags. This will instruct Apache to start a daemon that will interact with Python when URL requests are sent to http://<IP Address of Server>/flask

WSGIDaemonProcess webtool user=ubuntu group=ubuntu threads=5 home=/var/www/flask/
 WSGIScriptAlias /flask /var/www/flask/webtool.wsgi
 <directory /var/www/flask>
 WSGIProcessGroup webtool
 WSGIApplicationGroup %{GLOBAL}
 WSGIScriptReloading On
 Order deny,allow
 Allow from all

Restart Apache

service apache2 restart


Using your web browser go to the following URL  http://<IP Address of Server>/flask
All going well you should see the following web page and our faithful friend “Hello world!”


Source: OptimalBI


The first place to check will be the apache log files.  By default, they can be found here /var/log/apache2/ check the error.log file for any useful information.
The second suggestion I would have is to make sure that the user and group used in the Apache configuration is a valid user and group. if not change them to valid values

WSGIDaemonProcess webtool user=ubuntu group=ubuntu threads=5 home=/var/www/flask/

All the code, all the fun – Ben
Ben writes blogs about the technical side of BI the code, all the code and not much other than the code. 
You can read all of Ben’s blog here.
Don’t forget, we can train your team in the art of agile business intelligence at any time!

  1. mthaneq

    Does not work. Keep getting apache html file and not ‘Hello world’

  2. Ben Lee

    Hi mthaneq,
    That sounds odd, I would have thought you would have gotten a 404 error if your configuration was wrong.
    Which URL are you going to after the setup? Is there anything useful in the apache logs?

  3. gyu

    hello same question as ben….ben if you figured out please let me know

  4. Ben Lee

    Hi Gyu,
    I’ve gone through my instructions and found one small issue.
    There was a missing end tag in the section for configuring Apache. I’ve updated the blog with correction.
    Let me know if this fixes your issues.

  5. Peter

    Can I replace ubuntu with a username like peter1?

  6. Ben Lee

    Hi Peter,
    Any username can be used as long as the user has execute privileges on the WSGI script.

  7. Brian

    This also work for CentOS 7 if you have pip and flask installed. You will also need to create the user. Here are the commands:
    yum install httpd mod_wsgi -y python-pip -y
    pip install –upgrade pip
    pip install flask
    adduser centos
    ** Follow the above steps **
    #chown the files you created to apache
    change the user and group above to centos, chown the files to apache, and Bobs your uncle. Hope this helps!

Submit a Comment

Your email address will not be published. Required fields are marked *