WSGI (Web Server Gateway Interface) is an interface between web servers and web apps for python. A WSGI file enables Apache to communicate with a Flask application, by interpretting the python script.

This short guide will get a simple Flask application up and running uisng an apache virtual host and WSGI. We will create a Python 3 virtualenv for the Flask application.

Create a simple Flask application

Let's start by creating a simple Flask application on your local environment with a view to migrating this to your remote server afterwards.

Create a flaskapps directory inside the /var/www directory, this is where we will build our Flask app, you can create multiple flask apps in this directory if you wish.

$ sudo mkdir -p /var/www/flaskapps

Inside the flaskapps directory, create a directory for your Flask app called simpleflask.

$ sudo mkdir -p /var/www/flaskapps/simpleflask

Inside the simpleflask directory create a Python 3 virtual environment. The virtual environment enables us to migrate the app with the modules it needs and not have to worry about the python environment on the remote server

$ python3 -m venv venv

Activate the virtual environment

$ source venv/bin/activate

Note the (venv) prefix to your terminal prompt indicating that a virtualenv is active. You will also see a new venv directory in your simpleflask directory.

Install the Flask module

(venv) $ pip install flask

To deactivate the virtual environment

(venv) $ deactivate

Navigate to /var/www/flaskapps/simpleflask directory and create a directory named app

Inside the app directory create a file named __init__.py with the following content

from flask import Flask
app = Flask(__name__)
from app import routes

Also inside the app directory create another file named routes.py with the following content

from app import app

def index():
    return "Hello, Flask!"

Lastly, create a python script named /var/www/flaskapps/simpleflask/simpleflask.py at the top-level that defines the Flask application instance. simpleflask.py has the following content

from app import app

Activate the virtual environment

$ source venv/bin/activate

Run the Flask application

(venv) $ flask run

Navigate to localhost:5000/ to view the "Hello, Flask!" message served up by your Flask app.


We are able to run this app locally because Flask has an internal WSGI server for test that can directly execute an app via app.run().

Your directory structue should now look lke this


Press CTRL+C to quit and let's get our Flask app onto the remote server.


Install and enable mod_wsgi

We now have a simple Flask application running locally, it's time to move this to your server instance.

We will be taking the following steps to achieve this:

  1. Install dependencies on the remote server
  2. Migrate the Flask application to the remote server
  3. Add wsgi file to application
  4. Create new site in apache
  5. Adjust your DNS records
  6. View error log file


1. Install dependencies on the remote server

Connect to your remote server and install Apache and the Python 3 WSGI adapter module for Apache.

server@remote:~# sudo apt install apache2
server@remote:~# sudo apt-get install libapache2-mod-wsgi-py3


2. Migrate the Flask application to the remote server

On the remote server create a directory to mirror your local environment

server@remote:~# sudo mkdir -p /var/www/flaskapps

Migrate the simpleflask app from your local environment to the remote server do not use sudo for this command

$ scp -r wsgiflaskapp :/var/www/flaskapps/


3. Add a wsgi file to application

server@remote:~# sudo touch /var/www/flaskapps/simpleflask/simpleflask.wsgi

Add the following content to the /var/www/flaskapps/simpleflask/simpleflask.wsgi file

import sys
	from app import app as application

The directory structure on your remote server should now look like this



4. Create new site in Apache

In /etc/apache2/sites-available, create a copy of the 000-default.conf file and name it simlpleflask.conf

server@remote:~# sudo cp 000-default.conf simlpleflask.conf

Adjust the content of the simlpleflask.conf file to include the following

# However, you must set it for any further virtual host explicitly.
	ServerName simpleflask.com

	ServerAdmin webmaster@localhost
	DocumentRoot /var/www/html

	WSGIDaemonProcess simpleflask user=www-data group=www-data threads=5 python-home=/var/www/flaskapps/simpleflask/venv
	WSGIScriptAlias / /var/www/flaskapps/simpleflask/simpleflask.wsgi

		WSGIProcessGroup simpleflask
		WSGIApplicationGroup %{GLOBAL}
		Order deny,allow
		Require all granted

	# Available

Make sure the simpleflask.conf file has the following:

  1. The Correct ServerName, this will be the name you setup in your DNS records, for example if you have setup a DNS record for simpleflask..com then that is what you will assign to the ServerName constant in the simpleflask.conf file
  2. An app name that needs to appear in the WSGIDaemonProcess and later in the WSGIProcessGroup
  3. Paths for your python-home and WSGIScriptAlias that reflect your directory structure

In the /etc/apache2/sites-available directory, enable your site with the following command

server@remote:~# sudo a2ensite simpleflask

For the changes to take effect, reload the Apache 2 Web Server with the following command

server@remote:~# sudo systemctl reload apache2


5. Adjust your DNS records

With your domain name provider, setup your DNS records to point at your new site. This may take a few hours to update.


Open a browser and navigate to your new site


6. View error log file

If you experience any issues, the Apache error log is a great place to start troubleshooting. Use the following command to view the last 100 lines of the Apache error log:

sudo tail -100 /var/log/apache2/error.log