Multiple Django applications using virtualenv on Apache 2 on Ubuntu 11 - django

I've successfully setup one Django application using virtualenv on Ubuntu and Apache 2, using the WSGIPythonHome directive pointing to my virtualenv location. Now I am in need to create a separate Django application, that is going to run on Apache on a different port on the same Ubuntu server. I am wondering if there's a way to have Apache run multiple WSGIPythonHome instances? Currently with WSGIPythonHome being set to one virtualenv root, there's a problem with imports on the second Django app…

The best way to do this, I've discovered about a year ago, is to use WSGI as a daemon and set the python path in the daemon directive. Example is below
<VirtualHost *:80>
ServerName yourhost.com
<Directory />
Order deny,allow
#Require all granted
</Directory>
#Alias /static /opt/yourhost/static
WSGIScriptAlias / /opt/yourhost/wsgi.py
WSGIApplicationGroup %{GLOBAL}
WSGIDaemonProcess yourhost.com python-path=/opt/yourhost:/opt/yourhost/venv/lib/python2.7/site-packages processes=2 threads=15 display-name=%{GROUP}
WSGIProcessGroup yourhost.com
</VirtualHost>
WSGISocketPrefix /var/run/wsgi

You should do this with separate virtual hosts in Apache. Each one can listen to a particular port, and can have its own separate WSGI directives.

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>

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>

Apache doesn't load static folder for Django project

I have a Django 1.4.5 project called mp which I'm trying to run on my localhost using Apache 2.4. Following the official tutorial for Django with mod_wsgi
(How to use Django with Apache and mod_wsgi) I managed to display my Django page when I visit http://127.0.0.1:8801/.
My project folder mp is located in /opt/masterportal/mp, and the static files are located in /opt/masterportal/mp/mp/static.
This is my masterportal.conf file in /etc/apache2/sites-available:
Listen 8081
<VirtualHost *:8081>
ServerAdmin my#mail.adress
XSendFilePath /opt/masterportal/mp/mp/uploads/
<Files *.*>
XSendFile On
</Files>
WSGIDaemonProcess masterportal python-path=/opt/masterportal/mp:/opt/masterportal/mp/env/dev/lib/python2.7/site-packages
WSGIProcessGroup masterportal
WSGIScriptAlias / /opt/masterportal/mp/mp/apache/wsgi.py
Alias /static /opt/masterportal/mp/mp/static
<Directory /opt/masterportal/mp/mp/static>
Require all granted
</Directory>
<Location />
WSGIProcessGroup masterportal
Require all granted
</Location>
</VirtualHost>
However, the website at http://127.0.0.1:8801 can't find any of the static files. This is odd, because the exact same project works on the server of my university (where I don't have access to the apache configuration). So there must be something wrong with my Apache configuration, but I can't see what. I'm desperate for help.
Some general information: I'm using Django 1.4.5 (because this is the version on the university server), and Apache 2.4. The project runs in a virtualenv located here /opt/masterportal/mp/env. I also tried it with Alias /static/ instead of Alias /static, but that didn't work either. My apache2.conf is still original - I made no changes there.
Edit: Here's my configuration for the site in /etc/apache2/conf-available/:
<Location "/mp/2015/suse">
ProxyPass https://my-computername:8081/
ProxyPassReverse https://my-computername:8081/
RequestHeader set X-FORWARDED-PROTOCOL ssl
RequestHeader set X-FORWARDED-SSL on
</Location>
Try to move the lines
Alias /static /opt/masterportal/mp/mp/static
<Directory /opt/masterportal/mp/mp/static>
Require all granted
</Directory>
before
WSGIDaemonProcess masterportal python-path=/opt/masterportal/mp:/opt/masterportal/mp/env/dev/lib/python2.7/site-packages
WSGIProcessGroup masterportal
WSGIScriptAlias / /opt/masterportal/mp/mp/apache/wsgi.py
because the link with /static/some_static_files... might be forwared to the wsgi app instead of pointing to the static directory.

Django multiple sites with apache mod_wsgi: Request going to wrong site

I am trying to run 2 Django sites from one apache server using virtual hosts with mod_wsgi. The problem is that all my request are going to the same Django site(probably the first one loaded by Apache) even when I use the hostname for the other site. My sites are
https://staging-test.mydomain.com
and
https://staging.mydomain.com
Here is the content of my virtual hosts files. I have created the two files for deployment which I place in the site-enabled directory of my Apache installation(Ubuntu). Two files because it helps in dev-ops. Regardless, the config looks like
<VirtualHost _default_:443>
ServerAdmin webmaster#localhost
ServerName https://staging-test.mydomain.com
SetEnv PYTHON_EGG_CACHE /tmp
WSGIDaemonProcess https://staging-test.mydomain.com user=www-data group=www-data processes=10 threads=1 python-path=/home/talha/site1 display-name=wsgid
WSGIProcessGroup https://staging-test.mydomain.com
WSGIScriptAlias / /home/talha/site1/wsgi.py process-group=https://staging-test.mydomain.com application-group=%{RESOURCE}
WSGIPassAuthorization On
WSGIApplicationGroup %{RESOURCE}
</VirtualHost>
<VirtualHost _default_:443>
ServerAdmin webmaster#localhost
ServerName https://staging.mydomain.com
SetEnv PYTHON_EGG_CACHE /tmp
WSGIDaemonProcess https://staging.mydomain.com user=www-data group=www-data processes=10 threads=1 python-path=/home/talha/site2 display-name=wsgid
WSGIProcessGroup https://staging.mydomain.com
WSGIScriptAlias / /home/talha/site2/wsgi.py process-group=https://staging.mydomain.com application-group=%{RESOURCE}
WSGIPassAuthorization On
WSGIApplicationGroup %{RESOURCE}
</VirtualHost>
I have tried to use the WSGIDaemonProcess and WSGIProcessGroup to make the sites run in different processes, however the requests are still going to only one of them.
Any ideas what might be wrong here?
As someone pointed out, your initial problem is that you should not use a URI in the ServerName, it must be just the host name. Beyond fixing that, then make sure you read:
http://blog.dscpl.com.au/2012/10/requests-running-in-wrong-django.html
You are hitting a variation of the default VirtualHost problem described in that post due to wrong setting for ServerName.