metadata-list: orientation=horizontal}
| Name |
Installing and running Pylons as plain CGI on a webhost |
| Space |
Pylons CookBook |
| Section |
|
| Page |
Installing and running Pylons as plain CGI on a webhost |
| Version |
1.0 |
| Status |
Draft |
| Curator |
Graham Higgins |
| Reviewed |
False |
| Author(s) |
Bayle Shanks |
Installing and running Pylons as plain CGI on a webhost
Today I installed a rudimentary Pylons website on a website hosted by NearlyFreeSpeech.net
.
Setting up Pylons was comparatively easy on this webhost, because they allow you to compile things on their server. If your hosting service does not permit you to compile anything, you may still be able to use pre-built binaries, but things are a lot more complicated. See Installing and running Pylons as plain CGI with a no-frills hosting service.
Step 1: Set up Python so that you can install new site-packages within your user directory
On this webhost, you login to find that you have a directory called "/home". You do not have write access to /home itself, but underneath it there are various directories that you do have access to, including "public" (where you put your webpages), "private" (stuff that is only visible to you), and "protected" (stuff that both you and the webserver can access). I will be installing everything within "/home/protected".
I don't remember exactly where, but I think one of the install scripts wanted to install something in $HOME by default. So I begin by setting $HOME to where I want things to be installed. This'll also let us use "" as an abbreviation for $HOME. Note, however, that this environment setting will go away the next time we log in, and we'll have to type "/home/protected/" instead of "" (or make the environment setting permanent). Also, when I actually installed by Pylons project from an egg, I think it wasn't using the right interpreter until I set the PATH variable to point to this directory.
HOME=/home/protected
PATH=/home/protected/bin:$PATH
cd /home/protected
Now we install our own Python environment, so that we can install our own modules as a user:
wget http://peak.telecommunity.com/dist/virtual-python.py
python2.5 virtual-python.py
Test it:
Exit out of the Python interpreter, then:
Step 2: Install Pylons
wget http://peak.telecommunity.com/dist/ez_setup.py
./bin/python ez_setup.py
mv ez_setup.py bin/
./bin/easy_install Pylons
Test it:
~/bin/python -c 'import pylons'
Step 3: Set it up to run as CGI
Install wsgiref and test it:
~/bin/easy_install wsgiref
~/bin/python -c 'import wsgiref'
Make a file (I called it test.py, and put it in the "public" directory) containing something like this (replace the #!/home/protected with wherever you installed everything, and replace the config:/home/protected/test.ini with the path to your .ini file):
1
2
3
4
5
6 | #!/home/protected/bin/python
from paste.deploy import loadapp
wsgi_app = loadapp('config:/home/protected/test.ini')
import wsgiref.handlers
wsgiref.handlers.CGIHandler().run(wsgi_app)
|
Chmod it, so that the webserver can run it:
chmod a+x /home/public/test.py
That's it.
Now, just actually install your Pylons project itself, and setup the test.ini file. Point your webserver to test.py.
Addendum: Optional database stuff (MySQLdb and pysqlite)
MySQLdb
We gotta manually install MySQLdb because it can't be automatically installed by easy_install. Note that you'll have to go to http://sourceforge.net/projects/mysql-python
to get the actual download link to "wget" from, the following is just an example:
wget http://internap.dl.sourceforge.net/sourceforge/mysql-python/MySQL-python-1\.2.2.tar.gz
tar -xovzf MySQL-python-1.2.2.tar.gz
cd MySQL-python-1.2.2
~/bin/python setup.py build
~/bin/python setup.py install
cd ..
Test it:
~/bin/python -c 'import MySQLdb'
pysqlite
First, install sqlite (this webhost is FreeBSD, so in the example we install that from a binary package).
wget ftp://ftp4.freebsd.org/pub/FreeBSD/ports/i386/packages-5-stable/All/sqlite3-3.3.16.tbz
tar -tovzf sqlite3-3.3.16.tbz
rm -i *C*
rm -i *M*
Now we install pysqlite. I don't know how to tell easy_install to compile stuff using the /home/protected directory as a prefix to the includes, etc (strangely, just using '--prefix' didn't work), but we can manually download the package and then edit setup.cfg.
wget http://initd.org/pub/software/pysqlite/releases/2.3/2.3.3/pysqlite-2.3.3.tar.gz
tar xovzf pysqlite-2.3.3.tar.gz
cd pysqlite-2.3.3
emacs setup.cfg
Change the "include_dirs" and "library_dirs" lines of setup.cfg to this:
include_dirs=/home/protected/include
library_dirs=/home/protected/lib
Save, exit the editor, and do:
~/bin/python setup.py build
~/bin/python setup.py install
To test:
~/bin/python -c 'import pysqlite2'