Deploying multiple django apps on Apache with mod_wsgi - django

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!

Related

How to configure Apache to work with Django

I've followed the instructions on Django website for configuring Apache with my Django app on a CentOS 7 server. This included building mod_wsgi from sources to work with the installed python3.4.
Apache restarts without errors but when I hit my app with the URL
http://example.com/myapp/
I get a 503 error like:
Service Temporarily Unavailable
The server is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again later.
Apache/2.2.15 (CentOS) Server at example.com Port 80
I'm not sure how I troubleshoot what's wrong here. Can anyone help?
Details of the config:
My django app lives at /mnt/net/django/myapp
I've added the file wsgi.conf to my apache conf.d directory and it looks like this:
#LoadModule wsgi_module modules/mod_wsgi.so
# use python34 pip installes mod_wsgi
LoadModule wsgi_module "/usr/lib64/python3.4/site-packages/mod_wsgi/server/mod_wsgi-py34.cpython-34m.so"
#WSGIPythonHome "/usr"
Alias /robots.txt /mnt/net/django/myapp/static/robots.txt
Alias /favicon.ico /mnt/net/django/myapp/static/favicon.ico
Alias /media /mnt/net/django/myapp/media/
Alias /static/ /mnt/net/django/myapp/static/
<Directory /mnt/net/django/myapp/static>
Order deny,allow
Allow from all
</Directory>
<Directory /mnt/net/django/myapp/media>
Order deny,allow
Allow from all
</Directory>
# Allows URLs like example.com/myapp to forward to django
WSGIScriptAlias /myapp /mnt/net/django/myapp/myappsite/wsgi.py process-group=example.com
# Use the virtual env for the myapp site
#WSGIPythonHome /mnt/net/django/myapp/env-myapp-py3-4
# Need to use WSGIDaemon
WSGIDaemonProcess example.com python-home=/mnt/net/django/myapp/env-myapp-py3-4 python-path=/mnt/net/django/myapp
#WSGIPythonPath /mnt/net/django/myapp
<Directory /mnt/net/django/myapp/myappsite>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
How to use Django with Apache and mod_wsgi -- follow this: I have done it myself many times, it is very straight forward.
Tip: Create a document and record everything you do whilst setting this up, this way if it doesn't work then you can retrace your steps, but if it does work... Great, you have your very own guide to setting up an Apache server for Django.
Solved my problem (mostly)
The problem is that mod_wsgi with a daemon process tries to write a socket file into the apache logs directory and permissions are denied.
Solution is to tell apache another place to write the socket like this:
WSGISocketPrefix /var/run/wsgi

how to resolve You don't have permission to access / on this server

Hi I need help in integrating django with apache and mod_wsgi on centos6.
I am getting following error every time---"Forbidden You don't have permission to access / on this server."
My django project path= /home/mchauras/esapp/eswebsite
my apache version is 2.2.15
my .conf file looks like this----
<VirtualHost *:80>
DocumentRoot /home/mchauras/esapp/eswebsite/
Alias /static /home/mchauras/esapp/eswebsite/esapp/static
<Directory /home/mchauras/esapp/eswebsite/esapp/static>
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride None
Order deny,allow
Allow from all
</Directory>
<Directory /home/mchauras/esapp/eswebsite/eswebsite>
<Files wsgi.py>
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride None
Order deny,allow
Allow from all
</Files>
</Directory>
WSGIDaemonProcess esapp python-path=/home/user/myproject:/home/mchauras/esapp/eswebsite/myvenv/lib/python3.5/site-packages/
WSGIProcessGroup esapp
WSGIScriptAlias / /home/mchauras/esapp/eswebsite/eswebsite/wsgi.py
ErrorLog /home/mchauras/esapp/eswebsite/error.log
CustomLog /home/mchauras/esapp/eswebsite/access.log combined
</VirtualHost>
my wsgi.py file is like this---
import os
import sys
from django.core.wsgi import get_wsgi_application
sys.path.append('/home/mchauras/esapp/eswebsite')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "eswebsite.settings")
application = get_wsgi_application()
its looks like you did not login as a administrator user or maybe you need to changes and enable all file permission in the path
This specific error is usually always caused by the user that Apache runs as not being able to access the WSGI script file. It is not enough to just make the file itself readable to others. All the directories from '/' down to that directory must also be accessible to the user Apache runs as. As home directories for users are not usually readable by others, the Apache user cannot see into it. You are better off moving the application directory outside of your home directory.
Another possible cause, although one which usually results in a slightly different error, is having SELinux enabled where the profile for Apache httpd server on SELinux doesn't allow access to the directories where your application is.

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.

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.

setting up two Django websites under Apache with WSGI

I've set up a django website as described in the django docs: https://docs.djangoproject.com/en/dev/howto/deployment/modwsgi/
Now I want to setup another version of the site (different source dir, different database) to run on the same server. There are active users and flex apps who use app #1, so I want to keep app #1 access unchanged. I also rather not change the urls.py at all even for app #2.
I was thinking of different port for app #2
For example
http://192.168.1.1/load_book/123/ will load book from app #1
http://192.168.1.1:444/load_book/123/ will load book from app #2
I'm a complete noob to Apache and WSGI... how do I set it up?
What do you mean by they have the same URLs? The same hostname, perhaps?
Let's say you've got 2 apps:
http://example.com/your_app
http://example.com/my_app
These can both be Django apps, served by WSGI, on the same Apache instance. Using either Directory or Location directives in your apache conf to specify the .wsgi loader file as described in the django docs linked above:
<Location /your_app>
WSGIScriptAlias /your_app /path/to/mysite/apache/your_app/django.wsgi
...
</Location>
<Location /my_app>
WSGIScriptAlias /my_app /path/to/mysite/apache/my_app/django.wsgi
...
</Location>
The only real gotcha is that you'll need to tell your_app and my_app that they are no longer on the document root of the host. To do this, add a base_url parameter to your settings.py and prefix all of the entries in your urls.py with this param. This will ensure when the request comes through Apache, your python app can route it accordingly.
For an easy example of how this is done, have a look at the code for Bookworm, a Django app.
You can attatch the wsgi application to different sub-paths under the same domain. If you do this the paths to the views inside Django will still be the same. You do not have to modify the urls.py. In the following example Django will regard /site1 as the root of project1.
Check out http://code.google.com/p/modwsgi/wiki/InstallationInstructions for documentation on mod_wsgi.
<VirtualHost *:80>
ServerName www.example.com
WSGIDaemonProcess example
WSGIProcessGroup example
WSGIScriptAlias /site1 /home/django/project1/deploy/wsgi.py
<Directory /home/django/project1/deploy>
Order deny,allow
Allow from all
</Directory>
WSGIScriptAlias /site2 /home/django/project2/deploy/wsgi.py
<Directory /home/django/project2/deploy>
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
Now the two sites will run in the same daemon process using different python sub-interpreters.