Configure Mod_Python fo Django on Apache server on Ubuntu

62 views
Skip to first unread message

HangingClowns

unread,
Apr 5, 2008, 5:53:27 PM4/5/08
to Django users
I should have the latest versions available for Apache and mod_python
cause I just put those onto my server about 2 weeks ago. I'm having
trouble understanding how to edit the Apache config file for Django. I
currently use the code snippet below for my settings in Apache. And
this is what the error looks like:

http://67.207.140.149/mysite/

Can someone help me out?

I have started a project in the /home/webmastr/public_html/django
called mysite, cause I'm following the tutorial on Djangoproject.org.
I did not find their instructions for configuring modpython to be very
helpful for me. So, back to subject, within that django folder, is a
folder called "mysite" with all of the beginning project python .py
files.

<Location "/mysite/">
SetHandler python-program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE mysite.settings
PythonDebug On
PythonPath "['/home/webmastr/public_html/django'] + sys.path"
</Location>

Michael

unread,
Apr 5, 2008, 6:08:26 PM4/5/08
to django...@googlegroups.com
Simply django can't find your settings.py file. This could be a few things but based on where the file is located my bet is that the apache process (most likely www-data on ubuntu) doesn't have permissions to access your settings.py file. Give www-data permissions to access the file and you should be good to roll,

Michael

HangingClowns

unread,
Apr 5, 2008, 7:27:03 PM4/5/08
to Django users
What's the syntax for that? Is that like that chmod?

On Apr 5, 5:08 pm, Michael <newmani...@gmail.com> wrote:
> Simply django can't find your settings.py file. This could be a few things
> but based on where the file is located my bet is that the apache process
> (most likely www-data on ubuntu) doesn't have permissions to access your
> settings.py file. Give www-data permissions to access the file and you
> should be good to roll,
>
> Michael
>

Will Boyce

unread,
Apr 6, 2008, 3:23:11 AM4/6/08
to Django users
Assuming your django project (mysite) is in /home/webmaster/
public_html/django/ then you'll want a trailing "/" in your
PythonPath.

Hope this helps.

Graham Dumpleton

unread,
Apr 6, 2008, 3:32:36 AM4/6/08
to Django users


On Apr 6, 5:23 pm, Will Boyce <wrbo...@gmail.com> wrote:
> Assuming your django project (mysite) is in /home/webmaster/
> public_html/django/ then you'll want a trailing "/" in your
> PythonPath.

No you don't. Trailing slash shouldn't make any difference.

What gives you that idea?

Graham

ke...@securitynow.us

unread,
Apr 6, 2008, 8:03:27 AM4/6/08
to django...@googlegroups.com
Exact same issue here. Tried everything listed. Any other ideas or possibly a sample config for httpd.conf on ubuntu.

Thanks in advance for your help

-----Original Message-----
From: Will Boyce <wrb...@gmail.com>
Sent: Sunday, April 6, 2008 3:23am
To: Django users <django...@googlegroups.com>
Subject: Re: Configure Mod_Python fo Django on Apache server on Ubuntu


Assuming your django project (mysite) is in /home/webmaster/
public_html/django/ then you'll want a trailing "/" in your
PythonPath.

Hope this helps.

Michael

unread,
Apr 6, 2008, 10:52:06 AM4/6/08
to django...@googlegroups.com
Hey gang;

Wow a lotof people are all having the same problem. Somehow the conversation I had with the original poster got taken off the list. Here is the next message I sent:

chmod would change the permissions, but hede a little caution here; opening up your home directory allows everyone to get inside there. This might not be an issue with your server if you don't have anyone on it who you might not be able to trust, but it isn't a bad idea to get in the right habits here.

What I would recommend if you are a web developer, is to add yourself to the group www-data. So in a shell punch in usermod -a -G www-data webmastr (I am assuming webmastr is your linux login name here). This will let you just give permissions to people in your group instead of everyone. Then go back to /home/webmastr/ and type chmod 750 -r public_html . This will give your full read-write-executable access to the files (7), your group including www-data read-execute permissions (5) and everyone else no permissions (0).

Hopefully that will get your django install to work. If you want to learn more about linux permissions look here: http://www.freeos.com/articles/3127/ or do a search for ubuntu permissions. There are tons of resourses. Let me know how that works,

Then it became clear that he didn't own that file and I told him to look into 'chown' to own the file.

There are four parts to an django site you need to remember exist when setting it up. You need a media root, where files are served directly by apache. I always use /media/ as an example. Then you need a project folder that is where you put all of you python files. The important file here is settings.py because it tells mod_python (the apache module for python) what to load and that is how django starts up. The third part is a templates directory, which is where all the django templates go. The last part is the SQL we won't get into that here. The media and the templates folders need to be defined by you inside of settings.py. Apache (generally www-data) should be given permission to read and execute these files.

You should make sure django is in your python path. This is simple open up a python shell and type 'import django'. make sure your project is in a python path. This is done with the PythonPath in the config file. Other than that read up on how apache configuration works and the django and mod_python docs work. I promise they all work on Ubuntu without problems.

Here is an example VirtualHost that I have included from my main apache.conf file: http://dpaste.com/43421/

I hope that helps you all:

Michael

HangingClowns

unread,
Apr 6, 2008, 11:23:12 AM4/6/08
to Django users
I've worked on putting Django on a Red Hat machine, and I've noticed
that the Ubuntu version of Apache is a bit different. But, in any
case, In my /etc/apache2 httpd.conf I have:

<IfModule dir_module>
DirectoryIndex index.html index.php
</IfModule>

<Location "/mysite/">
SetHandler python-program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE mysite.settings
PythonDebug On
PythonInterpreter mysite
PythonPath "['/home/webmastr/public_html/django']+sys.path"
</Location>

And that's it. Excluding the /media, it should still show something,
I'd believe, or am I wrong?

On Apr 6, 9:52 am, Michael <newmani...@gmail.com> wrote:
> Hey gang;
>
> Wow a lotof people are all having the same problem. Somehow the conversation
> I had with the original poster got taken off the list. Here is the next
> message I sent:
>
> chmod would change the permissions, but hede a little caution here; opening
> up your home directory allows everyone to get inside there. This might not
> be an issue with your server if you don't have anyone on it who you might
> not be able to trust, but it isn't a bad idea to get in the right habits
> here.
>
> What I would recommend if you are a web developer, is to add yourself to the
> group www-data. So in a shell punch in usermod -a -G www-data webmastr (I am
> assuming webmastr is your linux login name here). This will let you just
> give permissions to people in your group instead of everyone. Then go back
> to /home/webmastr/ and type chmod 750 -r public_html . This will give your
> full read-write-executable access to the files (7), your group including
> www-data read-execute permissions (5) and everyone else no permissions (0).
>
> Hopefully that will get your django install to work. If you want to learn
> more about linux permissions look here:http://www.freeos.com/articles/3127/ordo a search for ubuntu

Michael

unread,
Apr 6, 2008, 11:47:32 AM4/6/08
to django...@googlegroups.com
I've worked on putting Django on a Red Hat machine, and I've noticed
that the Ubuntu version of Apache is a bit different. But, in any
case, In my /etc/apache2 httpd.conf I have:

Debian packages are always different in how they are initially set up, but Apache works the same in most instances. If you have a conf file that works in red hat, as long as all the modules and the directories are the same, it should work in Ubuntu (also make sure that you have the same apache versions). 

I always find that with Apache simplicity is best. I would go through each line of the http.conf and ask myself what is it doing here, etc. The apache docs are pretty good at figuring out what is going on.
<IfModule dir_module>
       DirectoryIndex index.html index.php
</IfModule>

For example, do you know what this is doing? I am not saying it is you problem, but it looks like it was copied from somewhere. Do you know if you have dir_module on or off. Is this DirectoryIndex being applied?
 

<Location "/mysite/">
       SetHandler python-program
       PythonHandler django.core.handlers.modpython
       SetEnv DJANGO_SETTINGS_MODULE mysite.settings
       PythonDebug On
       PythonInterpreter mysite
       PythonPath "['/home/webmastr/public_html/django']+sys.path"
</Location>

And that's it. Excluding the /media, it should still show something,
I'd believe, or am I wrong?

This looks right to start up a django site. Is there a /home/webmastr/public_html/django/mysite/settings.py file. Does apache have permissions to get to it? To what Server/virtualmachine, port is apache executing this Location.

The media is only important to serve static files directly through apache so Django doesn't have to deal with them. If these instructions and the Django docs aren't working, you need to look into configuring apache. There are tons of sites that explain this. Just do a search for configuring apache. Every server is different and your needs are going to be different, just copying what other people won't work until you have a basic understanding of this. On a lovely Sunday you can learn enough about Apache to get django running.

Hope that helps,

Michael

HangingClowns

unread,
Apr 6, 2008, 12:05:06 PM4/6/08
to Django users
Well, after I followed your advice for putting webmastr in the www-
data group, I was getting 403 errors and forbidden errors on all of my
sites, so I 777 public_html recursively, so now it's all running, I'd
suppose that Apache should have access to it. So, from there on, I'm
lost at what could be the problem. I know Mod_Python is working, I
know Python is working, I know Apache is passing off to Mod_Python, I
just don't know why it can't find the project.
> >http://www.freeos.com/articles/3127/ordoa search for ubuntu

Michael

unread,
Apr 6, 2008, 12:09:11 PM4/6/08
to django...@googlegroups.com
The fact that it works with 777 permisions means that it was a permissions issue. Be really careful with 777 permissions however on your server that gives full read write execute permissions to everyone. THAT INCLUDES WEB USERS. It won't take much to hack your system if you grant full permissions to your public_html folder.

HangingClowns

unread,
Apr 6, 2008, 12:31:20 PM4/6/08
to Django users
Alright, what I'm saying is that, it STILL didn't work with the 777
permission, but, my other websites worked with 777. That's what I
meant to say, but I'm still getting the "EnvironmentError: Could not
import settings 'mysite.settings' (Is it on sys.path? Does it have
syntax errors?): No module named mysite.settings" error.

On Apr 6, 11:09 am, Michael <newmani...@gmail.com> wrote:
> The fact that it works with 777 permisions means that it was a permissions
> issue. Be really careful with 777 permissions however on your server that
> gives full read write execute permissions to everyone. THAT INCLUDES WEB
> USERS. It won't take much to hack your system if you grant full permissions
> to your public_html folder.
>
> > > >http://www.freeos.com/articles/3127/ordoasearch for ubuntu
Reply all
Reply to author
Forward
0 new messages