I'm looking for a simple, straightforward way to install Flask.

It should also be able to support the application being mounted on a URL Path other than "/" if needed.

asked 12 Mar '13, 22:12

ryans's gravatar image

ryans ♦♦
3.9k1934
accept rate: 44%


Step 1

First, install a "mod_wsgi 3.4/Python 2.7" app from one-click installer. Do not name the application "flask"; use something else. In this example, I'll name it "myflask".

Then, mount it to a website record. I'll use "/" for my URL Path to mount it at the root of the domain, but you could mount it at a different URL Path as well; for example "/foo".

Step 2

Next, open an SSH session. Enter the following commands to deploy flask.

#!/bin/bash

# First, fill in these variables with your Application Name and URL Path:

APPNAME="myflask"
URLPATH="/"

# Step 2
# deploy the app
cd $HOME
mkdir -p $HOME/lib/python2.7
easy_install-2.7 flask
rm -r $HOME/webapps/$APPNAME/htdocs
mkdir $HOME/webapps/$APPNAME/$APPNAME
touch $HOME/webapps/$APPNAME/$APPNAME/__init__.py
touch $HOME/webapps/$APPNAME/$APPNAME/index.py
echo "import sys" > $HOME/webapps/$APPNAME/wsgi.py
echo "sys.path.insert(0, '$HOME/webapps/$APPNAME')" >> $HOME/webapps/$APPNAME/wsgi.py
echo -e "from $APPNAME import app as application\n" >> $HOME/webapps/$APPNAME/wsgi.py
sed -i "s^WSGILazyInitialization On^WSGILazyInitialization On\nWSGIScriptAlias / $HOME/webapps/$APPNAME/wsgi.py^" $HOME/webapps/$APPNAME/apache2/conf/httpd.conf
sed -i "s^AddHandler wsgi-script .py^AddHandler wsgi-script .py\n    RewriteEngine on\n    RewriteBase /\n    WSGIScriptReloading On^" $HOME/webapps/$APPNAME/apache2/conf/httpd.conf
sed -i "s/htdocs/$APPNAME/g" $HOME/webapps/$APPNAME/apache2/conf/httpd.conf

cat << EOF >> $HOME/webapps/$APPNAME/$APPNAME/__init__.py
from flask import Flask
app = Flask(__name__)

EOF

if [[ "$URLPATH" != "/" ]]; then
cat << EOF >> $HOME/webapps/$APPNAME/$APPNAME/__init__.py
class WebFactionMiddleware(object):
    def __init__(self, app):
        self.app = app
    def __call__(self, environ, start_response):
        environ['SCRIPT_NAME'] = '$URLPATH'
        return self.app(environ, start_response)

app.wsgi_app = WebFactionMiddleware(app.wsgi_app)

EOF
fi

cat << EOF >> $HOME/webapps/$APPNAME/$APPNAME/__init__.py
@app.route("/")
def hello():
    return "Hello World!"

if __name__ == "__main__":
    app.run()
EOF

# Step 3
$HOME/webapps/$APPNAME/apache2/bin/restart

Hope that helps!

Acknowledgements: I incorporated pieces of the Flask WSGI guide, Flask WebFaction Snippet, and Flask Hello World example to create this guide.

link
This answer is marked "community wiki".

answered 12 Mar '13, 22:16

ryans's gravatar image

ryans ♦♦
3.9k1934
accept rate: 44%

edited 12 Mar '13, 22:31

Awesome reference and resource Ryan. Thank you for posting what you learned.

Any guidance on how I could modify/update your script to allow the application to use virtualenvwrapper?

Chris K.

(15 Aug '13, 00:54) chrislkeller chrislkeller's gravatar image

Hi Chris, First of all, let me say that the use of virtualenv is not recommended and it is usually much better to just configure things as described in the above post. But if you really want to add extra layers of abstraction and complexity into your app, here is how you can do it.

(15 Aug '13, 11:25) likebike ♦♦ likebike's gravatar image

Well written Ryans.

I noticed you put everything into the 'init.py' file (markup kills the underscores, sorry) instead of the index.py that you created in step 2. One can easily put the code in index.py and have the page still load if you modify the wsgi file to:

from $APPNAME.index import app as application

This allows the 'init.py' to remain empty or have additional package information in it, instead of code. This also helps avoiding confusion or conflicts when further code is developed.

(13 Sep '13, 09:36) pbulsink pbulsink's gravatar image
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×27

Asked: 12 Mar '13, 22:12

Seen: 2,614 times

Last updated: 13 Sep '13, 09:37

Plans & prices    Sign up    Why WebFaction?    Contact us    Affiliate program    Support    Legal    Jobs    Blog    Control panel login
Powered by OSQA
© Copyright 2003-2012 Swarma Limited - WebFaction is a service of Swarma Limited