Trinh Hoang Nhu Trinh Hoang Nhu - 1 month ago 6
Python Question

Update new Django and Python 2.7.* with virtualenv on Dreamhost (with passenger)

Dreamhost is a great host for small project. And it's also Django friendly hosting. Everything good except python and Django version is a little bit out of date. Well it's a whole day of work to figure out how to update Python 2.7.3, Django 1.4 on dreamhost and I really want to share with whoever finding it

Answer

I currently have private server, a shell account and a bit of luck. So here is what I do:

  1. SSH to your host to upgrade python

     cd ~
     mkdir tmp
     cd tmp
     wget http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tgz
     tar zxvf Python-2.7.3.tgz
     cd Python-2.7.3
     ./configure --enable-shared --prefix=$HOME/Python27 --enable-unicode=ucs4
     make
     make install
    
  2. Configure system to use our new Python. Open ~/.bashrc and add the following line

     export PATH="$HOME/Python27/bin:$PATH"
     export LD_LIBRARY_PATH=$HOME/Python27/lib
    
     #save it and run
     source ~/.bashrc
    

    you can now check your python version with which python

  3. Install easy_install, pip

    cd ~/tmp
    wget http://peak.telecommunity.com/dist/ez_setup.py
    python ez_setup.py
    easy_install pip
    
  4. Install virtualenv

     pip install virtualenv
     virtualenv $HOME/<site>/env
     #Switch to virtualenv
     source $HOME/<site>/env/bin/activate
    

    you can also add env path to bashrc

     export PATH="$HOME/<site>/env/bin/:$PATH"
     source ~/.bashrc
    
  5. Install django and everything else

     pip install django
     pip install ....
     pip install ....
     pip install ....
    
  6. Create project

     cd $HOME/<site>/
     python $HOME/<site>/env/bin/django-admin.py startproject project
    
  7. Create passenger_wsgi.py in HOME/<site>/ with following content

     import sys, os
     cwd = os.getcwd()
     sys.path.append(cwd)
     sys.path.append(cwd + '/project')  #You must add your project here or 500
    
     #Switch to new python
     #You may try to replace $HOME with your actual path
     if sys.version < "2.7.3": os.execl("$HOME/<site>/env/bin/python",
         "python2.7.3", *sys.argv)
    
     sys.path.insert(0,'$HOME/<site>/env/bin')
     sys.path.insert(0,'$HOME/<site>/env/lib/python2.7/site-packages/django')
     sys.path.insert(0,'$HOME/<site>/env/lib/python2.7/site-packages')
    
     os.environ['DJANGO_SETTINGS_MODULE'] = "project.settings"
     import django.core.handlers.wsgi
     application = django.core.handlers.wsgi.WSGIHandler()
    

If you are using django 1.7, replace the last two line with

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
  1. Enjoy :D
Comments