I am trying to deploy a Django application for the first time using mod_wsgi
with Apache
on a Ubuntu 12.04
VM. I have been following several tutorials, especially Ayman Farhat blog, this excellent YouTube video and of course the official Django Documentation
This follows an earlier question I posted here wondering why my Django survey did not simply work when I uploaded it to the /var/www/ (blush!) I have since been looking into mod_wsgi
as per the answers.
I'm not sure what stage I am missing. The project is able to start on the server via python manage.py runserver
with no errors. I have also ran python manage.py collectstatic
with no errors.
I then restart Apache with
sudo service apache2 restart
However when I go the URL http://phaedrus.scss.tcd.ie/bias_experiment/surveythree/ where I expect to see the survey nothing is there. I just see the standard 404 error.
I am really not sure what I have to do next or why this is not working.
Below is my setup and what I have tried so far.
NOTE: I have a Bias_Experiment Django Project created in Pydev. It has three applications contained within an src
folder.
- survey (my working project)
- polls (a tutorial i was following)
- bias_experiment (the root application with my settings file etc)
My Project Structure

My virtual host located at /etc/apache2/sites-available/bias_experiment
<VirtualHost *:80>
ServerAdmin admin@email.com
ServerName kdeg-vm-18.scss.tcd.ie
ServerAlias http://collegeserver.ie/bias_experiment
WSGIScriptAlias / var/www/bias_experiment/src/bias_experiment/index.wsgi
Alias /static/ /var/www/bias_experiment/src/bias_experiment/static/
<Location "/static/">
Options -Indexes
</Location >
</VirtualHost >
My WSGI file located at /var/www/bias_experiment/src/bias_experiment/index.wsgi
import os
import sys
import site
# Add the site-packages of the chosen virtualenv to work with
site.addsitedir('~/var/www/bias_experiment/lib/python2.7/site-packages')
# Add the app's directory to the PYTHONPATH
sys.path.append('/var/www/bias_experiment')
sys.path.append('/var/www/bias_experiment/src/bias_experiment')
os.environ['DJANGO_SETTINGS_MODULE'] = 'bias_experiment/src/bias_experiment.settings'
# Activate your virtual env
activate_env=os.path.expanduser("~/var/www/bias_experiment/bin/activate_this.py")
execfile(activate_env, dict(__file__=activate_env))
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
My URL patterns from bias_experiment/src/bias_experiment/urls.py
urlpatterns = patterns('',
url(r'^polls/', include('polls.urls', namespace="polls")),
url(r'^admin/', include(admin.site.urls)),
url(r'^surveythree/$', SurveyWizard.as_view([SurveyForm1, SurveyForm2, SurveyForm3, SurveyForm4, SurveyForm5])),
)
The address you are going to in your browser does not match either the ServerName or ServerAlias directives in your Apache configuration, so the virtualhost won't know to respond to that request.
Note that ServerAlias should be similar to ServerName - a hostname, not a URL, without http prefix or path. Also note you can have multiple values for ServerAlias if you need that virtualhost to respond to many hostnames.
If you want the Django app to be served underneath /bias_experiment, that should be part of the WSGIScriptAlias.
So it should be:
ServerAlias phaedrus.scss.tcd.ie
WSGIScriptAlias /bias_experiment /var/www/bias_experiment/src/bias_experiment/index.wsgi
Also I'm confused about the locations of your code. Is it in /var/www/... or /home/whoever/var/www? Your wsgi file refers to the latter, but the Apache conf has the former.
Next, virtualenv is supposed to take care of setting all the Python paths. So since you are running the activate script, you can remove the lines that modify sys.path and site.addsitedir. Although you might need to keep the one that adds the src
directory.
Another problem is with your DJANGO_SETTINGS_MODULE. It should be a Python module, not a file path - actually what you have is a cross between them both. Since src
is on the Python path, you can just set this to 'bias_experiment.settings'.
来源:https://stackoverflow.com/questions/24188604/django-application-not-visible