apache2+mod_wsgi with multiple django process in each host name - django

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

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>

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.

Routing http request to WSGI app (Flask + Apache)

My Flask based web app is ready to go, and I'm currently connecting it up to Apache 2.2.
When I start Apache serving the app from root
WSGIScriptAlias / /var/www/path/to/script.wsgi
everything works as expected. However, I want to serve multiple versions of the WSGI script that will connect to different databases, which means I need to serve each with a unique alias:
WSGIScriptAlias /firstscript /var/www/path/to/first/script.wsgi
...
WSGIScriptAlias /secondscript /var/www/path/to/second/script.wsgi
When I try to access one of these:
www.example.com/firstscript
the WSGI app loads, but all http requests are still routed to the root. What is the best way for me to reroute all http requests to my WSGI app? Here is my apache config:
WSGIPythonHome /usr
WSGIPythonPath /var/www/path/to/first
<VirtualHost *>
WSGIDaemonProcess firstscript user=apache group=apache threads=5 python-path=/lib/python2.7/site-packages
WSGIScriptAlias /firstscript /var/www/path/to/first/script.wsgi
<Directory /var/www/path/to/first>
WSGIProcessGroup %{GLOBAL}
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
EDIT:
I've followed the (much appreciated) recommendations from #grahamdumpleton, which have helped me to clean up my Apache configs, but my http requests continue to be routed to the Apache server's root, rather than the WSGI app. Here's my current config:
WSGIPythonHome /usr
WSGIPythonPath /var/www/path/to/first
<VirtualHost *>
WSGIDaemonProcess firstscript threads=5
WSGIScriptAlias /firstscript /var/www/path/to/first/script.wsgi process-group=firstscript application-group=%{GLOBAL}
<Directory /var/www/path/to/first>
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
I've also tried adding a process group under the directory tag: WSGIProcessGroup firstscript, which also failed to have any impact.
The following should be removed for a start:
WSGIProcessGroup %{GLOBAL}
This is causing everything to be handled in the same Python interpreter, and not even in the daemon process group, so likely they are interfering with each other.
Using:
python-path=/lib/python2.7/site-packages
is also wrong. You don't need that when using system Python, and python-path is wrong way of referring to a virtual environment anyway.
Finally you don't need:
user=apache group=apache
either as defaults to using the Apache user already.
That first change may be enough to get it working as each application will now run in a separate sub interpreter of the daemon process group.
If that doesn't work, you should create more than one daemon process group and delegate each to a separate daemon process group.
That would be done using:
WSGIDaemonProcess firstscript threads=5
WSGIScriptAlias /firstscript /var/www/path/to/first/script.wsgi process-group=firstscript application-group=%{GLOBAL}
WSGIDaemonProcess secondscript threads=5
WSGIScriptAlias /secondscript /var/www/path/to/second/script.wsgi process-group=secondscript application-group=%{GLOBAL}

Python Djangocms Install App WSGI vs 8000 port

I follow the tutorial http://docs.django-cms.org/en/develop/introduction/plugins.html, when I install the Polls App as indicated it is visible on 8000 port But not in WSGI/Apache mode (error message no module polls). To see the APP polls I need to copy Polls application files in the default root directory. Idem with the Aldryn Blog News. I guess I have to specify some more PATH in wsgi mode to help Python to find the modules. Where and how to do that in my Virtualenv, to be also effective when I deploy all the stuff on a remote platform ?
Thanks for any help
Thank you for your interest. IMHO I don't think the problem is on APACHE configuration, everything is OK and DjangoCMS works without polls. Herinafter the conf file, the domain is a local virtual one.
DocumentRoot "/var/www/djangocms"
WSGIScriptAlias / /var/www/djangocms/default/wsgi.py
ServerName djangocms.net
Alias /static/ /var/www/djangocms/default/static/
Options +ExecCGI
Order Allow,Deny
Allow from All
ErrorLog "logs/errordjangocms_log"
LogLevel error
Marcel
Here is a working config i use
<VirtualHost *:80>
ServerName iot.mydomain.com
ServerAlias iot
WSGIDaemonProcess iot.local python-path=/home/name/PycharmProjects/iotdata
WSGIProcessGroup iot.local
WSGIScriptAlias / /home/name/PycharmProjects/iotdata/iotdata/wsgi.py process-group=iot.local
#WSGIPythonPath /home/name/PycharmProjects/iotdata
Alias /static/ /home/name/PycharmProjects/iotdata/static/
<Directory /home/name/PycharmProjects/iotdata>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
<Directory /home/name/PycharmProjects/iotdata/static>
Require all granted
</Directory>
</VirtualHost>

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.