How set different alias in django wsgi configuration? - django

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.

Related

Django and Apache WSGIScriptAlias not handling deep URLs

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

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 blank page

I am trying to get Django to work together with Apache using WSGI. The problem is that it simply returns an empty page (200 OK or 203 not modified).
I can't find any error upon restarting or when accessing the page in either error.log or django-error.log. There is an entry in django-access.log though.
WSGIPythonPath /web/django/dtest/
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /web/django
ServerName django.markv.nl
<Directory />
Options FollowSymLinks Includes
AllowOverride All
</Directory>
WSGIScriptAlias /wsgi-scripts/ /web/django/dtest/dtest/wsgi.py
LogLevel info
ErrorLog ${APACHE_LOG_DIR}/django-error.log
CustomLog ${APACHE_LOG_DIR}/django-access.log common
</VirtualHost>
I tried using the manage.py runserver, in which case it works fine, so it's really an apache/wsgi problem.
I tried disabling mod_php5 with no result (except that PHP stopped working).
Any clues what the problem is or where I can find an error? It's probably something trivial but I fail to find it...
You're serving your application at /wsgi-scripts/, which seems an odd thing to do, so you'll need to go to that URL to see it.
Note that you absolutely certainly do not want to set your DocumentRoot to the location of your Django files, although this is not the source of your problem.

Django virtual host setup. Apache mod_wsgi

I am hoping there is a simple answer to my question as I am not the most experienced with python and Apache.
I am trying to hook up Apache with mod_wsgi. I have used a virtual host to do so. see below:
<VirtualHost *:80>
ServerAdmin admin#example.com
ServerName testserver.com/django
#DocumentRoot /
WSGIScriptAlias / /home/mycode/mysite/scripts/django.wsgi
Alias /media/ /home/mycode/mysite/mysite/media/
Alias /adminmedia/ /opt/python2.7/lib/python2.7/site-packages/django/contrib/admin/media/
<Directory "/home/mycode/mysite/mysite/media">
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
This works great for my django project when I go to testserver.com instead of my php index page I get my django project.
What I am looking for is help with allow my php projects in /var/www/html/ and my django projects to coexist. I am trying to make it so to reach my django project I type testserver.com/django
Any help or guidance is greatly appreciated :)
Thanks!
Change:
WSGIScriptAlias / /home/mycode/mysite/scripts/django.wsgi
to:
WSGIScriptAlias /django /home/mycode/mysite/scripts/django.wsgi
The first argument is the mount point, you have it to be the root of the web server, so just change it to '/django' instead.

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.