Question

I want to deploy my django app in a Ubuntu server with Apache and mod_wsgi. I am following this tutorial http://thecodeship.com/deployment/deploy-django-apache-virtualenv-and-mod_wsgi/

in my site file I have

<VirtualHost *:80>
    ServerAdmin webmaster@mydomain.com
    ServerName mydomain.com
    ServerAlias www.mydomain.com
    WSGIScriptAlias / var/www/index.wsgi

    Alias /static/ /var/www/static/
    <Location "/static/">
        Options -Indexes
    </Location >
</VirtualHost >

Don't mind the generic mydomain.com entries. The site is for progress display only so I can discuss it with my friend.

in /var/www/index.wsgi I have this

import os
import sys
import site

# Add the site-packages of the chosen virtualenv to work with
site.addsitedir('/home/voger/venvs/ssite/local/lib/python2.7/site-packages')



# Add the app's directory to the PYTHONPATH
sys.path.append('/home/voger/projects/ssite')
sys.path.append('/home/voger/projects/ssite/ssite')

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

# Activate your virtual env
activate_env=os.path.expanduser("/home/voger/venvs/ssite/bin/activate_this.py")
execfile(activate_env, dict(__file__=activate_env))

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

in my settings.py I have two lines for redirecting after login and logout. These are not prepended with index.wsgi

LOGIN_REDIRECT_URL = '/login_redirect'
ACCOUNT_LOGOUT_REDIRECT_URL = "/logout_redirect"

When these urls are requested it returns 404

Reverse for 'login_redirect' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []

In my html I have 4 links

<a id="logout" style="display: inline-block;" href="/index.wsgi/accounts/logout/">Logout</a>
<a id="profile" style="display: inline-block;" href="/accounts/profile">My Account</a>
<a id="signup" style="display: none;" href="/index.wsgi/accounts/signup/">Sign Up</a>
<a id="login" style="display: none;" href="/index.wsgi/accounts/login/">Sign In</a>

and as you can see 3 entries that are inserted with {% url %} are prepended automatically with /index.wsgi while the only one who is hardcoded it is not prepended. But still they work as expected.

Why do I have this /index.wsgi prepended and how can I remove it? In my development machine using django's built in server everything works fine.

I don't know if it is related but the login, logout and signup urls are provided by django-allauth.

Thank you for any answer or comment

Was it helpful?

Solution

For starters:

WSGIScriptAlias / var/www/index.wsgi

is wrong. You are missing leading slash on the path for the WSGI script.

You also should not really go sticking index.wsgi in DocumentRoot directory as you possibly are. If for some reason your VirualHost with WSGIScriptAlias isn't being used, and a .wsgi handler of wsgi-script has been setup elsewhere in the Apache configuration, it means that a URL of /index.wsgi works to access your Django with all your problems then starting from there. This is because Django will believe it is meant to prefix URLs with /index.wsgi as that ends up being the mount point of the application.

So, move your WSGI script file out of DocumentRoot directory for your server. Remove any AddHandler directive for .wsgi extension. Ensure you VirtualHost is actually being used and specifically that WSGIScriptAlias directive usage is correct.

BTW, why are you using some arbitrary persons blog post rather than the official mod_wsgi setup notes on the Django docs site.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top