1
votes

I've a django app that works perfectly under the django development server. Now I'm trying to run it under apache2.2 using the mod_wsgi.
In the httpd.conf file of apache I've added:

<IfModule wsgi_module>
    WSGIScriptAlias /index my_path_to_wsgi_modules/django.wsgi
</IfModule>

and the django.wsgi module contains all the basic configuration as explained in django documentation: http://docs.djangoproject.com/en/dev/howto/deployment/modwsgi/
Unfortunately when I run the server and try to access the main page I obtain the template page but without variable data in it. The server log says:

[Fri Feb 18 13:50:33 2011] [error] [client 127.0.0.1] File does not exist: /usr/local/apache2/htdocs/api, referer: http://127.0.0.1/example/


As I said before the strange thing is that the same code works perfectly under django development server. I'm new in programming web app, please, can anybody help?

My django.wsgi file looks like this:

import os import sys

from os.path import sep

basepath = '/home/example/WorkSpace/examplews/src'

sys.path.append(basepath)
sys.path.append('/home/example/WorkSpace/examplews/src/examplews')

os.environ['DJANGO_SETTINGS_MODULE'] = 'examplews.settings'

import django.core.handlers.wsgi application = django.core.handlers.wsgi.WSGIHandler()

And my httpd.con file, like this:

ServerRoot "/usr/local/apache2"

Listen 80

LoadModule wsgi_module modules/mod_wsgi.so

User apache
Group apache

ServerAdmin [email protected]

DocumentRoot "/usr/local/apache2/htdocs"

<Directory />
Options FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
</Directory>

<Directory "/usr/local/apache2/htdocs">
Options Indexes FollowSymLinks

AllowOverride None

Order allow,deny
Allow from all

</Directory>

<IfModule dir_module>
DirectoryIndex index.html index.php index.sh default.jsp
</IfModule>

<FilesMatch "^\.ht">
Order allow,deny
Deny from all
Satisfy All
</FilesMatch>

ErrorLog "logs/error_log"

LogLevel warn

<IfModule log_config_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common

<IfModule logio_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
</IfModule>
CustomLog "logs/access_log" common

</IfModule>

<IfModule alias_module>
</IfModule>

<IfModule cgid_module>
</IfModule>

<IfModule wsgi_module>
WSGIScriptAlias /main /home/example/WorkSpace/examplews/src/examplews/apache_conf/django.wsgi
</IfModule>
<Directory "/home/example/WorkSpace/examplews/src/examplews/apache_conf">
Order allow,deny
Allow from all
</Directory>
DefaultType text/plain

<IfModule mime_module>
TypesConfig conf/mime.types
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
</IfModule>

<IfModule ssl_module>
SSLRandomSeed startup builtin
SSLRandomSeed connect builtin
</IfModule>

1
Does /usr/local/apache2/htdocs/api actually exist? Maybe something else in Apache is misconfigured.Evan Porter
No, /usr/local/apache2/htdocs/api it doesn't exist. Actually it is a virtual URL that should be accessed through django urls.pygreen69
Please post your full Apache configuration, and the wsgi file.Daniel Roseman
I've post my django.wsgi and my httpd.conf above. Many thanks!green69

1 Answers

0
votes

Finally I found the error. It was not connected to httpd.conf file but to how URLs are specified both to django urls.py file and to templates.
As I mounted myapp in this way:

WSGIScriptAlias /myapp my_path_to_wsgi_module/django.wsgi

I was believing that URLs specified in django template files should carry an initial slash, like this: '/api/dir'
It results that in this way the application works only on django development server but not on apache.

Instead if you use URLs without initial slash like:
'api/dir'
The app works correctly both on django development server and on apache!

You must avoid using starting slashes even on pattern matching of django urls.py file:
like this: (r'^api/dir$', 'available_services')
and NOT like this: (r'^/api/dir$', 'available_services')

Maybe this is an obvious thing for expert django users but if you're novice like me, this can make you loose a certain amount of time because it's a hard problem to be detected.