If you benefit from web2py hope you feel encouraged to pay it forward by contributing back to society in whatever form you choose!

Setting up web2py using FastCGI on Hostmonster's shared hosting

Preface

This document describes all the steps it took me to get web2py running on Hostmonster's shared hosting. It is is largely based on the following two articles:

"Setting up Web2Py in a Subdirectory using FastCGI" by rppowell

Web2py on Dreamhost

However, it took me much effort to fill up parts that were missing for me, so I think this description might bring you some additional value.

Setting up Python

First issue I encountered was inability to install virtualenv under the preinstalled Python2.6 (somehow connected with the installation's missing Makefile). Now, virtualenv is important, because it allows to set up Python home with all necessary modules independently of the hosting installation, which is very usefull in case of shared hosting. In our case, we will need to install several modules, and without virtualenv we would have to mess with PYTHONPATH and other stuff, and it won't do us any good.

So, I decided to take law into my own hands and install the brand new Python2.6.5 at my home folder.

This is how I did it:

First download and unpack the latest version of Python:

cd $HOME
wget http://www.python.org/ftp/python/2.6.5/Python-2.6.5.tgz
tar xzf Python-2.6.5.tgz

Now, compile and install:

cd $HOME/Python-2.6.5
./configure --prefix=$HOME
make
make install

$HOME/bin folder will be created and will contain python executable.

Once we have Python, it is time to get and install virtualenv:

cd $HOME
wget http://pypi.python.org/packages/source/v/virtualenv/virtualenv-1.4.7.tar.gz
tar xzf virtualenv-1.4.7.tar.gz
bin/python virtualenv-1.4.7/virtualenv.py local

At this point, we have new independent Python home with Python2.6.5 executable at $HOME/local/bin/python.

Now we will install ez setup which will allow us to easily add all additonal modules without the hassle of looking for download URL and figuring where to put them:

cd $HOME
wget http://peak.telecommunity.com/dist/ez_setup.py
local/bin/python ez_setup.py

Now we can bring all additional modules necessary for healthy operation of web2py under FastCGI:

# driver for MySQL database driver
$HOME/local/bin/easy_install MySQL-python
# FCGI wrapper
$HOME/local/bin/easy_install flup

At this point we are done with setting up Python.

Installing web2py

In case of Hostmonster, www root is located at $HOME/public_html. For me, the natural choice was to put web2py as a folder under public_html, so that it won't mess with other stuff.

wget http://www.web2py.com/examples/static/web2py_src.zip
unzip web2py_src.zip -d $HOME/public_html

Setting up FastCGI

At this point, we need to wrap web2py with FastCGI to expose it to the web server.

Go to the folder where web2py root is located ($HOME/public_html/web2py) and create new file web2py.fcgi with the following content:

#!/home/username/local/bin/python
import sys
from flup.server.fcgi_fork import WSGIServer
import gluon.main
application=gluon.main.wsgibase
# or
# application=gluon.main.wsgibase_with_logging
WSGIServer(application).run()

Pay attention to the first line. It must specify the path to the python executable in virtual python home we've set up earlier. /home/username should be replaced by the content of $HOME environmental variable (don't just put $HOME, it won't be expanded), which can be looked up as follows:

echo $HOME

After the file is created, make it executable by giving appropriate permission:

chmod +x web2py.fcgi

Now, in the same folder create additional file .htaccess with the content:

RewriteEngine On
RewriteRule ^web2py\.fcgi/ - [L]
RewriteRule ^(.*)$ web2py.fcgi/$1

This file effectively tells Apache to rewrite all URL directed to web2py folder by adding web2py.fcgi, which will invoke web2py engine.

Note, that here RewriteBase is missing, contrary to rppowell's instructions, it just didn't work that way in my case.

The last thing to do is to set up web2py internal routing by means of editing routes.py:

routes_in=(('/web2py/(?P<a>.*)','/\g<a>'),)
...
routes_out=(('/(?P<a>.*)','/web2py/\g<a>'),)

Administrator access:

In order to be able to access web2py administrator application, which is very convinient for setting up new applications, one needs two things:

1) secure connection (HTTPS) or access via SSH tunnel (haven't tried)

2) having set administrator password

The latter can be achieved by running web2py directly and renaming the resulting password file, for example:

$HOME/local/bin/python $HOME/public_html/web2py/web2py.py --ip=127.0.0.1 --port=8080 --password=somepassword

This will result in creating parameters_8080.py file in web2py folder. Just rename it to parameters_80.py, and it will be used by web2py when run through FastCGI on port 80, so that you'll be able to access administator application with the somepassword password.

If you haven't set up HTTPS and don't want to mess with SSH tunnel, there's another way to enable admin access by disabling the check for secure connection, but it is a hack and should be avoided. Open the file $HOME/public_html/web2py/applications/admin/models/access.py and comment out the following lines:

if request.env.http_x_forwarded_for \
        or request.env.wsgi_url_scheme in ['https', 'HTTPS'] \
        or request.env.https == 'on':
    session.secure()
elif not remote_addr in hosts:
    raise HTTP(200, T('Admin is disabled because insecure channel'))

Related slices

Comments (6)

  • Login to post



  • 0
    johncc 7 years ago

    Ignore the link to Dreamhost - all information on hosting web2py was removed from the Knowledge base. When asked, DH directed me to a external site (which didn't have info either).

     


  • 0
    watr 10 years ago

    This tutorial didn't work for me until I moved my web2py directory into a cgi-bin folder that had chmod -R 755 applied to it. Then it works great, except I still haven't got admin to work. May post a follow-up comment on how to get it to run later (note the hack mentioned in the article about commenting out code doesn't work).


  • 0
    rahuld 13 years ago
    I have configured everything like you've said here. Everything done fine. I am using cpanel. I think its a bit similar to configure it but how does it launch web2py? and what would be the url i need to fire in webbrowser to check it? Also we did not do anything for Apache configuration.. Any restarts etc needed? Please suggest.

  • 0
    leftcoastgeek 13 years ago
    I had this message on login, "admin disabled because unable to access password file" I fixed it by changing "parameters_80.py" file to "parameters_443.py". thanks -Keith

  • 0
    iggass 13 years ago
    Hi Dave, What error log are we talking about? I'd suggest putting some printouts in web2py.py in order to see if it's been called. If it does, then there's a problem with web2py which should be debugged, otherwise it's likely some apache configuration problem, and hostmonster's helpdesk are likely to be able to help. Actually, I just finished setting up another hostmonster account according to this instructions, it went completely OK. No particular reason for using 8080 except: 1) it seems to be open by default. For other ports (like 8000), you'd need to submit request to the support. 2) it actually doesn't matter, it just being copied to another file, Apache listens to 80 anyway. If you have any other questions, please contact me directly, as I'm not likely to visit this page anywhere soon. Best, Igor
show more comments

Hosting graciously provided by:
Python Anywhere