Building a Flask Foundation


Not exactly how we do it, but there are additional resources here:

Flask Documentation

Great book. While it is not free, you can find it in Humble Bundles for a great deal if you watch.


At this point, you should already have a LAMP stack set up. If you don’t, you can follow my guide here: LAMP Stack on Ubuntu Server

Now that we have our LAMP stack as a foundation, we are going to start building our application layer. Our application layer is going to utilize Flask for the web framework. Flask is Python based and really modular, so it works well for projects of all sizes. It also has a lot of support and libraries which makes the development process much faster.

Flask Directory

To start, we are going to build our Flask directory structure.

Navigate to the apache web folder
cd /var/www
Create a new directory for our project
sudo mkdir app
Assign your user as the owner, and make www-data the group (so it has web access) *Be sure to change my username to yours.
sudo chown tannercrook:www-data app
Navigate into our project’s directory
cd app

We are going to use this as our base hub. This is an area that can hold files that will be separate from the publicly available resources. For example, we may put a database connection file here containing our database credentials for the app to use. Obviously, we don’t want the internet to have access to this file.

So, let’s build our Flask folder and structure:

Build the flask app directory and move into it
mkdir app
cd app
Build the various directories for Flask
mkdir -p models static/styles templates
Build our main app python file

We should now have our base structure for Flask:
├── models
├── static
│   └── styles
└── templates


Our Flask Application

Now we need to set up our file so we can configure and test Apache.

* We have already generated a secret key, but if you want to generate one of your own (if you host your own) you can generate it by using the instructions here.

from flask import Flask 
from flask import render_template

app = Flask(__name__)
app.config['SECRET_KEY'] = '\xd2\x04S4\xbc\xce\xe2\x17\xfb\xff\x19C@\xa6e\xc2\xf4\x18\xad\xe8\xc4\xcb'

def index():
    return 'Hello, World!'

Place the following code of your empty file using either FTP and your editor or the command line (editing with nano):
To paste: ctrl+shift+v
To save: ctrl+o

Once that is saved, we need to set up our python environment.
python3 -m venv venv
This will create a container for python so we can install components that won’t interfere with other parts of the system. Or if you wanted to have multiple Flask apps running, you don’t have to worry about interference between the two.

To activate our environment
source venv/bin/activate
And you should now see (venv) at the beginning of your prompt.

Now we can install the python dependencies we used in our file.
pip3 install wheel
pip3 install flask

Once we have done that, our Flask directory is set up!


Apache and WSGI Configuration

For our webserver, we are using Apache which will direct to WSGI to serve flask URL.

First, we need to set up our WSGI config file in a main directory
cd ..
Should return /var/www/app

Let’s create the file:
touch app.wsgi
nano app.wsgi

import sys
import logging

from app import app as application
application.secret_key = '\xd2\x04S4\xbc\xce\xe2\x17\xfb\xff\x19C@\xa6e\xc2\xf4\x18\xad\xe8\xc4\xcb'

WSGI is now ready to go! Now we just need to configure Apache to point to our Flask configuration.

Change permissions on our files
sudo chown -R tannercrook:www-data app
sudo chmod -R 774 /var/www/app
Let’s install the Apache WSGI mod
sudo apt install libapache2-mod-wsgi-py3
Ensure it is enabled
sudo a2enmod wsgi

We will replace the existing default website config with our new config.
sudo nano /etc/apache2/sites-available/000-default.conf


<VirtualHost *:80>
        ServerAdmin webmaster@local

        # Build WSGI for Flask
        WSGIDaemonProcess app python-home=/var/www/app/app/venv
        WSGIScriptAlias / /var/www/app/app.wsgi process-group=app application-group=%{GLOBAL}

        <Directory /var/www/app/app/>
                Order allow,deny
                Allow from all
        Alias /static /var/www/app/app/static
        <Directory /var/www/app/app/static/>
                Order allow,deny
                allow from all

        ErrorLog ${APACHE_LOG_DIR}/error.log
        LogLevel warn

        CustomLog ${APACHE_LOG_DIR}/access.log combined


Restart Apache
sudo service apache2 restart

If all went well, you should now be able to enter the IP Address of your server in a browser and see ‘Hello, World!’. This means that you have a working LAMP stack with Flask!






Leave a Reply

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

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>