## 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.

#### cgi-bin/wsgi.py

#!PWD/domain.tld/python/Python-2.7
from wsgiref.handlers import CGIHandler
from main import app

CGIHandler().run(app)

#### cgi-Bin/main.py

from flask import Flask

@app.route("/")
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:

#### .htaccess

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.

## Simple way to get mail off the system – nullmailer and nail

###### nullmailer

Nullmail provides a sendmail implementation that forwards the mails though a smtp-server.  You have to set up some basic information in nullmailer’s config files. Which address to send from, which domain to use if only a username is provided and the smtp server through which to do the actual sending.

mignon@hades nullmailer % sudo tail -n+1 *
root@eleusis.se

==> defaultdomain <==
eleusis.se

==> me <==
eleusis.se

==> remotes <==
smtp.eleusis.se smtp auth-login user=******@eleusis.se pass=******** port=587 starttls
###### nail

Some existing scripts / software doesn’t invoke sendmail directly to send mail but expects the mail command to be present on the system (mdadm, smartd…). For those we can simply install the nail which will provide minimal mail and mailx commands and will with an empty configuration file send via nullmailers sendmail just like we want.

One could actually use nail exclusively as it also has support for sending via an smtp server directly. But other packages had dependencies on virtual/mta (Gentoo) that pulled in nullmailer (unless other mta installed) anyways.