Tutorial: Installing Django on (mt) Media Temple DV 3.5 server with mod_python

Posted 2 years ago in Django

When I first set out to install Django on my Media Temple server running under mod_python, I ran into a few gotchas. Luckily, there are lots of people who've tried the same thing and were successful. Unfortunately, however, those tutorials and discussions are scattered all over the place, and there really is no definitive guide to installing Django on a (mt) Media Temple DV. Thus, I'll attempt to do just that, from start to finish.

Root access and Developer Tools package

First thing to do is make sure you've got root access to your server and that you have the (mt) Developer Tools package installed.

You can enable root access and Developer Tools on your DV by going to your (mt) account center, select the server, and select 'Root Access and Developer Tools'. From there, you can switch on root access, as well as the Developer Tools package.

Among other things, the Developer Tools package installs Subversion, which we'll use to grab the latest Django release.

Once you've got root access, you're ready to proceed with installation. Here's a high-level look at what we'll be doing:

  1. Download Django
  2. Install Django
  3. Start a Django project
  4. Serve the project through Apache

First, SSH to your server. Switch to the root user with the password you setup in the account center, with the following command:

You should see something like this:

Download Django

You'll want to install Django into '/var', so switch to that directory:

Once you're there, download Django from djangoproject.com with the following command:

This command will use Subversion to checkout the latest trunk release of Django. This may change in the future as Django approaches a 1.0 release, as trunk checkouts are generally considered unstable.

Install Django

Next, you'll need to install Django. The installation process essentially allows Python to see your Django checkout. Django is technically nothing more than a bunch of Python modules, so installation is simple. Python simply needs to have the Django directory accessible from the PythonPath. Django ships with an installation script to copy your checkout into the Python packages directory, but this eliminates your ability to run periodic Subversion updates on the Django checkout, since Django is still not yet a 1.0 release. Thus, we simply create a symlink from our checkout to the Python packages directory. The following code will do that for you from within the '/var/' directory:

If nothing happens, it was successful. You can test whether or not Python is ready to use Django by hopping into the Python interactive shell by typing the command at any directory other than the parent directory of the 'django-trunk/django' directory (because Python inherits the path you're currently in):

You should get:

Type the following at the Python prompt:

then:

You should see something similar to this:

The version numbers may be different, but that's fine. As long as you didn't see an import error, you've successfully installed Django and can begin building applications.

Start a Django project

Now you need to start your first Django project. Django provides a handy script that starts a base project for you. This script lives within your Django checkout, but for simplicity's sake, it'll be easier to put this script on your system path so you don't need to use the full path to your Django checkout everytime you want to start a new project. Run the following command from your '/var' directory to put the script on your path:

Cool. Let's say you want all of your Django projects to live within one directory ('/var/django-projects'). Head over to '/var' and run the following command:

Now create a project within that directory with the following command:

That's it. The Django script will generate a directory within django-projects titled 'mysite' which has some basic code to get you up and running with your first application.

Serve the project through Apache

Django comes with a really handy development server, but it is not intended for use as a production server. Thus, the easiest thing to do is to get Apache to server your Django application with mod_python. While mod_python is quite old, and is not the fastest and most preferred method of hosting your Django apps on Apache, it is currently the recommended solution.

Your DV 3.5 server should already have mod_python installed and working under Apache. There's nothing you need to do to get that running.

You do, however, need to let Apache know that you'd like it to serve a Django application somewhere. You'll do this in your vhost directives.

You'll need to be logged in as root to modify your vhost directives, so go ahead and do that, and navigate to your domain's directory. It'll be something like this:

Once in there, navigate to the 'conf' directory for that domain.

Inside of that directory, you'll see this:

Don't touch that. That's a file that's generated from Plesk, and if you modify it, you'll lose your changes the next time Plesk updates. Instead, create a file named 'vhost.conf'. Plesk looks for this file in every 'conf' directory when it updates to include that file if there is one. Thus, you can make all of your changes here, and you'll be good to go.

Within your newly created 'vhost.conf' file, enter this:

These directives will do a few things:

  1. Tell Apache to let mod_python handle all of the requests to '/' for 'domain.com' (because we're in /var/www/domain.com).
  2. Tell Django where to find the settings file for the Django application. We use mysite.settings here because we used django-admin.py to start the project 'mysite', and 'settings.py' lives within that directory.
  3. Turn the Python debugger on - you'll want this off for production sites (it basically enables detailed error messages).
  4. Set a unique id for the PythonInterpreter. This is needed if you plan to power multiple applications under mod_python (I think).
  5. Tell Apache to not use mod_python for any files under the '/media' directory (or any file ending with .jpg, .gif or .png). This is because we'll want to serve real files under this directory - and mod_python shouldn't touch them.

Once you've saved that file, you need to restart Plesk / Apache so that it will detect this file and process it for directives. On your (mt) DV 3.5, run the following command to restart:

Go ahead and load up the domain that you've pointed to use Django, and, with any luck, this will be what ya get:

It worked!

My headache

Now, when I first attemped to get Django running under mod_python, Python wouldn't recognize Django. This made me crazy for about two days, until I realized I had installed Django into my home folder, which Apache was not allowed to read. Sigh.

So that's about it, have fun!

25 Comments

2 years ago

Check out mod_wsgi in daemon mode :)

2 years ago

Thanks, Nathan. I know mod_wsgi is the new hotness, and I'll be poking around with it on my (mt) server shortly.

Nick

Anonymous
2 years ago

Thank you sooo very much for this post. I struggled last night deploying a django site on a very cheap budget Plesk host. This morning I came in and had things running within 5 minutes, thanks to your instructions.

This is also not a bad way to deploy: it sure beats running django under fcgi through htaccess, which seemed like a hack and didn't work for me either.

Thanks again.

2 years ago

Glad to have helped!

Nick

2 years ago

This blog post was a huge help! Thank you so much. I am still having one last challenge. For some reason none of my media files are being served.

These are my media settings...
MEDIA_ROOT = '/var/www/vhost/pauldzurec.com/subdomains/media/httpdocs'

MEDIA_URL = 'http://media.pauldzurec.com'
Those are both pointing to the same directory that has my images.

This url shows that I've got my subdomain working.
http://media.pauldzurec.com/logo.gif

but my django page does not show this image...

django is loading the page completely except for images (css is contained within the html file)
www.pauldzurec.com/home

Given the information i have provide (which probably is insufficient) do you have any idea why images won't show up?

Thanks considering my question.

1 year ago

Hi Nick,
I have a question about this short installation tutorial. Im currently on mediatemple dv dedicated server 3.5. I follow all your instruction and all goes ok until I want import django.

this is the message i become in to the shell terminal:

>>> import django
Traceback (most recent call last):
File "", line 1, in ?
ImportError: No module named django
>>>

Im currently learning, what can i do to going forward ? can you help me ? thank you and regards.

1 year ago

Maurizio,

You're probably seeing the import error because Django is not installed correctly, and thus, not on your PythonPath.

Are you sure you followed this step:

ln -s `pwd`/django-trunk/django /usr/lib/python2.4/site-packages/django

... and that django-trunk is within the present working directory?

Nick

Charlie
1 year ago

Hi Nick,

First off, thank for the tutorial. I have a couple questions though:

I found your tutorial a little late, so the first time I installed I ran into the root problem you did (python_egg_cache issues). I thought I uninstalled and then reinstalled from /var/, but I'm still getting that stupid error that I have to set an environment variable to fix. Could you tell me how you repaired your installation?

Also, I've been having some trouble with sqlite3 on the dv server. It might be tied to the problem above, but I get an Error loading pysqlite2 module: /tmp/pysqlite-2.5.0-py2.4-linux-i686.egg-tmp/pysqlite2/_sqlite.so: everytime I try to syncdb with a sqlite3 db.

Sorry for the spam comment, thanks again!
-Charlie

1 year ago

Charlie,

Not sure what root problem you're talking about - could you explain further?

Also - I haven't experimented with SQLite3 yet, so I probably wouldn't be much help with that either.

Sorry!

Nick

Charlie
1 year ago

Hey Nick,

Well, I switched to MySQL as the DB store, and all seems well now. Whenever I try sqlite3 on the DV, it gives me a pysqlite2 error. I can get going with MySQL, but if anybody else has a DV server from (mt), give a sqlite3 project a try and let me know if it works for you.

Thanks again, Nick. You saved me a weekend of apache hacking.

-Charlie

1 year ago

Charlie,

Ahh, weird! Sounds like you might need an additional Python library.

Glad you got it working!

Nick

1 year ago

Hi Nick,

i just wanted to thank you. Your post helped me a lot to simplify the installation process. It is strange how we always tend to complicate things...

Thanks again.

Cheers,

Diego.

1 year ago

It took me all day, but after several attempts, I was able to successfully install Django!

Some of the changes I had to implement was the use of Python2.3, and get Python to recognize site-packages/django as a valid module source folder.

I essentially had to reference several guides. I'm so happy to finally have it up and running though! HURRAY!

Favourite Guest
last year

got it working thanks a million... however I symlinked the paths which i might add worked out great but i received this error message when trying to create a new project:

"-bash: django-admin.py: command not found "

I am able to create a new project when enter the full path name to my trunk folder

"/var/django-trunk/django/bin/django-admin.py startproject mysite"

I was wondering if I was doing something wrong with the symlink....thanks in advance

last year

@Favourite Guest,

Make sure you were in the directory that had 'django-trunk' inside of it when you ran the symlink code.

Nick

Favourite Guest
last year

@Nick...thanks again

I followed your steps and went into the 'django-trunk' directory and ran:

ln -s `pwd`/django-trunk/django/bin/django-admin.py /usr/local/bin

Response was:

ln: creating symbolic link `/usr/local/bin/django-admin.py' to `/var/django-trunk/django-trunk/django/bin/django-admin.py': File exists

last year

If you're in the django-trunk directory already, you should run:

ln -s `pwd`/django/bin/django-admin.py /usr/local/bin

(note I removed /django-trunk).

You'll have to remove the old symlink first:

rm /usr/local/bin/django-admin.py

Nick

Favourite Guest
11 months ago

Thanks Nick everything seems to be working.

11 months ago

I tried to follow the steps and I got a following error:

Mod_python error: "PythonHandler django.core.handlers.modpython"

Traceback (most recent call last):

File "/usr/lib64/python2.4/site-packages/mod_python/apache.py", line 299, in HandlerDispatch
result = object(req)

File "/usr/lib/python2.4/site-packages/django/core/handlers/modpython.py", line 228, in handler
return ModPythonHandler()(req)

File "/usr/lib/python2.4/site-packages/django/core/handlers/modpython.py", line 191, in __call__
self.load_middleware()

File "/usr/lib/python2.4/site-packages/django/core/handlers/base.py", line 33, in load_middleware
for middleware_path in settings.MIDDLEWARE_CLASSES:

File "/usr/lib/python2.4/site-packages/django/utils/functional.py", line 269, in __getattr__
self._setup()

File "/usr/lib/python2.4/site-packages/django/conf/__init__.py", line 40, in _setup
self._wrapped = Settings(settings_module)

File "/usr/lib/python2.4/site-packages/django/conf/__init__.py", line 75, in __init__
raise ImportError, "Could not import settings '%s' (Is it on sys.path? Does it have syntax errors?): %s" % (self.SETTINGS_MODULE, e)

ImportError: Could not import settings 'proteus.settings' (Is it on sys.path? Does it have syntax errors?): No module named proteus.settings

11 months ago

@Kory,

Looks like your project isn't on the Python path - what's your Apache vhost look like?

Nick

11 months ago

Thank you for your help.
I created /var/django-projects/ folder as well
When I run django-admin.py startproject proteus on "mysite"
It created all the files on "mysite"/proteus folder
This is my vhosts settings:

SetHandler python-program
PythonPath "['/var/django-projects'] + sys.path"
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE proteus.settings
PythonDebug On
PythonInterpreter proteus

SetHandler None

SetHandler None

11 months ago

It looks like all my / is disapered when I inserted here

11 months ago

<Location &34;&47;&34; >

11 months ago

It is looks like all my <Location "/" > is got cut out

11 months ago

Yes you were right! it was not in the python path!
All works great!

Thanks!

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]".

More information about formatting options