0

I have a Django application with the following directory structure

/myapp/
   /login/
   /myapp_settings/
   /subapp1/
   /supapp2/

manage.py is in the myapp directory.

In the project's url.py I have URL settings like this:

urlpatterns = patterns('',
    url(r'^subapp1/', include('subapp1.urls')),
    url(r'^xhr/', include('subapp1.urls')),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^subapp2/', include('smart_selects.urls')),

        # Login / logout.
    url(r'^login/$', 'django.contrib.auth.views.login'),
    url(r'^logout/$', 'django.contrib.auth.views.logout', {'next_page': '/subapp1/'}, name='auth_logout'),
    url(r'^logout/(?P<next_page>.*)/$', 'django.contrib.auth.views.logout', name='auth_logout_next'),
)   

When deployed on the development runserver, everything links and loads correctly. When I deploy the entire myapp directory to the Django root on Apache, I find it's not linking as expected.

For example, if I link to example.com/login/, I get an Apache 404. I think it's because I don't have a virtual directory configuration defined for that specific directory.

I have the following set up in Apache for my application:

WSGIScriptAlias /myapp /var/www/django-projects/myapp/myapp_settings/wsgi.py
WSGIPythonPath /var/www/django-projects/myapp

Alias /media/ /var/www/django-projects/myapp/media/
Alias /static/ /var/www/django-projects/myapp/static/


<Directory /var/www/django-projects/myapp/static>
Order deny,allow
Allow from all
</Directory>

<Directory /var/www/django-projects/myapp/media>
Order deny,allow
Allow from all
</Directory>

<Directory /var/www/django-projects/myapp/myapp_settings/>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>

Do I need an Alias and a Directory configuration for each URL pattern I use? If so, how can I redo my URL patterns so that I don't need to do this. I don't want to have to do all of this extra Apache configuration when I deploy the application.

EDIT: I modified my WSGIScriptAlias as suggest by Reinbach. It now reads WSGIScriptAlias / /var/www/django-projects/myapp/myapp_settings/wsgi.py. However, this still returns a 404. The error in the Apache log says

[Fri Sep 07 09:11:00 2012] [error] [client 192.189.x.x] File does not exist: /var/www/html/login

Notice that it's looking in /var/www/html (Default Apache root) instead of /var/www/django-projects

EDIT2: I'm attaching the VirtualHost block for this section

WSGIPythonPath /var/www/django-projects/myapp
<VirtualHost sub.example.com:80>
     DocumentRoot /var/www/django-projects/myapp
     ServerName sub.example.com
     WSGIScriptAlias / /var/www/django-projects/myapp/myapp_settings/wsgi.py


     Alias /robots.txt /var/www/django-projects/myapp/static/robots.txt
     Alias /favicon.ico /var/www/django-projects/myapp/static/favicon.ico
     AliasMatch ^/([^/]*\.css) /var/www/django-projects/myapp/static/css/$1
     AliasMatch ^/([^/]*\.js) /var/www/django-projects/myapp/static/js/$1
     AliasMatch ^/([^/]*\.png) /var/www/django-projects/myapp/static/images/$1
     AliasMatch ^/([^/]*\.swf) /var/www/django-projects/myapp/static/swf/$1


     Alias /media/ /var/www/django-projects/myapp/media/
     Alias /static/ /var/www/django-projects/myapp/static/

     <Directory /var/www/django-projects/myapp/static>
         Order deny,allow
         Allow from all
     </Directory>
     <Directory /var/www/django-projects/myapp/media>
         Order deny,allow
         Allow from all
     </Directory>
     <Directory /var/www/django-projects/myapp/myapp_settings/>
         <Files wsgi.py>
              Order deny,allow
              Allow from all
         </Files>
     </Directory>
</VirtualHost>
Andy
  • 49,085
  • 60
  • 166
  • 233

2 Answers2

3

I believe you want to change your WSGIScriptAlias as you currently have it expecting to handle example.com/myapp/login while your sample is showing you trying to use example.com/login

WSGIScriptAlias / /var/www/django-projects/myapp/myapp_settings/wsgi.py

See How to use Django with Apache and mod_wsgi

Reinbach
  • 761
  • 4
  • 7
  • you will need to define a VirtualHost for this application and then set the "DocumentRoot /var/www/django-projects/myapp" – Reinbach Sep 07 '12 at 14:55
1

LONG STORY SHORT

Allow and Deny are deprecated since Apache 2.4.x, use Require all granted (or denied) instead.

THE LONG STORY

I'm encountering the same problem while trying to set up Django with Apache and mod_wsgi. I'm not entirely sure why this happens but when I comment out the following lines in the httpd.conf

# Deny access to the entirety of your server's filesystem. You must
# explicitly permit access to web content directories in other 
# <Directory> blocks below.
#
#<Directory />
#    AllowOverride none
#    Require all denied
#</Directory>

everything works alright.

I'm not sure if this is the right and secure way to solve the problem, but I hope it might help.

P.S. I think this shouldn't cause much security troubles since root directory is aliased anyways:WSGIScriptAlias / /var/www/django-projects/myapp/myapp_settings/wsgi.py, but I may be worng.

P.P.S. Looks like the better solution would be to leave those lines uncommented, but change Order and Allow directives in favor of Require all granted. For example:

<Directory /var/www/django-projects/myapp/myapp_settings/>
<Files wsgi.py>
Require all granted
#Order deny,allow
#Allow from all
</Files>
</Directory>

However this is only a trial and error solution I could come up. I got no deep understanding why it works but Order and Allow doesn't.

P.P.P.S Oh, now I know what. Allow and Deny are deprecated since Apache 2.4.x. Good answer can be found here.

Community
  • 1
  • 1
Wormer
  • 665
  • 7
  • 11