Python on DreamHost: Part 1 - Custom Installation

This blog is somewhat of a lark for me and I use it to play around with things that I do not have the luxury to try out at the office. Its written in Python and I host it on DreamHost (disclosure: affiliate code on that link). I am on a shared server and the version of Python running there is very out of date, to say the least. Here is a quick guide to get everything up and running with Python 3.4 (or whatever version you want).

Install Custom Version of Python

This is covered in the DreamHost Python Wiki, but I will run though it again here. My server had 2.6.6 installed on it, but I wanted 3.4.1. To do this you'll need to log into your DreamHost panel and create a shell account. Then log into your server and run the following commands:

$ wget --no-check-certificate
$ tar -xzvf Python-3.4.1.tgz
$ cd Python-3.4.1
$ ./configure --prefix=$HOME/.python3.4
$ make
$ make install

This installs a custom version of Python but leaves the DreamHost installation as the default. To me this is preferable because it allows multiple versions to exists side by side. I personally have custom installations of both 2.7.8 and 3.4.1 alongside the default 2.6.6. You can control the active version on each site you deploy using virtualenv.

At this point you can remove the installation files, if you want to clean up.

$ cd ..
$ rm Python-3.4.1.tgz
$ rm -r Python-3.4.1

Create a Virtualenv

If you deploy your application now it will still be running on 2.6.6, so we need to set up a virtualenv using the newly installed Python distribution. Virtualenv has a parameter, -p, that lets one specify the python executable to use.

As an aside, if you went with an older version of Python, like 2.7, you'll need to install pip and virtualenv before you can proceed. This can be done simply:

$ cd $HOME/.python2.7/bin
$ wget --no-check-certificate
$ ./python
$ rm
$ ./pip install virtualenv
$ ./virtualenv -p $HOME/.python2.7/bin/python2.7 $HOME/venv
$ cd $HOME/venv
$ source bin/activate

If you installed 3.4, pip and virtualenv should already be present. You can skip the above and create the virtualenv this way:

$ cd $HOME/.python3.4/bin
$ ./virtualenv -p $HOME/.python3.4/bin/python3.4 $HOME/venv
$ cd $HOME/venv
$ source bin/activate

If you plan on hosting multiple sites or having multiple virtualenvs, I suggest you organize your distributions into a common directory and pick a more descriptive name than venv. Either way, you can now check that virtualenv is using the correct Python version and then pip install any packages you need for your configuration. I need Flask and Markdown. When complete, exit the virtualenv using the deactivate command.

Configure Passenger

DreamHost recommends using Passenger for WSGI compliant Python applications. Mixt Meta uses Flask, but this also applies for Django and many other Python frameworks. Passenger must be enabled in the DreamHost Panel and the web directory must point to a ./public folder in your virtualenv.

DreamHost Panel

Next, configure and add a to your $HOME/venv directory. It should indicate the virtualenv python executable as follows:

import sys, os
INTERP = os.path.join(os.environ['HOME'], 'venv', 'bin', 'python')
if sys.executable != INTERP:
    os.execl(INTERP, INTERP, *sys.argv)
from app import app as application

Deploy your Python package code to $HOME/venv/app and, if you are using Flask like me, place your static files in the $HOME/venv/public directory. There is another DreamHost Wiki with a lot of additional information regarding the file, including instructions on how to configure logging and handle certain HTTP errors.

Lastly, to restart the application once deployed, run the following:

$ cd $HOME/venv
$ mkdir tmp
$ touch tmp/restart.txt

Launch up the ol' browser and you should be good to go.

comments powered by Disqus