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 activate_this.py 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 flask.py. It needs to be absolute for mod_wsgi to work ok.

yup it was a pain in the butt.

Ref URLs

http://www.subdimension.co.uk/2012/04/24/Deploying_Flask_to_Apache.html
https://code.google.com/p/modwsgi/wiki/VirtualEnvironments
http://flask.pocoo.org/docs/deploying/mod_wsgi/
http://thegnar.org/sync/?p=1
http://thegnar.org/sync/?p=299

Configurations:

wsgi user account:

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

/etc/apache2/apache2.config:

add the following to the end of the file:

WSGIPythonHome /var/FLASK/venv

/etc/apache2/sites-available/default:

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
 </Directory>

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

/var/FLASK/flaskr/flaskr.wsgi:

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 http://thegnar.org/sync/?p=1

The steps are:

  • HOST:
    • sudo apt-get install bridge-utils uml-utilities ssh
    • add to /etc/ssh/sshd_config: AllowTcpForwarding yes
    • create a vboxbridge.sh 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 10.1.1.1/24 dev br0
sudo ip route add 10.1.1.0/24 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 127.0.0.1 9999
    • Firefox: about:config (in addressbar):  network.prox.sock_remote_dns true
    • Firefox manual proxy setting:  localhost Port 9999, SOCKSv5
    • ssh -C <host_username>@10.1.1.1 -D 9999 -N

 

with this configuration / set up the Vbox guest is now able to host a web server to 10.1.1.2:80 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