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.
Domain
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
Database
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.
Troubleshooting
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.txtBonus: 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 OffNow any directory, where PassengerEnabled is Off will not be handled by passenger or django.
Is there any chance you'd be willing to sell this domain?
probably not, but I'll listen to offers, feel free to contact me @ andrewwatts__AT__gmail__DOT__com
[...] it proved usefulx100 immediatly. You see, I was following another guide from andrew.io on installing Django with virtualenv on dreamhost and low and behold the guide said to “[i]nstall any packages you may need for your [...]
Thanks for the information. One doubt. What is the importance to create a virtual env to install python 2.6 ? Could be possible to request for dreamhost to update python version? I´m having this problem now, so I would like to know the best choices.
Thanks Alex
The last time I checked dreamhost did not install python 2.6
Hi! Thanks for a great instruction. However, I keep getting An error occurred importing your passenger_wsgi.py, precisely at the point of importing django.core.handlers.wsgi. I.e if I remove the line import django.core.handlers.wsgi passenger doesn't fail. import os, sys INTERP = os.path.join(os.environ['HOME'], '.virtualenvs', 'vpk', 'bin', 'python') if sys.executable != INTERP: os.execl(INTERP, INTERP, *sys.argv) cwd = os.getcwd() myapp_directory = cwd + '/vpknews' sys.stdout = sys.stderr sys.path.insert(0,myapp_directory) sys.path.append(os.getcwd()) os.environ['DJANGO_SETTINGS_MODULE'] = "vpknews.settings" import django.core.handlers.wsgi #this is where it fails
def application(environ, start_response): write = start_response('200 OK', [('Content-type', 'text/plain')]) return ["Hello, world!" + ' Running Python version ' + sys.version + '
']
Without django import I get
Hello, world! Running Python version 2.6.5 (r265:79063, Nov 9 2010, 14:38:02)
[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)]
It seems strange because command line python imports django.core.handlers.wsgi flawlessly.
$ python -V Python 2.6.5
I also get the same "An error occurred importing your passenger_wsgi.py" after following the instructions to the letter (also after doing pkill python and touch ..). Any fix to this yet?
By simply putting the "If sys.." and "INTERP…" on one line this seems to make things run smoothly:
Import sys, os
INTERP = os.path.join(os.environ['HOME'], '.virtualenvs', 'project_name', 'bin', 'python') if sys.executable != INTERP: os.execl(INTERP, INTERP, *sys.argv)
[the rest of your passenger_wsgi.py file goes here]
sounds like you may have had an indentation error.
Thank you so much for this valuable guidelines which have helped me tremendously! I have one question regarding serving static files with PassengerEnable=Off. I'm using the new static file support in Django for gathering all static files into static folder which is under public folder. This can easily result in many, many directories and subdirectories. It seems that I have to visit each and every one of those directories to create .htaccess file with PassangerEnable=Off. There must be a better way but I haven't found one yet.
Any ideas or suggestions?
Thanks again.
Hi
I am using dreamhost shared hosting and would love to run python (django or whatever).
I encounter this error while running visiting 'http://your_domain.com/admin' after configuring passenger_wsgi.py
'suexec policy violation: see suexec log for more details'
the full error log is [Mon Sep 19 12:39:16 2011] [error] [client 218.186.17.226] suexec policy violation: see suexec log for more details [Mon Sep 19 12:39:16 2011] [error] [client 218.186.17.226] Premature end of script headers: passenger_wsgi.py [Mon Sep 19 12:39:16 2011] [error] [client 218.186.17.226] File does not exist: /home/alesmana/<domain>/public/internal_error.html
I would appreciate any suggesions
thanks in advance
solved after moving passenger_wsgi.py
from /home/alesmana/<domain>/public/passenger_wsgi.py to /home/alesmana/<domain>/passenger_wsgi.py
quick question, where should i put passenger_wsgi.py btw
Thank you.
[...] This process was stolen primarily from Installing Django With virtualenv on Dreamhost. [...]
[...] a URL http://projeto.com.br/admin.ReferênciasDjango (Dreamhost Wiki)Passenger WSGI (Dreamhost Wiki)Installing Django With virtualenv on DreamhostCompartilhe:EmailPrint This entry was posted in Desenvolvimento and tagged deploy, django, [...]