Serving Django apps using mod_wsgi from a site that does not run Django from /

跟風遠走 提交于 2019-12-22 09:49:27

问题


I have a website that I am currently rewriting app-by-app using Django. Server is RedHat. Running Apache 2. Installed mod_wsgi. Everything works fine. Awesome.

If I go to http://www.example.com/ I get to the main site that pre-exists (in PHP).

I have mod_wsgi running and working just fine. If I go to http://www.example.com/django/ I get to my Django powered homepage.

I have several apps that work from that just fine:

  • http://www.example.com/django/software: runs my Django software app just fine.
  • http://www.example.com/django/newsletter: runs my Django newsletter app just fine.

All the reverse url lookups work fine - everything is great.

The global entry in Apache's httpd.conf is:

WSGIScriptAlias /django /path/to/django/mysite/mysite/apache/django.wsgi

However, as I roll this out into production, I don't want to have the /django part of the url involved.

So, a request for http://www.example.com/software would run the Django software app.

A request for http://www.example.com/newsletter would run the Django newsletter app.

I can't make http://www.example.com/ redirect to the root Django wsgi application just yet - that will probably happen in a couple months time when the whole site Django rewrite is done.

So, any ideas how I can make my Django apps work in the current setup?

Do I have to create per app wsgi files (eg: /path/to/django/mysite/mysite/apache/software.wsgi and /path/to/django/mysite/mysite/apache/newsletter.wsgi) with per app entries in the httpd.conf file? Such as:

WSGIScriptAlias /software /path/to/django/mysite/mysite/apache/software.wsgi
WSGIScriptAlias /newsletter /path/to/django/mysite/mysite/apache/newsletter.wsgi

I can't seem to find that much specific documentation on this particular style of setup. Most docs assume that you are using one main wsgi file and Django (via urls.py) to broker all requests. I would love that to the case for me, but right now that can't happen.

It might be some Apache directive kung-fu I have to wrangle. If that is the case please let me know.

TIA.


回答1:


I understand you're rolling out new bits for everything on www.example.com piece by piece. How about setting up the whole Django project in another, new Apache virtualhost container with a new subdomain like new.example.com? You root Django app would be accessible from http://new.example.com, the software app from http://new.example.com/software etc. For the main domain www.example.com, just set up redirects:

www.example.com/django -> new.example.com
www.example.com/software -> new.example.com/software
www.example.com/newsletter -> new.example.com/newsletter

You can test your whole project on the same URL structure it will have after you throw the switch, and throwing the switch becomes merely replacing the ServerName directive in two virtualhosts.




回答2:


I had a similar issue, just the other way around. I wanted to replace a django installation part by party with a php-installation.

In Apaches virtual host config for the site use

WSGIScriptAliasMatch ^(/(software|newsletter)) /path/to/django/mysite/mysite/apache/django.wsgi$1

instead of

WSGIScriptAlias /software /path/to/django/mysite/mysite/apache/software.wsgi
WSGIScriptAlias /newsletter /path/to/django/mysite/mysite/apache/newsletter.wsgi

In the urls.py make django believe, that the mount point is the root url / like so:

...
url(r'^software/', include('software.urls')),
url(r'^newsletter/', include('newsletter.urls')),
...

This way, when you finished the site and want to serve everything, including the root url from django, you just need to replace the WSGIScriptAliasMatch entry with

WSGIScriptAlias / /path/to/django/mysite/mysite/apache/django.wsgi

and that will be it.

Check these Q&A for further information:

Django (wsgi) and Wordpress coexisting in Apache virtualhost

Reconfiguring Apache to serve website root from new php source and specific sub-urls from old django site



来源:https://stackoverflow.com/questions/12320803/serving-django-apps-using-mod-wsgi-from-a-site-that-does-not-run-django-from

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!