Setting up web2py using FastCGI on Hostmonster's shared hosting
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:
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.
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:
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>'),)
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'))