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

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

For a tutorial on this using Gunicorn, click here.  If you want to do it by manually configuring mod_wsgi, read on.

(For a tutorial on deploying a Flask app with Python 2.7, read our other post.)

After hopping into your favorite banca, SSH into your server and and work from the command line or console.

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__)
@app.route("/")
def hello():
    return "Please, please work!"
if __name__ == "__main__":
    app.run()

Save and close __init__.py

Step 3. Install Pip and Flask

apt-get install -y python3-pip
apt-get install -y python3-venv

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

python3 -m venv 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 http://127.0.0.1:5000/”. 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
        </Directory>
        Alias /static /var/www/FlaskApp/FlaskApp/static
        <Directory /var/www/FlaskApp/FlaskApp/static/>
            Order allow,deny
            Allow from all
        </Directory>
        ErrorLog ${APACHE_LOG_DIR}/error.log
        LogLevel warn
        CustomLog ${APACHE_LOG_DIR}/access.log combined
        WSGIDaemonProcess pylons python-home=/var/www/FlaskApp/yourenvironment
</VirtualHost>

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:

#!/usr/bin/python                                                                

import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/FlaskApp/")

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

Now your directory structure should look like this:

|--------FlaskApp
|----------------FlaskApp
|-----------------------static
|-----------------------templates
|-----------------------yourenvironment
|-----------------------__init__.py
|----------------flaskapp.wsgi

Restart Apache to effect all changes.

service apache2 restart

And you are done!  Thanks to this digitalocean tutorial and Graham Dumpleton’s fix that enabled us to figure out how to do this with Python 3.5.

Leave a Reply

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