Getting Started with Flask: Part 5

By Michael

Welcome to part 5 of my getting started guide. If you missed part 4 you can find it here.

Apache and WSGI

I will assume you have a Ubuntu server somehwere and that you have access and permissions to set up the needed packages for this article. If you do not there are many helpful places online to figure that out and help you get started. You will also need to have a domain and DNS set up if you intend to use this as a server on the internet.

Getting started

First we will get our dependancies installed then set up the virtual environment that will allow us to run our Python Flask project from an Apache worker thread.


michael@ubuntu:~$ sudo apt install apache2 mysql-server phpmyadmin libapache2-mod-wsgi python3-dev libmysqlclient-dev python3-venv

Don't forget to set a MySQL password you can remember!

Now lets create and set up our virtual environment. Navigate to your project's directory and follow the commands.

Ubuntu (Python 3.6)

michael@ubuntu:~$ cd /var/www/blog
michael@ubuntu:/var/www/blog$ pip install venv
michael@ubuntu:/var/www/blog$ python -m venv my-project-env
michael@ubuntu:/var/www/blog$ source my-project-env/bin/activate
(my-project-env) michael@ubuntu:/var/www/blog$ pip install flask flask_sqlalchemy flask_migrate flask_login mysqlclient

We can check the database is ready for us. Lets first navigate to our server's phpmyadmin and add the user and database as you did in Part 2. Make sure you update the `app.config['SQLALCHEMY_DATABASE_URI']` value in ``. If you coppied the migrations folder delete it as that will prevent the database from being created.

A Choice

You can start from a fresh database here or import your database from your local mysql database. If you want to import follow guides on the internet then skip to Apache and WSGI.


(my-project-env) michael@ubuntu:/var/www/blog$ flask db init
(my-project-env) michael@ubuntu:/var/www/blog$ flask db migrate -m "Initial setup of the database"
(my-project-env) michael@ubuntu:/var/www/blog$ flask db upgrade

We don't want to be left without a user to login with so lets do that now if everything went ok.


(my-project-env) michael@ubuntu:/var/www/blog$ python
# >>> from app import db, User
# >>> from import generate_password_hash
# >>> user = User(name='Michael', email='', password=generate_password_hash('password'))
# >>> db.session.add(user)
# >>> db.session.commit()
# >>> exit()
(my-project-env) michael@ubuntu:/var/www/blog$ deactivate


Create a file called `app.wsgi` inside your blog directory and lets set up.

import logging
import sys

from app import app as application

Apache Config

Now lets create the config we will use for Apache to run our WSGI Flask app. Create a confi for the site, mine is named `/etc/apache2/sites-available/blog.conf` and open it up.


michael@ubuntu:/var/www/blog$ sudo nano /etc/apache2/sites-available/blog.conf

The contents


<VirtualHost *:80>

 DocumentRoot /www/var/blog

 WSGIDaemonProcess blog python-home=/var/www/blog/my-project-env
 WSGIProcessGroup blog

 WSGIScriptAlias / /var/www/blog/app.wsgi

 <Directory /home/blog>
  Options FollowSymLinks
  AllowOverride None
  Require all granted

 ErrorLog /var/www/blog/error.txt
 CustomLog /var/www/blog/access.txt combined


We're almost there. If you do not have any errors in typing you can run the next command to enable and reload Apache to see your new blog!


michael@ubuntu:/var/www/blog$ sudo a2ensite blog.conf
michael@ubuntu:/var/www/blog$ sudo systemctl reload apache2

That is it for now. I'll revisit this if I see any improvements.

Other posts by Michael

Getting Started with Flask: Part 5

In our final installment we will set up Apache and WSGI to make our app run in a production environment.

Getting Started with Flask: Part 3

Admin page and authentication!