Routing http request to WSGI app (Flask + Apache) - python-2.7

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}

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 can I deploy multiple django cms projects under same domain.I am using apache 2.2 and mod_wsgi

I need to deploy two Django Cms projects under the same domain name. I need to retrieve the two sites when calling the following domain.
http://rndbkw.tk
http://rndbkw.tk/blog
I have two wsgi configurations included in the httpd.conf
for http://rndbkw.tk
ServerName rndbkw.tk
WSGIDaemonProcess rnd python-path=/home/rndbkw/djangocms:/home/rndbkw/virtualenv2.7/lib/python2.7/site-packages/
WSGIProcessGroup rnd
WSGIScriptAlias / /home/rndbkw/djangocms/rnd/wsgi.py
ServerName rndbkw.tk
WSGIDaemonProcess blog python-path=/home/rndbkw/projects/djangocms:/home/rndbkw/projects/virtualenv2.7/lib/python2.7/site-packages/
WSGIProcessGroup blog
WSGIScriptAlias / /home/rndbkw/projects/djangocms/rnd/wsgi.py
But i cannot get back http://rndbkw.tk/blog
You have a few problems with your configuration of:
ServerName rndbkw.tk
WSGIDaemonProcess rnd python-path=/home/rndbkw/djangocms:/home/rndbkw/virtualenv2.7/lib/python2.7/site-packages/
WSGIProcessGroup rnd
WSGIScriptAlias / /home/rndbkw/djangocms/rnd/wsgi.py
ServerName rndbkw.tk
WSGIDaemonProcess blog python-path=/home/rndbkw/projects/djangocms:/home/rndbkw/projects/virtualenv2.7/lib/python2.7/site-packages/
WSGIProcessGroup blog
WSGIScriptAlias / /home/rndbkw/projects/djangocms/rnd/wsgi.py
Instead use:
ServerName rndbkw.tk
WSGIDaemonProcess blog python-home=/home/rndbkw/projects/virtualenv2.7 python-path=/home/rndbkw/projects/djangocms
WSGIScriptAlias /blog /home/rndbkw/projects/djangocms/rnd/wsgi.py process-group=blog application-group=%{GLOBAL}
WSGIDaemonProcess rnd python-home=/home/rndbkw/virtualenv2.7 python-path=/home/rndbkw/djangocms
WSGIScriptAlias / /home/rndbkw/djangocms/rnd/wsgi.py process-group=rnd application-group=%{GLOBAL}
Changes made were:
Mount blog at sub URL of /blog.
Move the WSGIScriptAlias for /blog before that for / so that it takes precedence, else that for / will always match first and nothing will ever get through the the blog site.
Per best practice, use python-home of WSGIDaemonProcess directive to specify the location of the virtual environment instead of adding site-packages using python-path.
Use process-group option to WSGIScriptAlias to indicate which daemon process group to use. This makes it more precise. Your use of WSGIProcessGroup wouldn't have worked as wasn't qualified to a Location or Directory scope so whichever of the two WSGIProcessGroup directives was last would have overridden the first.
Set application-groupto %{GLOBAL} to force use of main interpreter context of each daemon process. This solves problems with some third party extension modules for Python that will not work in sub interpreter contexts.
The last line in your configuration needs to be
WSGIScriptAlias /blog /home/rndbkw/projects/djangocms/rnd/wsgi.py

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.

Multiple Django applications using virtualenv on Apache 2 on Ubuntu 11

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.