Tutorials

Deploy a Flask Application on Ubuntu 14.04

Table of Contents

Introduction

Flask is a micro web framework written in Python and based on the Werkzeug toolkit and Jinja2 template engine. It is BSD licensed. Flask tackles routing, HTML template rendering, sessions etc.You can develop web apps quickly using Flask. Two examples of large web applications that use the Flask framework are Pinterest and LinkedIn. Flask is called a micro framework because it does not force a developer to use a particular tool or library. It has no database abstraction layer, form validation, or any other components where pre-existing third-party libraries provide common functions.

In this tutorial, we are going to show you how to deploy your application using Flask on a Ubuntu 14.04 server. The process should be very similar on other versions of Ubuntu with only minor modifications due to package dependencies and what is included by default.

Requirements

  • A server running Ubuntu 14.04 with Apache installed.
  • A static IP Address for your server.
  • A non-root user account with sudo privilege set up on your server.

Install and Enable mod_wsgi

The Web Server Gateway Interface (WSGI) is a specification for simple and universal interface between web servers and web applications or frameworks for the Python programming language. Mod_wsgi is an Apache HTTP Server module that provides a WSGI compliant interface for hosting Python based web applications under Apache. It enables Apache to serve Flask applications.

You can easily install mod_wsgi by running the following command:

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

Next, you will need to enable mod_wsgi Apache module. To do so, run the following command:

sudo a2enmod wsgi

Creating a Flask App

In this step, we will create a Flask app and place our app in the /var/www directory.

Now, change to the /var/www directory:

cd /var/www/

Now, create the application directory structure using the following command:

sudo mkdir Flask
cd Flask

Create another directory Flask by giving following command:

sudo mkdir Flask
cd Flask

Now, create two subdirectories named static and templates using the following commands:

sudo mkdir static templates

Now, create the __init__.py file that will contain the Flask application logic.

sudo nano __init__.py

Add the following content:

from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
    return "Hello, Welcome to Profitbricks!"
if __name__ == "__main__":
    app.run()

Save and close the file.

Install Flask

Now, you will need to set up virtual environment that will keep the application and its dependencies isolated from the main system.

You can use pip to install virtualenv and Flask. If pip is not installed, you can install it by running the following command:

sudo apt-get install python-pip

Now, install virtualenv by running the following pip command:

sudo pip install virtualenv

Next run the following command with the name of your temporary virtual environment.

sudo virtualenv Virtenv

Now, install Flask in that environment by activating the virtual environment with the following command:

source Virtenv/bin/activate

Now, run following command to install Flask inside it:

sudo pip install Flask

Next, run the following command to test if the installation is successful and the app is running:

sudo python /var/www/Flask/Flask/__init__.py

You should see the following output:

* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

If you see the above output, you have successfully configured the app.

You can also deactivate the virtual environment by running the following command:

deactivate

Configure New Virtualhost for Flask

Now, you will need to create a new virtual host file for Flask App.

To do so, run the following command:

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

Add the following content. Make sure change the ServerName to your domain name:

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

Save and close the file.

Enable the virtual host with the following command:

sudo a2ensite FlaskApp

Next, create a flaskapp.wsgi file inside the Flask directory to serve the Flask App.

sudo nano /var/www/Flask/flaskapp.wsgi

Add the following content:

#!/usr/bin/python
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/Flask/")

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

Save the file and restart Apache to apply the changes

sudo apachectl restart

To test your Flask App, open your favorite web browser and type the URL (http://yourdomain.com) that you entered in your virtual host configuration.

Alternatively, you could use the following curl command in your terminal:

curl http://yourdomain.com

You should see the following output:

Hello, Welcome to Profitbricks!

Enjoy!........