Productivity Sync Just another WordPress weblog

April 14, 2013

Setting up mod_wsgi to host flaskr

Filed under: Uncategorized — admin @ 5:47 pm

Problems and gotchas:


I spent a bunch of time today getting apache2 with mod_wsgi to share up the Flask sample application: “flaskr”  this is posting to capture the issues and solutions so when I look at this again I won’t be starting from scratch.

Setting up the virtual env within the *.wsgi file failed. I tried the thing but it never worked.

I kept getting a syntax error form mod_wsgi in the /var/FLASK/flaskr/flaskr.wsgi file on the line ” import sys”!!!! I eventually worked around it by setting the WSGIPythonPath in the apache2.conf file. (no clue on why it failed or why changing the apache2.conf fixed it)

I forgot to turn my flask app into a “module” the from flaskr import app line of the flaskr.wsgi file was failing because my app was a program an not a module that could be imported. Then I needed to fix up the import path to work with the sys.path I set up.

Finally the search path to the sqlite3 data base was path relative in my It needs to be absolute for mod_wsgi to work ok.

yup it was a pain in the butt.

Ref URLs


wsgi user account:

useradd -M flask
usermod -s /bin/false flask
usermod -L flask
adduser flask www-dat


add the following to the end of the file:

WSGIPythonHome /var/FLASK/venv


put the following in the <VirtualHost *:80> section

 WSGIDaemonProcess flaskr user=flask group=www-data threads=5 home=/var/FLASK
 WSGIScriptAlias /flaskr /var/FLASK/flaskr/flaskr.wsgi
 <Directory "/var/FLASK/flaskr">
   WSGIProcessGroup flaskr
   WSGIApplicationGroup %{GLOBAL}
   WSGIScriptReloading On
   Order deny,allow
   Allow from all

Set up working directory and virtualenv to use (do as root):

mkdir /var/FLASK/
 cd /var/FLASK
 virtualenv --no-site-packages venv
 . venv/bin/activate
 pip install Flask
 cp -r /home/<username>/dev/flaskr .
 chown -R flask:flask /var/FLASK


import sys
sys.path.insert(0, '/var/FLASK')
from flaskr.flaskr import app as application

Virtual Box bridge interface and ProxyChains

Filed under: Uncategorized — admin @ 5:27 pm

How to experiment with apache2-mod_wsgi using virtual box:

I wanted to figure out how to deploy Flask applications on my work computer without screwing it up too much with experimentation and hacking around in crazy ass ways.  So I decided to deploy it on a Virtual box ubuntu guest OS.  But to make it work I need the VBox guest to open up port 80 that my host could somehow send http requets too.

This post is basically a SOCKS enhanced version of the bridging information I posted about in 2009

The steps are:

  • HOST:
    • sudo apt-get install bridge-utils uml-utilities ssh
    • add to /etc/ssh/sshd_config: AllowTcpForwarding yes
    • create a scrip:
# create a tap
sudo tunctl -t tap1 -u <username>
sudo ip link set up dev tap1
# create the bridge
sudo brctl addbr br0
sudo brctl addif br0 tap1
# set the IP address and routing
sudo ip link set up dev br0
sudo ip addr add dev br0
sudo ip route add dev br0
    • Install VirtualBox
    • build a ubuntu guest VM from an ISO file.
    • After settup and installations are finished you will Configure it to use bridged networking on the tap1 interface
    • For initial setup you will use NAT to because of proxy issues with bridging to tap1.
  • Guest
    • apt-get install tmux apache2 vim  ssh pyflakes pep8 python-setuptools python-virtualenv proxychains libapache2-mod-wsgi
    • /etc/proxychains.conf: [ProxyList] socks5 9999
    • Firefox: about:config (in addressbar):  network.prox.sock_remote_dns true
    • Firefox manual proxy setting:  localhost Port 9999, SOCKSv5
    • ssh -C <host_username>@ -D 9999 -N


with this configuration / set up the Vbox guest is now able to host a web server to to the host OS.  Also, the guest web browser works ok and I can use “proxychains apt-get” and proxify other non proxy aware applications using proxychains.



Powered by WordPress