mod_wsgi and multiple projects - django

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.

Related

Apache virtual hosts on subdirectories

I am trying to setup Apache to serve multiple apps on one IP-Address over subdirectories. Lets say, I would like to access App1 over http://aaa.bbb.ccc.ddd/app1 and App2 over http://aaa.bbb.ccc.ddd/app2. Both, App1 and App2, are independent django projects. I ensured, that both apps are working fine by serving only one of them over Apache.
I added the following lines to the httpd.conf file of Apache:
# App1
<VirtualHost *:80>
DocumentRoot "D:/Projects/App1/App1"
ServerName App1
<Directory "D:/Projects/App1/App1">
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIScriptAlias /app1 "D:/Projects/App1/App1/wsgi.py"
</VirtualHost>
# App2
<VirtualHost *:80>
DocumentRoot "D:/Projects/App2/App2"
ServerName App2
<Directory "D:/Projects/App2/App2">
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIScriptAlias /app2 "D:/Projects/App2/App2/wsgi.py"
</VirtualHost>
Working like this results into an error saying "Forbidden, You don't have permission to access this resource." when I call http://aaa.bbb.ccc.ddd/app2 from another machine. Similar to this, if I put the App2 virtual host in front of the App1 virtual host, I can not access http://aaa.bbb.ccc.ddd/app1 anymore. So it is either App1 or App2 that is accessible, but never both of them.
First question: Is my idea of serving to webpages over sub-URL's even possible? If not, what would be the alternative? Using different ports for different applications? If it is a "valid" approach, why do I have access to just one but not both apps at with this configuration.
The purpose of the VirtualHost directive is to provide different applications based on the requested hosts.
To resolve an application based on the directory, you may use the Alias directive instead of VirtualHost.

current directory for multiple django instances on one Apache

We are trying to run multiple django instances on the same Apache server. All sites share the same code (and settingsfile), but have a different working directory (with wsgi.py and a configfile with databaseparameters) and a different database. Version of Django is 1.6.2. The layout is as follows:
/bin/manage.py
/tttb/settings.py
/site1/wsgi.py
/config.txt
/site2/wsgi.py
/config.txt
Below is the current format of httpd.conf.
NameVirtualHost *:80
WSGIPythonPath "Z:/projects/bin/tttb"
# site1
# =======================================
<VirtualHost *:80>
ServerName site1
WSGIScriptAlias "/djangosite1" "Z:/projects/site1/wsgi.py"
<Directory "Z:/projects/site1">
Order allow,deny
Allow from all
AllowOverride None
Options None
Require all granted
</Directory>
</VirtualHost>
# site1
# =======================================
<VirtualHost *:80>
ServerName site2
WSGIScriptAlias "/djangosite2" "Z:/projects/site2/wsgi.py"
<Directory "Z:/projects/site2">
Order allow,deny
Allow from all
AllowOverride None
Options None
Require all granted
</Directory>
</VirtualHost>
Our wsgi.py files look like this:
import os,sys
bindir = os.path.join("Z:\\","projects","bin")
if bindir not in sys.path:
sys.path.append(bindir)
os.chdir(os.path.dirname(os.path.abspath(__file__)))
os.environ['DJANGO_SETTINGS_MODULE'] = 'tttb.settings'
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
Contrary to similar questions on StackOverflow (see here or here) we are running on windows (XAMMP) so using WSGIApplicationGroup or WSGIProcessGroup is not possible. However, as first glance this setup works. We have even tested it both with and without virtualhosts. In both case it works OK.
Except for one detail: when we need to determine the current working directory ("."), the instances get mixed up. eg.
myfile = open(os.path.join(".","someresultfile.txt"),"r")
Initially, -immediately after restart of Apache-, the workdir is still determined correctly, but after having browsed to both sites once, the workingdirectory seems to get locked into one of both paths.
Probably there are other, similar leaks that we are not aware of, but this is the most obvious. We have also tried this without using virtualhosts, and it gives the same result.
Another weird behavior is that when using virtualhosts as above, the url http://www.domain.com/djangosite1 (on the general hostname) is still pointing to one of the djangosites. However, I would expect that it should should point to the subdirectory htdocs/djangosite1 (and fail, as this directory does not exist).
Any help on this one is appreciated.

how to serve website with apache over the internet?

I have somehow managed to serve both my project app and its static files on the apache. But only I can see my webpage, by typing localhost and by my IPv4 address. And I can't see my webpage from the other's computer. In my http.conf, it is Listen 80. I don't know much about this. I even registered on a free dynamic DNS provider, but even from that url I can only see It works message. I really suck at these things. Please guide me here. Thank you.
snippet of http.conf:
WSGIScriptAlias / C:/Users/robin/web/etc/etc/etc/wsgi.py
WSGIPythonPath C:/Users/robin/web/etc/etc
<Directory C:/Users/robin/web/etc/etc>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
#Alias /robots.txt /path/to/mysite.com/static/robots.txt
#Alias /favicon.ico /path/to/mysite.com/static/favicon.ico
AliasMatch ^/([^/]*\.css) C:/Users/robin/web/etc/etc/static/styles/$1
#Alias /media/ /path/to/mysite.com/media/
Alias /static/ C:/Users/robin/web/etc/etc/static/
<Directory C:/Users/robin/web/etc/etc/static>
Order deny,allow
Allow from all
</Directory>
#<Directory /path/to/mysite.com/media>
#Order deny,allow
#Allow from all
#</Directory>
WSGIScriptAlias / C:/Users/robin/web/etc/etc/etc/wsgi.py
<Directory C:/Users/robin/web/etc/etc/etc>
<Files wsgi.py>
Order allow,deny
Allow from all
</Files>
</Directory>
You need to do the following:
1. Ensure that the server is publicly accessible
To do this you need to ensure
Port 80 is routed through your router to the servers internal IP address. This is called NAT. If you are using a Cisco router you can see the instructions here.
Any firewalls are configured not to block the traffic on port 80. If you are using Linux you would most likely need to configure iptables. If you are using Windows you can do this on the windows firewall. If you are using a third party firewall you need to search google to find out how to do it.
You need to ensure you are using the public ip address to connect. You can find this by using a website such as http://whatismyipaddress.com/
2. Setup the virtual hosts on apache
The reason you are only seeing the "It Works" message is because Apache is pointing to the default web root on the server for that domain name. To fix this, you need to setup a virtualhost for the domain name, and point it to the root directory of your application. Instruction for this can be found here.
Under for virtual hosts in your apache config, you could add something like this:
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin [email]#[address].com
DocumentRoot [directory root]
ServerName [registered domain name without www eg: something.com]
ServerAlias *.[registered domain name without www]
</VirtualHost>
The "NameVirtualHost *:80" might already exist, so search the config first. Usually it is in there and commented out by default, so you might just need to remove the #.
Note: ALWAYS backup the config by copying the contents to another file. Also restart apache after you make the adjustment.

apache2+mod_wsgi with multiple django process in each host name

I want to run multiple django project with multiple host name.
If use come from www.momsy.org, it goes to /var/web/momsy.git.org
other if come from www.momsy.net, it goes to /var/web/momsy.git.net
This is to log and analyze where he/she came from.
So, following does not work because [WSGIPythonPath cannot occur within section].
But I can't use http.conf because i need 'servername' variable.
How Can I solve this problem?
ServerAdmin webmaster#localhost
ServerName www.momsy.org
WSGIScriptAlias / /home/web/momsy.git.kr/momsy/wsgi.py
WSGIPythonPath /home/web/momsy.git.kr
<Directory /home/web/momsy.git.kr/momsy>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
A
Use daemon mode, delegate each Django instance to a separate set of processes, and set python-path against each daemon process group as required for each. See:
http://blog.dscpl.com.au/2012/10/why-are-you-using-embedded-mode-of.html
http://blog.dscpl.com.au/2012/10/requests-running-in-wrong-django.html
Otherwise set the sys.path in the WSGI script file and not in the Apache configuration.
http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango

Deploying multiple django apps on Apache with mod_wsgi

I want to deploy two different django apps in the same host: The first will correspond to the url /site1 and the second to the url /site2. Here's my configuration:
LoadModule wsgi_module modules/mod_wsgi.so
WSGIScriptAlias /site1 /var/www/py/site1/site1/wsgi.py
WSGIScriptAlias /site2 /var/www/py/site2/site2/wsgi.py
WSGIPythonPath /var/www/py/site1:/var/www/py/site2
<Directory "/var/www/py/site1/site1">
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
<Directory "/var/www/py/site2/site2">
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
Also here's the wsgi.py file for both applications
import os
import sys
path = '/var/www/py/site1'
if path not in sys.path:
sys.path.append(path)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "site1.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
Now, here's my problem. When I go to my server, let's say http://app1.sites.gr/site1 it some times loads site1, and some other times it loads site2 !!!! The same goes when I visit http://app1.sites.gr/site2 ... Sometiems I get the welcome page for site1, sometimes I get the welcome page for site2 ! I am hitting F5 and getting different welcome pages. I have checked everything for the previous hours and did not find anything strange...
Please, tell me what could be the problem before I go crazy ...
Thanks !
This is a problem with the wsgi.py file generated by Django 1.4. It will not work where trying to host two distinct Django instances in the same process, even though in separate sub interpreters.
Change:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "site1.settings")
to:
os.environ["DJANGO_SETTINGS_MODULE"] = "site1.settings"
Or better still use daemon mode and delegate each to run in distinct daemon process groups.
That is, instead of:
WSGIScriptAlias /site1 /var/www/py/site1/site1/wsgi.py
WSGIScriptAlias /site2 /var/www/py/site2/site2/wsgi.py
WSGIPythonPath /var/www/py/site1:/var/www/py/site2
use:
WSGIDaemonProcess site1 python-path=/var/www/py/site1
WSGIScriptAlias /site1 /var/www/py/site1/site1/wsgi.py process-group=site1 application-group=%{GLOBAL}
WSGIDaemonProcess site2 python-path=/var/www/py/site2
WSGIScriptAlias /site2 /var/www/py/site1/site2/wsgi.py process-group=site2 application-group=%{GLOBAL}
UPDATE
Note that there is a whole blog post about this and other causes now.
http://blog.dscpl.com.au/2012/10/requests-running-in-wrong-django.html
Your apps listen on the same port, and there doesn't seem to be a proxy that delegates them to different ones.
You either have to setup VirtualHosts within apache or use Nginx, lighttpd or something else to create a proper proxy
Graham Dumpleton's response is the one you probably want to read closest, but I would suggest saving yourself a lot of heartburn by hosting your two Djangos at the root of different subdomains rather than at non-root locations on the same domain. There are lots of gotchas for running non-root Django sites IMHO.
Good luck!