Flask (or other WSGI) @ Binero

CGI WSGI (Flask) @ Binero

Experimental serving of python wsgi applications on binero hosting. Binero doesn’t have support for mod_wsgi or other wsgi server, but they have python and CGI support (or, officialy not, but it works), which together with other useful CLI apps makes it possible to set up. This is not so kind on the servers, the whole python interpreter having to be started for each call and all, but we’re only letting them serve a few pages to ourselves, so neglectable impact, right? Here’s an example of how to get a Flask app up and running.

Local installation of python 2.7 + Flask and deps

We cannot install python packages globally (obviously) and my guess is that crafting a lib directory by hand with Flask and all dependencies is a pita (not really, pip can install into arbitrary dir…).

Download python source package and unpack and cd to dir. Let’t not bother with virtualenv as it will depend on local installation anyways, just enable pip and install into local install dir.

cd domain.tld
mkdir python
cd python
wget Python-2.7.x.tar.xz --no-check-certificate
tar -Jxf Python-2.7.x.tar.xz
cd Python-2.7.x
./configure --prefix=[installdir]
make && make install
cd [installdir]
bin/python -m ensurepip
bin/pip install Flask

Create cgi-bin directory

A cgi-bin directory is no longer created by default when creating a new site using the control panel, but the servers will still execute scripts in such a dir if one exists. The CGI handler seems rather picky when it comes to file ownership and permissions though and the error messages are virtually useless. Creating the directory with “mkdir cgi-bin” over ssh won’t work as it will get user’s default gid and scripts won’t run. However if using FTP or the control panel filemanager to create the dir it will get a working gid (=33). Set the gid sticky bit on the directory so all files added to it will get the same gid automatically (chmod g+s cgi-bin). Also make sure that the scripts aren’t writable by group or the scripts won’t execute (chmod 755 script.cgi).

Or maybe just choose other dir and turn into cgi dir by adding appropriate directives to .htaccess. Like “Options ExecCGI” and “SetHandler cgi-script”, if permitted…

Set up test wsgi Flask app

Use python’s wsgiref.handlers.CGIHandler and a simple hello world app to test the setup. Browse to http://domain.tld/cgi-bin/wsgi.py to test. Replace PWD in path with path to your home directory, and again domain.tld with your domain name.


from wsgiref.handlers import CGIHandler
from main import app



from flask import Flask
app = = Flask(__name__)

def hello():
    return "Hello World!


Letting apache serve static files

We probably have a bunch of static files and want apache to serve exising files from public_html as usual and redirect all other requests (like to /) to our serve.py script. mod_rewrite makes this easy:


RewriteEngine On 
RewriteCond %{SCRIPT_FILENAME} !-f 
RewriteRule ^(.*)$ PWD/domain.tld/public_html/cgi-bin/wsgi.py/$1 [L]

Congrats, you can now serve wsgi applications!

Now you can create a more useful app, add some templates etc., and perhaps move it to the python dir if you wish, so to only keep wsgi.py in the cgi-bin dir (need to prepend python dir path to sys.path then though). Or move it to the virtualenv libs and the default sys.path should suffice for module import… or… or… whatever you please.