Tag virtualenvwrapper

Installing Django With virtualenv on Dreamhost

Over on twitter, the subject of django on dreamhost came up between @bamana, @jtemple77 and myself. I had installed django on dreamhost late last year for andrew.io, took notes, and the following is how I did it.

Note: these instructions are loosly based off of the instructions for django in the dreamhost wiki.

Setup Domain and Database

If you don’t have a domain and database already setup, you’ll need to get them setup before continuing. This can be done from the dreamhost admin panel.


Visit manage domain panel and edit the domain you’re going to use for django. There are few important settings you’ll need to make sure are setup correctly. Under Users, Files and Paths:

  • select the user you want to run django under, and
  • define a web directory.

Re: the web directory, it needs to end with /public. This is what will enable Passenger on your domain and Passenger is the preferred way to run wsgi apps at dreamhost. Also, I have 7 domains on dreamhost and the naming convention I use for web directories is $HOME/www/<domain_name>/public. The rest of these instructions, assume you’re doing the same.

Next, under Web Options:

  • enable the checkbox next to Passenger


Visit the MySQL Databases Panel, create a database, and make note of the hostname, user, and password you use to create the database.

Install django from Dreamhost’s Setup Wizard

For the following steps, the location of the django-setup.py script should be in the public parent directory. Also you need to run this script with /usr/bin/python, which is dreamhost’s python default, it’s version 2.3.5, but you can change this later.

    $ cd ~/www/<domain_name>
    $ ls
    ./     ../      public/
    $ wget http://wiki.dreamhost.com/django-setup.py
    $ ls
    ./      ../     django-setup.py         public/       
    $ /usr/bin/python django-setup.py
    Looks like your domain is probably your_domain.com ... cool.
    Testing domain service...  lookin good
    What would you like to name your project?
    . . .
    . . .
    $ ls 
    ./      django-setup.py         project_name/
    ../     passenger_wsgi.py       public/

This will verify your installation, ask you questions, create a settings.py, secure your installation, and do your first syncdb. All this is not necessary, but it is convenient.

The most important thing this script does is create a boilerplate passenger_wsgi.py file. This python file does a lot of important things, including setting an environment variable for your project settings and telling passenger_wsgi where the application is.

At this point, because dreamhost’s setup wizard configured django admin, you should be able to visit http://your_domain.com/admin and see the django admin login screen. If so, congratulations, you’ve successfully installed and configured django on dreamhost.

You’re more or less ready to go.

Cool, but, uh, Python 2.3.5 is Kinda Old

Yep, python 2.3.5 is dated, and I’ve written previously about setting up python 2.6.4 alongside virtualenv and virtualenvwrapper on dreamhost. If you’ve followed those instructions, you can now setup a virtualenv running python 2.6 for your django project. (If you haven’t set up python 2.6 or virtualenv, either with my instructions or your own, I’d recommend doing so and continuing back here.)

Setup virutalenv and Install Required Packages

Note: this section assumes you’re using virtualenv and virtualenvwrapper. If not setup your python environment to your liking and continue to the next section.

    $ mkvirtualenv --no-site-packages <project_name>

Install any packages you may need for your project, eg:

    (project_name) $ easy_install pip
    (project_name) $ pip install mysql-python
    (project_name) $ pip install django
    (project_name) $ pip install markdown

Configure passenger_wsgi.py to Use Your Environment

After you setup your environment, change passenger_wsgi.py to use it by adding the following to the top (below the imports) of your passenger_wsgi.py file.

    INTERP = os.path.join(os.environ['HOME'], '.virtualenvs', 'project_name', 'bin', 'python')
    if sys.executable != INTERP:
        os.execl(INTERP, INTERP, *sys.argv)

I’m assuming you’ve your project environment is located at $HOME/.virtualenvs/project_name, if not change the INTERP path accordingly.

Now, django should be handling requests under a custom environment running python 2.6. You probably want to force a restart of things (see troubleshooting below…) Then visit http://your_domain.com/admin to verify you can still get to django’s admin.

At this point you should be able to copy your project to $HOME/www/<domain_name>/<project_name> However, make note of the differences in settings.py. You probably want to use the settings dreamhost’s django-setup.py script provided in places where conflicts exist.


If you are tweaking passenger_wsgi.py and don’t see your changes reflected, you need to kill existing python processes, and notify passenger of changes.

    $ pkill python
    $ touch ~/www/<domain_name>/tmp/restart.txt

Bonus: Disabling Passenger for other apps and directories

You should now have a django project being served from $HOME/www/<domain_name>/<project_name> but, what if you want to serve requests not handled by django, like a wordpress blog or a directory full of static resources? You could configure django’s urls.py or better yet, you could create an .htaccess file (if it doesn’t exist) in the directory you don’t want passenger_wsgi to handle. For example my wordpress blog is served from $HOME/www/<domain_name>/public/weblog. No request paths that start with weblog should be handled by django, that is wordpress’ job. So I added the PassengerEnabled Off directive as the first line of .htaccess in the weblog directory. eg:

    $ cd $HOME/www/<domain_name>/public/weblog
    $ vi .htaccess
    PassengerEnabled Off

Now any directory, where PassengerEnabled is Off will not be handled by passenger or django.

Copyright © andrew.io/weblog
andrew watts' weblog

Built on Notes Blog Core
Powered by WordPress