Hostmonster + python 2.5 + subversion 1.4 + django-svn + mysqldb + fcgid

I’ve chosen Hostmonster as a shared hosting provider for one my projects using Django. I’ve been using hostmonster for this blog so I wanted to give it a try for more professional matters. The problem is that hostmonster is not django/python friendly. I’ve found the following problems:
- No subversion (svn) client.
- Old python without mysql support.
- Of course, no Django support.
As you can see:
$ python -V
Python 2.3.4
$ svn
-bash: svn: command not found
But not supported doesn’t mean impossible. You can have your python 2.5 and Django from subversion (And everything you want to install). You only need to install the software we need from scratch. If you want to know how, just read the rest of this entry.
1. Getting started.
Let’s make a summary of the steps we have to accomplish. But before starting you’ll need ssh access to hostmonster. So go to your control panel, request the service and add your public key (or generate a new pair).
Then you will download (hostmonster provides wget) and configure the following software:
- Python: the version provided is old.
- Svn:to use the svn version of djang, and perhaps to use the svn version of your application.
- Django from the svn repository and related python modules.
- All the python modules you need. Like MySQLdb, setuptools and others.
Every piece of software will be installed in your home folder. You’re not root on the shared server so you have to keep your things in your home directory. You will create different folders for this purpose.
2. Python
So let’s start with the easy one. You need to retrieve the python source code to be able to compile it in hostmonster and have our precious 2.5.x python there (2.5.2 at the time of writing this post) . From your home directory (the one you get when you enter through ssh) type:
$ wget http://www.python.org/ftp/python/2.5.2/Python-2.5.2.tar.bz2
$ tar xjf Python-2.5.2.tar.bz2
Now you need to create a directory to install python in it. (reminder: we’re not root, we cannot install it in the system directories!).
$ mkdir python
And then the final step: compile and install. You have to configure the python source to be installed in the directory we’ve created before. Also during the configure and make install steps you’ll see a lot of information. Just pay attention when configure and make install finish. If something went wrong it will be reported at the end (I mean, when the command stops).
From your home folder execute the following:
$ cd Python-2.5.2
$ ./configure –prefix=$HOME/python
$ make
$ make install
Everything seems perfect but, do you want to check if your new python works? No problem:
$ cd
$ python/bin/python
Python 2.5.2 (r252:60911, May 20 2008, 09:22:13)
[GCC 3.4.6 20060404 (Red Hat 3.4.6-9)] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.
>>>
The last step is to configure your shell to use our python installation by default. This can be done updating the path in the .bashrc file. This file is located in your home folder (the one you’re when you enter in your account, or when you type just cd). Edit this file with vim and add the following line in the end:
PATH=~/python/bin:$PATH
Notes:
- Do not use $HOME here, use ~ instead.
- You need to reload the contents of .bashrc. You can just disconnect and connect again. Or type:
source .bashrc
After that, type python to see if your changes have been applied and you’re executing the 2.5 version.
You’re ready for the next step. But before that, you have to clean a bit the mess. This mean to remove the python source code. We won’t use it again.
$ rm Python-2.5.2.tar.bz2
$ rm -rf Python-2.5.2/
3. SVN
Now it’s time for Subversion. You install subversion in second place because it will build a python extension and it’s better to do it with your new installation than with the old one. The first step, as before, is to download, unpack and create a destination directory:
$ wget http://subversion.tigris.org/downloads/subversion-1.4.6.tar.bz2
$ wget http://subversion.tigris.org/downloads/subversion-deps-1.4.6.tar.bz2
$ tar xjf subversion-1.4.6.tar.bz2
$ tar xjf subversion-deps-1.4.6.tar.bz2
$ mkdir svn
Then configure the source code and install in the directory we’ve created. I’ll explain the options here:
- –prefix: tells “make install” to install subversion in the directory we type there.
- –with-expat=builtin: There is a problem linking with the expat library provided by hostmonster. The library apr-utils (included in subversion deps) have problems with the original library, so we use the one included in apr-utils.
- –with-ssl: For https support.
- –with-pic: compiles all the code with support for “Position Independent Code”. If not, you’ll have problems linking with some libraries in hostmonster.
Let’s proceed with:
$ cd subversion-1.4.6
$ ./configure –prefix=$HOME/svn –with-expat=builtin –with-pic –with-ssl
$ make
$ make install
Is Subversion working?
$ cd
$ svn/bin/svn –version
svn, version 1.4.6 (r28521)
compiled May 20 2008, 09:47:21
…
Also as before, you need to access the subversion client you just compiled. For that you’ll update the .bashrc file. The new PATH line will be (sorry for making you update the file twice):
PATH=~/python/bin:~/svn/bin:$PATH
Do not forget to reload your .bashrc:
source .bashrc
And in the end, clean a bit the mess.
$ rm subversion-1.4.6.tar.bz2
$ rm subversion-deps-1.4.6.tar.bz2
$ rm -rf subversion-1.4.6/
4. Extra python modules.
Before continuing with Django, you need some extra python modules for it like mysqldb and flup (for fastcgi). There is a really good tool called easy_install from setuptools. With this tool we can install easily almost any package. You’ll use it here to install the ones needed by Django.
First download the installation package with:
$ wget http://peak.telecommunity.com/dist/ez_setup.py
And then install with:
$ python ez_setup.py
After that you can remove ez_setup.py:
$ rm ez_setup.py
Now let’s install the modules we need: flup and mysqldb. The script easy_install is in the same folder as your python binaries. If you have added the path to the .bashrc and loaded the file or re-entered again in your shell. Just type:
$ easy_install MySQL-python
$ easy_install flup
To test the installation, just import the libraries in a python shell:
$ python
Python 2.5.2 (r252:60911, May 23 2008, 08:23:55)
[GCC 3.4.6 20060404 (Red Hat 3.4.6-9)] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import MySQLdb, flup
5. Django from subversion.
Here you can follow the steps from the django documentation about installing from subversion. But in few lines, these are the steps.
First checkout Django from the subversion in your home folder:
$ svn co http://code.djangoproject.com/svn/django/trunk/ django-trunk
Then tell python to use the django module we’ve just downloaded:
$ cd python/lib/python2.5/site-packages/
$ ln -s ../../../../django-trunk/django
And the last step is to modify (again) our .bashrc to add the django utilities to our path. The ultra updated PATH line will look like this:
PATH=~/python/bin:~/svn/bin:~/django-trunk/django/bin:$PATH
Do not forget to check if everything is working:
$ cd
$ python -c “import django;print django.VERSION”
(0, 97, ‘pre’)
6. Running as Fast CGI.
You have everything ready in your account to run django applications. You can just follow the tutorial from Django about running Django in FastCGI mode.
There is only one thing you need to care about. In host monster instead of mod_fastcgi there is fcgid. It provides the same functionality, but it changes the AddHandler line in your .htaccess to:
AddHandler fcgid-script .fcgi
So your .htaccess file can look like this:
AddHandler fcgid-script .fcgi
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ mysite.fcgi/$1 [QSA,L]
For the mysite.fcgi you have to have in mind two things:
- Do not put /usr/bin/python , use /yourhome/yourusername/python/bin/python (Change yourhome and yourusername for the values you obatin from executing “echo $HOME”).
- You have to insert the path of the root folder of your application. Example: if your application is in /home2/john/mydjangoproject/. Use
sys.path.insert(0, “/home2/john/”)
os.environ['DJANGO_SETTINGS_MODULE'] = “mydjangoproject.settings”
As an example, your mysite.fcgi can look like this:
#!/home/pictumania/python/bin/python
import sys, os
sys.path.insert(0, “/home/pictumania”)
# Set the DJANGO_SETTINGS_MODULE environment variable.
os.environ['DJANGO_SETTINGS_MODULE'] = “pictuapp.settings”
from django.core.servers.fastcgi import runfastcgi
runfastcgi(method=”threaded”, daemonize=”false”)
7. In the end…
That’s all. I hope It helped you if you tried to do something similar or exactly this. If you have any questions, just put them as a comment and I’ll do my best to answer them, but I’m not a Django expert.
Also I hope you didn’t cry much reading my poor english grammar.
![]()
Tags:.htaccess,django,easy_install,Hostmonster,mysqldb,python,shared hosting,subversion »
Trackback URL: http://fitri.manzanisimo.net/2008/05/23/hostmonster-python-25-subversion-14-django-svn-mysqldb-fcgid/trackback/
Vizualbod said,
2008-06-10 @ 11.19 pm
Where could I find the .bashrc file you’ve mentioned. Google is silent.
graffic said,
2008-06-10 @ 11.35 pm
It’s in your home directory. When you enter in your account via ssh, in that moment you’re in your home directory. If you moved to other places you can type on of these commands:
cd (without anything else)
cd $HOME
cd ~
If you do a normal “ls” you won’t see it, because all the files that start with a dot, in unix, are “hidden”. They’re just normal files but they’re considered hidden by 95% of the software.
Vizualbod said,
2008-06-10 @ 11.37 pm
Found it in “/etc/”. I am noob on redhat if anyone was wandering.
Vizualbod said,
2008-06-10 @ 11.38 pm
Aah, Great thanks. Ignore the above. Anyway, Vi is a great fun
graffic said,
2008-06-10 @ 11.42 pm
The one in /etc/ is the system wide “bashrc”. Usually is owned by the administrator and you cannot change it.
About the editor, you can use nano. I’m sorry to talk always with vi/vim in mind, but once you get used it’s like your underwear: you use it always except during sex
Xin said,
2008-06-24 @ 12.32 am
Hi!
I have followed your method; but I still cannot see the run results.
When I type: http://django.mattsonmaps.com/
it will show a login page of hostmonster. My project name is django, the subdomain is django.
Thank you!
Xin said,
2008-06-24 @ 7.37 pm
Do not worry!
I have figured out.
I have missed a space in the .htaccess file
graffic said,
2008-06-26 @ 7.28 am
@Xin: Wow, really fast. I wasn’t able to check it
At least I hope you’ve found this howto useful.
skreeves said,
2008-06-26 @ 2.40 pm
Very useful writeup! Thanks!
dragon64 said,
2008-07-11 @ 3.39 am
Excellent job. Keep it up.
jcmanjar said,
2008-07-30 @ 8.11 pm
Very useful. I spend days trying to install mysqldb.
Thanks a lot
tito said,
2008-08-05 @ 2.51 am
thanks. great post. exactly what i needed.
unfortunately hostmonster does not support python, but everything works perfectly well.
keep up the good work.
cheers
Tito said,
2008-08-05 @ 6.38 pm
Although everything works fine, i have noticed that it takes ages for changes in the configuration files, for instance, urls.py or views.py, to take effect.
Is there any way to clear the cache? clearing my browser’s cache makes no effect. Hostmonster support team say they do not use any cache mechanism. Running a touch to the fcgi file doesn’t result either. Only after a long time the chances start functioning.
If i execute the page from the command line: #python manage.py runfcgi
i get the following warning:
WSGIServer: missing FastCGI param REQUEST_METHOD required by WSGI!
WSGIServer: missing FastCGI param SERVER_NAME required by WSGI!
WSGIServer: missing FastCGI param SERVER_PORT required by WSGI!
WSGIServer: missing FastCGI param SERVER_PROTOCOL required by WSGI!
any suggestion is welcome.
Tito
graffic said,
2008-08-05 @ 10.06 pm
@Tito: I’ll start from the end. Do not run it in fcgi mode manually, it’s not made to run in that way.
Now to the juciy thing. Quick: In order to restart your django application, just “touch mysite.fcgi”. In fact do something that changes the “last modification” date: edit it, upload a new version, etc.
Why? In FCGI mode there is a process running to serve the requests. The part “fast” in FCGI means that you don’t have to relaunch the process in every request. You launch it once and it stays there fore some time/some requests. That’s the reason it takes “ages” to reload the configuration.
This “should” work, but you never know
Tito said,
2008-08-05 @ 10.34 pm
thanks graffic,
unfortunately, “touch mysite.fcgi”, doesn’t work. continue to take ages, two or three hours, for the changes to take effect.
i haven’t mentioned before but i had to put the “mysite.fcgi” in the folder cgi-bin for django to work. in the root folder of the project it doesn’t get executed. and a question, does the fcgi file have to be named “mysite.fcgi” or can it be anything?
the fcgi daemon is part of the hostmonster server and only the administrator can stop/start it, right?
cheers.
Fred Chu’s Blog » links for 2008-08-19 said,
2008-08-20 @ 4.32 am
[...] Το φιτρί αγόρι I’ve chosen Hostmonster as a shared hosting provider for one my projects using Django. I’ve been using hostmonster for this blog so I wanted to give it a try for more professional matters. The problem is that hostmonster is not django/python friendly. I’ve found the following problems: [...]