Django and Apache WSGIScriptAlias not handling deep URLs - django

The tutorials, guides and even other posts here on SO dealing with how to deploy Django on Apache seem to assume that by default when using the WSGIScriptAlias directive all deep links under that alias are handled by the script as well (usually demonstrated by visiting the '/' and /admin page). For me that seems not to be the case. Requests to the root at api.mydomain.com/v1/ are handled by my Django app, but api.mydomain.com/v1/users/ (with or without trailing slash) generate Apache's 404 "Not Found. The requested URL was not found on this server." message.
Is there a setting I've missed somewhere that handles these deep links? For example for SPA's I'm used to having to redirect all requests to a front controller.
Here's my Apache configuration:
<VirtualHost *:443>
ServerName api.mydomain.com
WSGIPassAuthorization On
WSGIScriptAlias /v1/ /var/www/django_app/django_app/wsgi.py
SSLCertificateFile /etc/letsencrypt/live/api.mydomain.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/api.mydomain.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>

Remove the trailing slash from the WSGIScriptAlias directive's URL-path:
WSGIScriptAlias /v1 /var/www/django_app/django_app/wsgi.py

Related

Configuring Flask for Shibboleth SSO

I've rewritten my department's web server using Flask and now I need to configure everything to work with Shibboleth SSO. The old .htaccess files still exist in each directory, but I don't know what else to do. I've contacted the university's IT department and the best advice they had was that the shib.conf file should handle what necessary for authentication.
Below is an example of what my .conf file looks like for the site. Since the server has a mix of pages written with PHP and pages written with Python, I've added the AliasMatch to handle these:
<VirtualHost *:443>
ServerName my_fake_site
...
AliasMatch ^\/((?:flask_dir1|flask_dir2).*)\.((css|php|png)$((?:\?.*)?)) /var/www/html/app/$1.$2
AliasMatch ^\/(.*)\.(css|html|php|png) /var/www/html/$1.$2
WSGIDaemonProcess main_proc processes=8 python-home=/var/www/html/venv
WSGIScriptAlias / /var/www/html/wsgi.py
<Directory /var/www/html/>
WSGIProcessGroup main_proc
WSGIApplicationGroup %{GLOBAL}
Require all granted
</Directory>
SSLEngine on
...
</VirtualHost>
WSGIPythonPath /var/www/html
WSGIPythonHome /var/www/html/venv
So my question is what exactly should I be looking for and what particular things should I ask IT about? I've seen a handful of SSO tutorials online, but they all involve writing a custom sign-in page, whereas I believe the university has a dedicated server to handle these (currently, pages redirect to a url with idp in the address -- I'm trying not to reveal too much sensitive information).

Reconfiguring Apache to serve website root from new php source and specific sub-urls from old django site

How do I make a django website (in a apache/mod_wsgi/django setup) which is configured to serve from the root url, serve only for specific sub-url but not the root url? The root url shall be served from a new source (php). All this with a minimum of reconfiguration.
Currently the condensed virtualhost config looks like this
<VirtualHost *:80>
ServerAdmin admin#mysite.com
ServerName mysite.com
# mappings to django
WSGIScriptAlias / /opt/mysite/mysite.wsgi
<Directory /opt/mysite>
Order allow,deny
Allow from all
</Directory>
# mappings to wordpress
Alias /wp/ /var/www/mysiteWP/
<Location "/var/www/mysiteWP/">
Options -Indexes
</Location>
Alias /show/ /var/www/mysiteWP/
Alias /collection/ /var/www/mysiteWP/
</VirtualHost>
As you can see django and php(wordpress) are running side by side. Wordpress just serving mysite.com/show/ and mysite.com/collection/. Django is serving the rest, including the root url mysite.com. This configuration works.
What I want to do now, is, I want to make wordpress serve everything except some specific urls which should be served by django. E.g. django should just serve mysite.com/shop/ and mysite.com/news/ but nothing else, also excluding mysite.com.
How would I do this with a minimum of reconfiguration?
Thanks for your answers and hints.
Props to Graham Dumpleton. He answered another question of the exact same kind in this Q&A: Django (wsgi) and Wordpress coexisting in Apache virtualhost.
In short, after configuring Apache so the root url is served from php, the solution to route specific sub urls to django, but making it think its mount point is still the root, is WSGIScriptAliasMatch.
To this (example)problem the simple addition to the apache virtual host config was this:
WSGIScriptAliasMatch ^(/(shop|news)) /opt/mysite/mysite.wsgi$1
The whole virtual host config for this example is:
<VirtualHost *:80>
ServerAdmin admin#mysite.com
ServerName mysite.com
# mappings to django
WSGIScriptAliasMatch ^(/(shop|news)) /opt/mysite/mysite.wsgi$1
<Directory /opt/mysite>
Order allow,deny
Allow from all
</Directory>
# mappings to wordpress
DocumentRoot /var/www/mysiteWP/
<Location "/var/www/mysiteWP/">
Options -Indexes
</Location>
</VirtualHost>

How set different alias in django wsgi configuration?

In my configuration, I am using
WSGIScriptAlias /site1 /Library/WebServer/Documents/sampleapp/sampleapp/wsgi.py
instead of
WSGIScriptAlias / /Library/WebServer/Documents/sampleapp/sampleapp/wsgi.py
because my / has some other application hosted. Why this is not working? I am getting 404 page not found, but accroding to my understanding http://127.0.0.1/site1 should take me to django home page. Instead of I am getting this,
Full Configuration
<VirtualHost *:80>
WSGIDaemonProcess sampleapp python-path=/Library/WebServer/Documents/sampleapp:/Library/WebServer/Documents/sampleapp/env/lib/python2.7/site-packages
WSGIProcessGroup sampleapp
WSGIScriptAlias /site1 /Library/WebServer/Documents/sampleapp/sampleapp/wsgi.py
</VirtualHost>
This doesn't seem to have anything to do with your WSGI configuration. As you can see from the error message (which really you should have cut and pasted the text, rather than a screenshot), you have only defined URL patterns beginning with "djangoapp" and "admin". So you would need to go to "/site1/djangoapp" or "site1/admin/" to see your site. If that isn't what you want, then you'll need to change your urls.py.

403 forbidden when Django folder is located in /root

I'm trying to deploy my local Django site on my Ubuntu 12.04 server. I followed a tutorial, and everything seems to work fine, except Apache won't allow me to store my Django project under /root. More specifically, I get a 403 Forbidden when trying to access my site.
I suspect I need to configure my virtual host in a different manner. Any ideas?
Here is /etc/apache2/sites-available/mysite
<VirtualHost *:80>
ServerAdmin me#mysite.com
ServerName mysite.com
ServerAlias www.mysite.com
WSGIScriptAlias / /root/me/index.wsgi
Alias /static/ /root/me/static/
<Location "/static/">
Options -Indexes
</Location>
ErrorLog /var/log/mysite/error.log
</VirtualHost>
Thanks for the comments. I'll mark this as the correct answer for future reference.
I ended up moving the whole Django project to /home/anotheruser, and defined DocumentRoot as per Aamir Adnans suggestion. After service apache2 restart it started working.

Django (wsgi) and Wordpress coexisting in Apache virtualhost

I have a Django project that I need mounted at two different subdirectories of my url, and I need Wordpress running at /. So:
*.example.com - WordPress
*.example.com/studio - django
*.example.com/accounts - django
Here's the httpd.conf that I have so far:
<VirtualHost *:80>
ServerName wildcard.localhost
ServerAlias *.localhost
AddType application/x-httpd-php .php
DocumentRoot /var/empty
Alias /site_media/ /home/zach/projects/python/myproject/static/
Alias /media/ /home/zach/projects/python/myproject/env/lib/python2.6/site-packages/django/contrib/admin/media/
Alias / /home/zach/projects/python/myproject/wordpress/
WSGIScriptAlias /accounts /home/zach/projects/python/myproject/app/privio.wsgi
WSGIScriptAlias /studio /home/zach/projects/python/myproject/app/privio.wsgi
<Directory /home/zach/projects/python/myproject/app>
Order allow,deny
Allow from all
</Directory>
<Directory /home/zach/projects/python/myproject/wordpress>
Order allow,deny
Allow from all
</Directory>
Before I added the config for WordPress, the Django app was working fine. But with this new setup I am able to see the WordPress install at /, but the Django app isn't getting served. I'm sort of a noob at Apache config - what am I missing?
Replace:
DocumentRoot /var/empty
with:
DocumentRoot /home/zach/projects/python/myproject/wordpress
Remove:
Alias / /home/zach/projects/python/myproject/wordpress/
Replace:
WSGIScriptAlias /accounts /home/zach/projects/python/myproject/app/privio.wsgi
WSGIScriptAlias /studio /home/zach/projects/python/myproject/app/privio.wsgi
with:
WSGIScriptAliasMatch ^(/(accounts|studio)) /home/zach/projects/python/myproject/app/privio.wsgi$1
In other words, use DocumentRoot to refer to wordpress that needs to be at root of site and not Alias directive.
The WSGIScriptAliasMatch is so Django itself thinks it is still mounted at root site even though only nominated sub URLs of it are actually passed through. This simplifies things for urls.py.
Note that the $1 at end of WSGI script path is important, so don't leave it off.
Paging Graham Dumpleton :)
I'd venture a guess that the line
Alias / /home/zach/projects/python/myproject/wordpress/
overrides everything below it. Therefore any requests to /accounts will be processed by the wordpress application rather than by the Django application.
From the documentation:
Mounting At Root Of Site
If instead you want to mount a WSGI application at the root of a site, simply list '/' as the mount point when configuring the WSGIScriptAlias directive.
WSGIScriptAlias / /usr/local/www/wsgi-scripts/myapp.wsgi
Do note however that doing so will mean that any static files contained in the DocumentRoot will be hidden and requests against URLs pertaining to the static files will instead be processed by the WSGI application.