Home » Technical » How To Deploy A Flask Python 2.7 Application On Linux Ubuntu 16.04 Running Apache2

How To Deploy A Flask Python 2.7 Application On Linux Ubuntu 16.04 Running Apache2

There are tutorials for this, like the one here.  However, following the instructions exactly might result in an internal server error that will leave you dazed for a whole weekend.  The problem is best described here.  However, the suggested solutions might not make your weekend better, as they overlook one important thing…

And the culprit is this: the tutorial although almost complete, does not work for either Python 2.7 or Python 3.5.  It lacks some lines of code to work in either versions, in short not providing a clear solution.

So let us clean it up and show the correct tutorial for Python 2.7; for a tutorial on setting up Flask for Python 3.5 on Ubuntu 16.04 go to our other post.

SSH into your server and and work from the command line or console.  Here are the steps.

Step 1. Install and enable mod_wsgi

apt-get install libapache2-mod-wsgi python-dev
a2enmod wsgi

Step 2. Create a Flask app

cd /var/www

Create a directory for your app:

mkdir FlaskApp
cd FlaskApp

Yes, create another directory under your app:

mkdir FlaskApp
cd FlaskApp
mkdir static templates

Create your app:

nano __init__.py

Copy and paste the following into __init__.py:

from flask import Flask
app = Flask(__name__)
def hello():
    return "Please, please work!"
if __name__ == "__main__":

Save and close __init__.py

Step 3. Install Pip and Flask

apt-get install python-pip
pip install virtualenv

Create virtual environment (with Python 2.7) in it, activate it and install Flask into it:

virtualenv yourenvironment
source yourenvironment/bin/activate
pip install Flask

Test your python application:

python __init__.py

It should display “Running on http://localhost:5000/” or “Running on”. If you see this message, you have successfully configured the app.

Step 4. Configure and Enable Your Site

nano /etc/apache2/sites-available/FlaskApp.conf

Add the following lines of code to FlaskApp.conf to configure the virtual host. Be sure to change the ServerName to your domain or cloud server’s IP address:

<VirtualHost *:80>
		ServerName mywebsite.com
		ServerAdmin admin@mywebsite.com
		WSGIScriptAlias / /var/www/FlaskApp/flaskapp.wsgi
		<Directory /var/www/FlaskApp/FlaskApp/>
			Order allow,deny
			Allow from all
		Alias /static /var/www/FlaskApp/FlaskApp/static
		<Directory /var/www/FlaskApp/FlaskApp/static/>
			Order allow,deny
			Allow from all
		ErrorLog ${APACHE_LOG_DIR}/error.log
		LogLevel warn
		CustomLog ${APACHE_LOG_DIR}/access.log combined

Save and close.

Enable the virtual host with the following command:

a2ensite FlaskApp

Step 5. Create the .wsgi file

cd /var/www/FlaskApp
nano flaskapp.wsgi

Add the following lines of code to flaskapp.wsgi:

activate_this = '/var/www/FlaskApp/FlaskApp/yourenvironment/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

import sys
import logging

from FlaskApp import app as application
application.secret_key = 'Add your secret key'

Now your directory structure should look like this:


Restart Apache to effect all changes.

service apache2 restart

And you are done!

Leave a Reply

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