Running django site and wikis on the same linux machine with apache - django

I've got a bit of a problem with forcing django based site and a "regular" sites i.e. wikis to work on the same machine, and to top it I'm kinda new to this stuff...
I've been trying to setup this using the VirtualHost but it seems that this does not work very well. In other words I can either run the django site (alredy in form of host.com/djangosite) and other sites in the same time (i.e host.com/othersite1). Only one works at a time it looks like the apache runs first VirtualHost it finds.
the structure in the server looks like this
/var/www/djangosite
/var/www/othersite1
/var/www/othersite2 ... etc
Now the sites-enabled, I experimented a bit and finally ended up in one file with two VirtualHosts for now:
<VirtualHost *:80>
ServerAdmin ss#dd.gg
DocumentRoot /var/www/djangosite
ServerName host.com/djangosite
Alias /m/ "/var/www/djangosite/forum/skins/"
<Directory "/var/www/djangosite/forum/skins">
Order allow,deny
Allow from all
</Directory>
Alias /upfiles/ "/var/www/djangosite/forum/upfiles/"
<Directory "/var/www/djangosite/forum/upfiles">
Order deny,allow
Allow from all
</Directory>
WSGIScriptAlias /djangosite /var/www/djangosite/djangosite.wsgi
CustomLog /var/log/djangosite.access.log common
ErrorLog /var/log/djangosite.error.log
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/othersite1
ServerName host.com/othersite1
</VirtualHost>
On their own both VirtualHosts do ok, i.e. when I put this in a file in sites-enabled the djangosite works fine, the second is not found, (something with document root being rooted at wrong dir in filesystem structure). If I put the second Vhost on top it works the djangosite doesn't.
Ok. so now a real question is there a recipe for running djangosite and "regular" site on one linux/apache configuration with one NIC and one hostname.
thanks for at least reading :)

You don't need two seperate VirtualHosts. Just put them both in the same one, with the ServerName as just "host.com". Then you can use aliases - the WSGIScriptAlias for the Django site, and a normal Alias for the wiki.
One note: please do not put your Django code in the DocumentRoot. This is dangerous - it makes it potentially possible for people to see your code, including your passwords etc in settings.py. Move it somewhere else, and leave out the DocumentRoot directive.

Related

Django / WSGI blank page

I am trying to get Django to work together with Apache using WSGI. The problem is that it simply returns an empty page (200 OK or 203 not modified).
I can't find any error upon restarting or when accessing the page in either error.log or django-error.log. There is an entry in django-access.log though.
WSGIPythonPath /web/django/dtest/
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /web/django
ServerName django.markv.nl
<Directory />
Options FollowSymLinks Includes
AllowOverride All
</Directory>
WSGIScriptAlias /wsgi-scripts/ /web/django/dtest/dtest/wsgi.py
LogLevel info
ErrorLog ${APACHE_LOG_DIR}/django-error.log
CustomLog ${APACHE_LOG_DIR}/django-access.log common
</VirtualHost>
I tried using the manage.py runserver, in which case it works fine, so it's really an apache/wsgi problem.
I tried disabling mod_php5 with no result (except that PHP stopped working).
Any clues what the problem is or where I can find an error? It's probably something trivial but I fail to find it...
You're serving your application at /wsgi-scripts/, which seems an odd thing to do, so you'll need to go to that URL to see it.
Note that you absolutely certainly do not want to set your DocumentRoot to the location of your Django files, although this is not the source of your problem.

mod_wsgi and multiple projects

tearing my hair out here trying to figure out why my two django projects are not being separately served ... it seems that the static files for whichever is accessed first become the defacto static files for both projects, or something to similar effect.
I'm attempting to serve two projects (which are actually different versions of the same original project - with different databases, and different physical locations), via two domain names off the same IP address. Initially I tried virtualhosts on multiple IP addresses (differentiated by port), but that failed. Unfortunately - I have exactly the same problem using virtualhosts with different domain names.
The virtualhost section of the Apache http.conf is as so:
WSGIApplicationGroup %{GLOBAL}
Listen 80
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin shane.brown#gmail.com
ServerName www.toastprojects.tk
WSGIScriptAlias / "C:/Python27/sites/Adaptwater/adaptwater/wsgi.py"
Alias /static/ "C:/Python27/sites/Adaptwater/static/"
</VirtualHost>
<Directory "C:/Python27/sites/Adaptwater/static/">
Order deny,allow
Allow from all
</Directory>
<Directory "C:/Python27/sites/Adaptwater/adaptwater/">
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
<VirtualHost *:80>
ServerAdmin shane.brown#gmail.com
ServerName toastprojects.power.on.net
WSGIScriptAlias / "C:/git_clones/adaptwater/adaptwater/adaptwater/wsgi.py"
Alias /static/ "C:/git_clones/adaptwater/adaptwater/static/"
</VirtualHost>
<Directory "C:/git_clones/adaptwater/adaptwater/static/">
Order deny,allow
Allow from all
</Directory>
<Directory "C:/git_clones/adaptwater/adaptwater/adaptwater/">
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
I've set up wsgi.py for each as so (with the absolute path corresponding to the particular project's location) :
import os, sys
sys.path.append('C:/git_clones/adaptwater/adaptwater')
sys.path.append('C:/git_clones/adaptwater')
#os.environ.setdefault("DJANGO_SETTINGS_MODULE", "adaptwater.settings")
os.environ['DJANGO_SETTINGS_MODULE'] = "adaptwater.settings"
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
Running these on localhost through the dev server (and with nginx serving static files) at the same time works with no problem. And assuming I want to access the sites one at a time, and restart Apache - each works served externally on a solo basis. Anything obvious causing this problem?
Cheers, Shane
Update :
At this point I have to conclude that what I want to do cannot be done due to the less than perfectly happy marriage of mod_wsgi and the windows platform (I should have mentioned the platform - neglected to in my haste - windows 7 professional 64). I can't use WSGIDaemonProcess, and WSGIApplicationGroup must be set as global, and from what I've been able to glean from discussions relevant to this issue - that means I'm at a dead end.
I've managed to serve the second project with no weird settings hybridization using nginx & fastcgi instead ... as a stopgap. So far this combo has been treating me kindly.
The even less stellar option of nginx serving static files and proxy passing to the django dev server also works as a parallel arrangement for external serving. Have yet to try two nginx/fastcgi served versions of the project simultaneously - but I'll leave that as an exercise for another awesome day of frustration.
You must include your <directory> configuration directives within their corresponding <virtualhost> configuration directives.

Django urls.py catches all existing HTML projects' urls on WampServer

I had WampServer working perfectly with Aptana IDE using multiple folders for different html projects. Then I installed Django and added this to Apache's httpd.conf:
Alias /images/ "c:/wamp/www/daas/templates/images/"
<Directory "c:/wamp/www/daas/images>
Order allow,deny
Allow from all
</Directory>
WSGIScriptAlias / "c:/wamp/www/daas/apache/django.wsgi"
<Directory "c:/wamp/www/daas/apache">
Allow from all
</Directory>
<VirtualHost *:80>
ServerName 127.0.0.1
DocumentRoot c:/wamp/www/daas/
</VirtualHost>
and now the virtual hosts I had for html projects...
NameVirtualHost 127.0.0.1
<VirtualHost 127.0.0.1>
ServerName localhost
DocumentRoot 'C:\wamp\www'
</VirtualHost>
<VirtualHost 127.0.0.1>
ServerName projectA.local
DocumentRoot 'C:\wamp\www\projectA'
</VirtualHost>
don't load correctly because Django's urls.py is catching all urls The current URL, projectA, didn't match any of these regular expressions, even the ones not inside the Django's projects.
How can I solve this? On Djano's side or on Apache's side? Here (How do i run Django and phpmyadmin on apache webserver on ubuntu) is a similar problem but the solution didn't worked for me. And luckily localhost/phpmyadmin works correctly but localhost/projectA doesn't.
It looks like WSGIScriptAlias / "c:/wamp/www/daas/apache/django.wsgi" in your httpd.conf catches all requests on apache.

Pointing subdomain to specific url in Django

I have a Django site, working on mod_wsgi and Apache. (eg, example.com) I made a subdomain (eg, info.example.com) and want to point it to app in the existing project. Is it possible, that I have one urls.py file, and example.com/info and info.example.com point to the same view in Django project (without duplicating the whole project in different directory)?
If so, how can I do it? Currently the example.com virtalhost config for the existing project looks like this:
<VirtualHost 12.34.56.78:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /srv/www/Example/Pub_html
WSGIScriptAlias / /srv/www/Example/example.wsgi
<Directory /srv/www/Example/Example>
Order allow,deny
Allow from all
</Directory>
Alias /robots.txt /srv/www/Example/robots.txt
Alias /favicon.ico /srv/www/Example/favicon.ico
....
</VirtualHost>
Thanks in advance!
UPDATE: I did a quick fix here by creating another Django project which shares database tables and template folder with the existing one. It works, but it's not really DRY :)
I'd do this with a reverse proxy, I reckon. If you have mod_proxy and mod_proxy_html available, try something like this:
<VirtualHost 12.34.56.78:80>
ServerName info.example.com
ProxyPass / http://example.com/info
SetOutputFilter proxy-html
<Location />
ProxyPassReverse /info/
ProxyHTMLURLMap /info/ /
</Location>
ProxyHTMLURLMap http://example.com/info /
</VirtualHost>
the details may not be quite right, so let me know if you can't get it to work.

Django (wsgi) and Wordpress coexisting in Apache virtualhost

I have a Django project that I need mounted at two different subdirectories of my url, and I need Wordpress running at /. So:
*.example.com - WordPress
*.example.com/studio - django
*.example.com/accounts - django
Here's the httpd.conf that I have so far:
<VirtualHost *:80>
ServerName wildcard.localhost
ServerAlias *.localhost
AddType application/x-httpd-php .php
DocumentRoot /var/empty
Alias /site_media/ /home/zach/projects/python/myproject/static/
Alias /media/ /home/zach/projects/python/myproject/env/lib/python2.6/site-packages/django/contrib/admin/media/
Alias / /home/zach/projects/python/myproject/wordpress/
WSGIScriptAlias /accounts /home/zach/projects/python/myproject/app/privio.wsgi
WSGIScriptAlias /studio /home/zach/projects/python/myproject/app/privio.wsgi
<Directory /home/zach/projects/python/myproject/app>
Order allow,deny
Allow from all
</Directory>
<Directory /home/zach/projects/python/myproject/wordpress>
Order allow,deny
Allow from all
</Directory>
Before I added the config for WordPress, the Django app was working fine. But with this new setup I am able to see the WordPress install at /, but the Django app isn't getting served. I'm sort of a noob at Apache config - what am I missing?
Replace:
DocumentRoot /var/empty
with:
DocumentRoot /home/zach/projects/python/myproject/wordpress
Remove:
Alias / /home/zach/projects/python/myproject/wordpress/
Replace:
WSGIScriptAlias /accounts /home/zach/projects/python/myproject/app/privio.wsgi
WSGIScriptAlias /studio /home/zach/projects/python/myproject/app/privio.wsgi
with:
WSGIScriptAliasMatch ^(/(accounts|studio)) /home/zach/projects/python/myproject/app/privio.wsgi$1
In other words, use DocumentRoot to refer to wordpress that needs to be at root of site and not Alias directive.
The WSGIScriptAliasMatch is so Django itself thinks it is still mounted at root site even though only nominated sub URLs of it are actually passed through. This simplifies things for urls.py.
Note that the $1 at end of WSGI script path is important, so don't leave it off.
Paging Graham Dumpleton :)
I'd venture a guess that the line
Alias / /home/zach/projects/python/myproject/wordpress/
overrides everything below it. Therefore any requests to /accounts will be processed by the wordpress application rather than by the Django application.
From the documentation:
Mounting At Root Of Site
If instead you want to mount a WSGI application at the root of a site, simply list '/' as the mount point when configuring the WSGIScriptAlias directive.
WSGIScriptAlias / /usr/local/www/wsgi-scripts/myapp.wsgi
Do note however that doing so will mean that any static files contained in the DocumentRoot will be hidden and requests against URLs pertaining to the static files will instead be processed by the WSGI application.