Django application hosted on Ubuntu VM with Apache

2020-04-17 17:37发布

I have uploaded my first Django Application but I am having trouble accessing it. The application is called survey and has been uploaded onto an Ubunto VM running Apache with mod_wsgi. My VM has been made public using a Proxy pass at http://phaedrus.scss.tcd.ie/bias_experiment

Per the urls.py file below, the survey should be available at http://phaedrus.scss.tcd.ie/bias_experiment/surveythree/ When I access it locally it works at http://127.0.0.1:8000/surveythree/

I have posted two questions related (1)(2) to this lately and have received lots of help on related issues. However the application is still not visible and I do not understand why.

The steps I took

  • Uploaded the project to /var/www/ (project structure is below)
  • Installed virtualenv
  • Installed mod_wsgi
  • Installed any other related packages
  • Created the virtualhost file (below)
  • Created the index.wsgi file (below)
  • Restarted apache
  • Ran a2ensite a2ensite bias_experiment
  • Restarted apache

However when the user visited http://phaedrus.scss.tcd.ie/bias_experiment/surveythree/ they would get an Apache 404 error page and visiting http://phaedrus.scss.tcd.ie/bias_experiment/ would show them the file system,

I then added a direct link to the wsgi file by adding the below to /etc/apache2/sites-available/default

WSGIScriptAlias /bias_experiment/ /var/www/bias_experiment/src/bias_experiment/index.wsgi

Now when the user visits http://phaedrus.scss.tcd.ie/bias_experiment/ they at least see Django is running. However visiting http://phaedrus.scss.tcd.ie/bias_experiment/surveythree/ still results in an Apache 404 error page.

Can anyone see what is wrong with my setup? I have followed multiple tutorials and extensively looked into this but cannot figure it out.

The below is my setup Please feel free to request any aditional details.

Thanks

My VirtualHost file located at /etc/apache2/sites-available/bias_experiment

<VirtualHost *:80>
ServerAdmin myemail@gmail.com
ServerName phaedrus.scss.tcd.ie/bias_experiment
ServerAlias phaedrus.scss.tcd.ie
WSGIScriptAlias /bias_experiment/ /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 index.wsgi file located at /var/www/bias_experiment/src/bias_experiment/index.wsgi

import os
import sys
import site

# This was kept in order to add the src folder
sys.path.append('/var/www/bias_experiment/src')
sys.path.append('/var/www/bias_experiment/src/bias_experiment')

os.environ['DJANGO_SETTINGS_MODULE'] = '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 urls.py file located at /var/www/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])),   
)

My Apace error Log tail /var/log/apache2/error.log

(bias_experiment)spillab@kdeg-vm-18:/var/www/bias_experiment$ sudo tail /var/log/apache2/error.log
[Sun Jun 15 17:37:26 2014] [error] [client 134.226.38.233] Target WSGI script not found or unable to stat: /var/www/bias_experiment/src/bias_experiment/index.wsgisurveythree
[Sun Jun 15 17:37:50 2014] [error] [client 134.226.38.233] Target WSGI script not found or unable to stat: /var/www/bias_experiment/src/bias_experiment/index.wsgisurveythree
[Sun Jun 15 18:38:11 2014] [error] [client 134.226.38.233] Target WSGI script not found or unable to stat: /var/www/bias_experiment/src/bias_experiment/index.wsgisurveythree
[Sun Jun 15 18:39:53 2014] [error] [client 134.226.38.233] Target WSGI script not found or unable to stat: /var/www/bias_experiment/src/bias_experiment/index.wsgisurveythree
[Sun Jun 15 18:40:00 2014] [error] [client 134.226.38.233] Target WSGI script not found or unable to stat: /var/www/bias_experiment/src/bias_experiment/index.wsgisurveythree
[Sun Jun 15 18:40:01 2014] [error] [client 134.226.38.233] Target WSGI script not found or unable to stat: /var/www/bias_experiment/src/bias_experiment/index.wsgisurveythree
(bias_experiment)spillab@kdeg-vm-18:/var/www/bias_experiment$ 

My Project Structure

enter image description here

Thanks!

1条回答
迷人小祖宗
2楼-- · 2020-04-17 18:07

I think the virtual host is a non-starter, because the server name you are using is the main name of that server. So we can probably delete that altogether, and we need to concentrate on what's in /sites-available/default. And I think the only thing wrong there is that we don't need a trailing slash in the alias:

WSGIScriptAlias /bias_experiment /var/www/bias_experiment/src/bias_experiment/index.wsgi

(I might have previously steered you doubly wrong on that, but this time I found the recommendation in the mod_wsgi documentation directly from the author, Graham Dumpleton.)

查看更多
登录 后发表回答