Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django WSGI Apache VirtualHost, returning a 404 Error

  • Ubuntu 12.04
  • Apache 2.2.2
  • Python 3.2.3
  • Django 1.6.1

I followed directions at Using mod_wsgi to Serve Applications on Ubuntu 12.04, for setting up a WSGI for my Django on Apache. Problem is, the VirtualHost domain is now returning a confusingly weird 404 error:

[Mon Dec 16 19:53:49 2013] [error] [client 127.0.0.1] File does not exist: /etc/apache2/htdocs

It's confusing because... that directory doesn't even exist. The VirtualHost is setup in /var/www/main and the config's for it in /var/apache2/sites-available/default looks like this:

<VirtualHost *:80>
        ServerName 127.0.0.1
        ServerAlias coral
        WSGIScriptAlias / /home/dfy/code/djdev/djdev/wsgi.py
</VirtualHost>

I reloaded Apache after editing. That wsgi file is the one generated by Django when I started the project. I altered it to match what was needed according to the tutorial:

import os
import sys # added from tutorial
sys.path.append('/home/dfy/code/djdev/djdev/') # added from tutorial
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "djdev.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

To me, this all looks like it should work, but Apache is lookin' for static files in a non-existent directory (/etc/apache2/htdocs). I've been Googling for hours trying to figure out what's wrong here, but haven't found the answer. On a whim I tried chmod 777 wsgi.py, but that didn't change anything, so I'm sure it's not a permissions problem. I really no idea what's gone wrong.

like image 302
Zamphatta Avatar asked Dec 17 '13 01:12

Zamphatta


2 Answers

but Apache is lookin' for static files in a non-existent directory

I'm not sure what is going on here, but I suspect your main configuration is not correct. If you do not supply a DOCUMENT_ROOT, Apache will look for it in the default location, which is htdocs.

As per the django documentation on deployment, you need to provide the following configuration for your virutal host. You chould change the paths as they apply to your setup:

Alias /robots.txt /path/to/mysite.com/static/robots.txt

Alias /favicon.ico /path/to/mysite.com/static/favicon.ico

AliasMatch ^/([^/]*\.css) /path/to/mysite.com/static/styles/$1

Alias /media/ /path/to/mysite.com/media/
Alias /static/ /path/to/mysite.com/static/

<Directory /path/to/mysite.com/static>
    Order deny,allow
    Allow from all
</Directory>

<Directory /path/to/mysite.com/media>
    Order deny,allow
    Allow from all
</Directory>

WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py
WSGIPythonPath /path/to/mysite.com

<Directory /path/to/mysite.com/mysite>
    <Files wsgi.py>
        Order deny,allow
        Require all granted
    </Files>
</Directory>

Please use the official docs which are updated with changes to specific django versions.

like image 64
Burhan Khalid Avatar answered Oct 31 '22 21:10

Burhan Khalid


Maybe your loosing something: In my vhost configuration I have:

<virtualhost *:80>
...
Alias /static/ /the/path/to/static/
<Location "/static/">
        Options -Indexes
</Location>
...

</virtualhost>

I hope it helps

like image 28
azuax Avatar answered Oct 31 '22 20:10

azuax