How to deploy Flask applications to Apache webserver

This is a simple guide explaining how I managed to configure Apache 2.2 httpd server on a Windows platform so that it can serve a Python webapplication I wrote using the Flask micro-framework. The guide is valid, with a very little modification, also on Linux environments (you geeks know how to do)

Why I needed to to this

I developed this application at work and ‘ve been serving it from the beginning via the Flask’s built-in minimal webserver: unfortunately this  is not enough for production stage as I need a more robust server with SSL capabilities, which Flask’s has not. This was my first time in deploying a Python webapp…So, after googling a bit and reading the Flask deployment notes, I came up with the answer: what I needed was a WSGI-compliant server running on my target platform, a Windows 2012 server. The natural choice to me was to enable the WSGI module on the “good ole” Apache webserver, which I’m experienced with.

Steps

Flask app

We choose a folder in which we place the Python code. For instance,

D:\webapps\test

In this folder we create the real Flask webapplication that we want to deploy (file “test.py”):

from flask import Flask, request
app = Flask(__name__)

@app.route('/hello')
def hello_world():
    name = request.args.get('name','')
    return 'Hello ' + name + '!'
if __name__ == '__main__':
    app.run()

The Apache server won’t be aware of “test.py” at all. What you need to do now is to write in the same folder a Python file named “test.wsgi” that we will link into the webserver’s configuration: the code in this file will import the main Flask application object (built in our case as a singleton) and will be actually executed by the WSGI module of Apache. In the code, it is vital that you DON’T change the name of the “application” variable, as it is exactly what the server expects to find. Also please note that we are extending the Python classes path to include our own webapplication’s folder. This is “test.wsgi”:

import sys

#Expand Python classes path with your app's path
sys.path.insert(0, "d:/webapps/test")

from test import app

#Put logging code (and imports) here ...

#Initialize WSGI app object
application = app

As an additional remark, if you want to put any logging code (e.g: file/e-mail/console loggers) into your Flask app, you must put it before the if __name__ == ‘__main__’ block, otherwise it won’t log anything! Add your loggers to the app object.

Apache setup

Ok, what’s next? Now it’s all about installing and properly configuring Apache.

First: install Apache webserver. I downloaded and executed the .msi installer. Apache was installed at

"C:\Program Files (x86)\Apache Software Foundation\Apache2.2"

Second: install the WSGI Apache module. Pay attention to download the module compiled for your specific combination of platform and Python and Apache versions: I downloaded this module. Once downloaded, rename the .so file into “mod_wsgi.so” and put it under the “modules” subfolder of your Apache installation folder. Then you have to tell Apache to use it: open in a text editor the “httpd.config” file which is under the “conf” subfolder and add the following line at the bottom:

LoadModule wsgi_module modules/mod_wsgi.so

Third: restart Apache.

Now Apache is ready to serve WSGI webapplications. What is left is to tell about where our application is and match it to a URL alias. It’s child’s play: open in a text editor the “httpd.config” file we used before and add these lines to the bottom:

<Directory d:/webapps/test>
    Order allow,deny
    Allow from all
</Directory>
WSGIScriptAlias /flasktest d:/webapps/test/test.wsgi

(nevertheless, I prefer to place the per-virtual-host or per-alias configurations’ stuff into separate files and then use an Include directive into the main “httpd.conf”)

Now restart Apache again and if you open a browser and point it to:

http://localhost/flasktest/hello?name=claudio

and you should see the webapp’s greetings!

Further references

  • This guide helped me a lot in understanding how to setup Apache WSGI.
  • I also found this tutorial which is far more comprehensive than mine and covers Flask deployment on Apache on Debian/Ubuntu environments

How to install MongoDB on Ubuntu

This is a quick-guide to install MongoDB on Ubuntu 12.04 (if you want to know how to install it also on Fedora Core 11, please check out my old post.

Here is all you have to do:

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10
sudo touch /etc/apt/sources.list.d/10gen.list
line="$(echo "deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen")"
sudo echo "$line" > /etc/apt/sources.list.d/10gen.list
sudo apt-get update

And then you can install MongoDB with

sudo apt-get install mongodb-10gen

Enjoy!

How to install MongoDB on Fedora

During the last days, I’ve been delving into NoSQL datastores study and now I’ve got the chance to use MongoDB for a real-life need (of course, something relating to work issues). In order to reach my target, I needed a fresh installation of one of the most promising NoSQL technologies: the document-oriented datastore MongoDB.

Here is how I managed to install MongoDB 2.0.3 on a Fedora Core 11 host. You can easily adapt the steps I went through to your operational environment.

We’re about to issue every command as root user.

First, I downloaded, extracted and placed the Mongo stuff (I didn’t use 10gen repo, but just a tar.gz from MongoDB website):

cd /opt
wget http://fastdl.mongodb.org/linux/mongodb-linux-i686-2.0.3.tgz
tar xvf mongodb-linux-i686-2.0.3.tgz
mv mongodb-linux-i686-2.0.3.tg mongodb-2.0.3

I decided that my databases would go under /usr/data/mongodb and that MongoDB log file would be /var/log/mongodb.log

As I wanted my MongoDB server instance to be started/stopped as a demon, I prepared the following init.d script named “mongodb” and placed it under /etc/init.d/ :

#!/bin/bash
source /etc/rc.d/init.d/functions
prog="mongod"
mongod="/opt/mongodb-2.0.3/bin/mongod"
RETVAL=0

start() {
  echo -n $"Starting $prog: "
  #This is the fundamental call to start the MongoDB server instance
  daemon $mongod "--fork --journal --dbpath /usr/data/mongodb \
  --logpath /var/log/mongodb.log \
  --logappend 2&gt;&amp;1 &gt;&gt;/var/log/mongodb.log"
  RETVAL=$?
  echo
  [ $RETVAL -eq 0 ] &amp;&amp; touch /var/lock/subsys/$prog
  return $RETVAL
 }

stop() {
  echo -n $"Stopping $prog: "
  killproc $prog
  RETVAL=$?
  echo
  [ $RETVAL -eq 0 ] &amp;&amp; rm -f /var/lock/subsys/$prog
  return $RETVAL
}

case "$1" in
 start)
  start
  ;;
 stop)
  stop
  ;;
 restart)
  stop
  start
  ;;
 status)
  status $mongod
  RETVAL=$?
  ;;
 *)
  echo $"Usage: $0 {start|stop|restart|status}"
  RETVAL=1
 esac

exit $RETVAL

Then, I opened my Iptables firewall’s INPUT chain so that port 27017 (the port MongoDB server is listening to) is not blocked: I opened the file /etc/sysconfig/iptables and added the following rule before of the COMMIT statement:

-A INPUT -p tcp -m tcp -m multiport --ports 27017 -j ACCEPT

and restarted iptables with:

service iptables restart

That’s it. Finally, I started the server instance with:

service mongodb start

and tested the whole thing opening the Mongo Javascript shell like this:

cd /opt/mongodb-2.0.3/bin
./mongo

and everything was fine.