开发者

How to deploy Django application at sub-url?

开发者 https://www.devze.com 2022-12-15 18:52 出处:网络
I need to set up a django development environment that is publicly viewable on the internet (I am doing this for school, and my projects need to be viewable by my professor, this isn\'t 开发者_JAVA技巧

I need to set up a django development environment that is publicly viewable on the internet (I am doing this for school, and my projects need to be viewable by my professor, this isn't 开发者_JAVA技巧a setup that needs much security). I have a virtual server running Ubuntu 8.04 LTS.

I need to have multiple django applications running in subdirectories of my main site. That is, I need mysite.com to be a static page, mysite.com/wordpress to be my wordpress blog, and mysite.com/django1 mysite.com/django2 etc. to be django projects.

I am using apache, and I will either be using sqlite or mysql.

There seem to be as many different ways to install and configure django as there are websites offering advice, and all of them assume a single project is going to be the root of the website. I'd really appreciate some help, thank you.


You can use

WSGIScriptAlias /django1 /home/keratacon/www/django1/wsgi.py
WSGIScriptAlias /django2 /home/keratacon/www/django2/wsgi.py

in your apache+mod_wsgi config, assuming wsgi.py is the name of your wsgi script.


This blog explains the solution (assuming that mod_wsgi is used, with nginx/uwsgi the solution is similar apparently in nginx/uwsgi this is not necessary).

The first parameter of WSGIScriptAlias - the /sub-url will be stripped from the request url and the rest will go to your django app. If your Django app urls all start with /sub-url (which are stripped by mod_wsgi), then you will not be able to show the views at those urls, unless you "re-insert" the /sub-url to the request path part.

import django.core.handlers.wsgi

_application = django.core.handlers.wsgi.WSGIHandler()
def application(environ, start_response):
    #the line below will re-append the sub-url to all urls
    environ['PATH_INFO'] = environ['SCRIPT_NAME'] + environ['PATH_INFO']
    #this one will make django think that it's the only thing running on the server
    environ['SCRIPT_NAME'] = '' # my little addition to make it work
    return _application(environ, start_response)

Also in your urls.py all urls must be prefixed with the sub-url of your interest.

Finally, the WSGIScriptAlias must be the same as your sub-url:

#the below line will "take-out" the sub-url and pass the rest
#to your wsgi script
WSGIScriptAlias /sub-url /path/to/wsgi_script

Where file /path/to/wsgi_script must contain the definition of application as shown in the first code snippet.

To make the "sub-url" setup explicit in Django, the equivalent request path patching would have to occur within the Django framework.

0

精彩评论

暂无评论...
验证码 换一张
取 消