Skip to content

Setting Up: Flask

Sarah Anoke edited this page Feb 11, 2020 · 6 revisions

Flask was set up on a dedicated m5.large EC2 instance (2 vCPUs, 8GB memory), and served with Apache Server via WSGI.

1 - Installation of dependencies

sudo apt update
sudo apt install apache2
sudo apt install libapache2-mod-wsgi-py3
sudo apt install python3-pip
sudo pip3 install flask

2 - Create application

The website lives inside the directory ~/flaskapp, which is soft-linked to Apache's default web directory.

mkdir ~/flaskapp
sudo ln -sT ~/flaskapp /var/www/html/flaskapp

Test whether the link is working by writing something to the homepage and viewing it via the instance's public IP.

echo "Hello World" > ~/flaskapp/index.html

3 - Enable WSGI

WSGI is a specification that helps Apache display the dynamic content from our app. We enable it by editing Apache's configuration file.

sudo nano /etc/apache2/sites-enabled/000-default.conf

Add the following text just after the DocumentRoot /var/www/html line in 000-default.conf. This specification is for a website that'll live at <public-IP>/flaskapp.

WSGIDaemonProcess flaskapp threads=5
WSGIScriptAlias / /var/www/html/flaskapp/flaskapp.wsgi
<Directory flaskapp>
    WSGIProcessGroup flaskapp
    WSGIApplicationGroup %{GLOBAL}
    Order deny,allow
    Allow from all
</Directory>

Finally, initialize WSGI.

sudo a2enmod wsgi

4 - Write app script and restart webserver

The web interface is a Flask app served with Apache Server via a WSGI. The app itself is very standard, with

  1. a Python script translating user input into its corresponding SQL query and retrieving the results
  2. a single HTML file that displays the results in a plot

The plot is rendered with JavaScript (Chart.js) and website styled with the Bootstrap CSS library.

After any edits to the app or webpage, I needed to restart the server to see the changes.

sudo apachectl restart

[Hint] If the webpage is rendering weird, can check the error log at /var/log/apache2/error.log.