current directory for multiple django instances on one Apache - django

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.

Related

How to deploy multiple django apps on apache in Windows?

I want to deploy multiple django apps on apache on Windows but only know how to deploy one.
Overriding the localhost of the Wamp Server I can deploy the app without problem but I need to deploy more and don't know how. I've sehen virtual hosts and think are good but don't know how to configurate them. Anyone know how can I do this? Thanks in advance.
hosting severel django apps with Apache is possible using virtual hosts (vhosts)
important to care about:
during config of Apache I recommend to start apache from command line as "httpd.exe" as in XAMPP or WAMP you will not see some of the initial start-up error messages in error.log files.
you can only use 1 python version even in different virt.env for each vhost as apache module mod_wsgi compilation needs to fit to it and is loaded once at startup of apache
something like this in httpd.conf (you should have this already in place because of your running single app config):
LoadFile "c:/.../python/python38/python38.dll"
LoadModule wsgi_module "c:/..../mod_wsgi.cp38-win_amd64.pyd"
for those starting from scratch:
activate virt.env.
> pip install mod_wsgi
> mod_wsgi-express module-config
will give above output (LoadFile ....) that you need to copy to httpd.conf
how to set path to virt.env and app folders:
with 1 host you would point to your virt.env by setting WSGIPythonHome and WSGIPythonPath to point to your app folders in httpd.conf:
WSGIPythonHome "d:/..../django_project/env_folder"
WSGIPythonPath "d:/..../django_project/app_name"
but: you can not place WSGIPythonHome/WSGIPythonPath inside the VirtualHost declaration in httpd-vhosts.conf .... it will cause an error message
Solution: set paths in wsgi.py dynamically and remove WSGIPythonHome/WSGIPythonPath from apache *.conf:
wsgi.py:
# replacement for WSGIPythonHome "d:/..../django_project/env_folder"
# choose one:
sys.path.append('d:/.../env_folder/lib/site-packages') # add individual virt.environment packages at the end of sys.path; global env packages have prio
sys.path.insert(0,'d:/.../env_folder/lib/site-packages') # add individual virt.environment packages at the beginning of sys.path; indiv. virt.env packages have prio over global env
# replacement WSGIPythonPath "d:/..../django_project/app_name"
sys.path.append('d:/.../django_project/app_name') # add indiv. app folder to search path
here is example for apache conf:
(why the dummy host: there is a (strange or buggy) behavior of apache ... if none of the virtual host names match the request, then automatically apache will dispatch the request to the first vhost in the config - no matter which server name is defined ther. This can lead to confusion because the total wrong app is called and an error messages will most certainly pop-up from inside django, not indicating that the error is on the Apache conf level. A dummy host with a simple index.html and an error message can make this tranparent)
httpd-vhost.conf:
<VirtualHost *:80>
ServerName Dumme_Host
DocumentRoot "d:/WEBSPACES/Dummy_Host"
<Directory d:/WEBSPACES/Dummy_Host>
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName xxxx1
WSGIScriptAlias / "d:/.... /wsgi.py" application-group=app_name1
Alias /media/ d:/.../media/
Alias /static/ d:/.../static/
<Directory d:/.../app_name1>
Require all granted
</Directory>
<Directory d:/.../media>
Require all granted
</Directory>
<Directory d:/.../static>
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName xxxx2
WSGIScriptAlias / "d:/.... /wsgi.py" application-group=app_name2
Alias /media/ d:/.../media/
Alias /static/ d:/.../static/
<Directory d:/.../app_name2>
Require all granted
</Directory>
.....
</VirtualHost>

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.

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.

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!

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

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.