Django virtual host setup. Apache mod_wsgi - django

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.

Related

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 urls.py catches all existing HTML projects' urls on WampServer

I had WampServer working perfectly with Aptana IDE using multiple folders for different html projects. Then I installed Django and added this to Apache's httpd.conf:
Alias /images/ "c:/wamp/www/daas/templates/images/"
<Directory "c:/wamp/www/daas/images>
Order allow,deny
Allow from all
</Directory>
WSGIScriptAlias / "c:/wamp/www/daas/apache/django.wsgi"
<Directory "c:/wamp/www/daas/apache">
Allow from all
</Directory>
<VirtualHost *:80>
ServerName 127.0.0.1
DocumentRoot c:/wamp/www/daas/
</VirtualHost>
and now the virtual hosts I had for html projects...
NameVirtualHost 127.0.0.1
<VirtualHost 127.0.0.1>
ServerName localhost
DocumentRoot 'C:\wamp\www'
</VirtualHost>
<VirtualHost 127.0.0.1>
ServerName projectA.local
DocumentRoot 'C:\wamp\www\projectA'
</VirtualHost>
don't load correctly because Django's urls.py is catching all urls The current URL, projectA, didn't match any of these regular expressions, even the ones not inside the Django's projects.
How can I solve this? On Djano's side or on Apache's side? Here (How do i run Django and phpmyadmin on apache webserver on ubuntu) is a similar problem but the solution didn't worked for me. And luckily localhost/phpmyadmin works correctly but localhost/projectA doesn't.
It looks like WSGIScriptAlias / "c:/wamp/www/daas/apache/django.wsgi" in your httpd.conf catches all requests on apache.

Pointing subdomain to specific url in Django

I have a Django site, working on mod_wsgi and Apache. (eg, example.com) I made a subdomain (eg, info.example.com) and want to point it to app in the existing project. Is it possible, that I have one urls.py file, and example.com/info and info.example.com point to the same view in Django project (without duplicating the whole project in different directory)?
If so, how can I do it? Currently the example.com virtalhost config for the existing project looks like this:
<VirtualHost 12.34.56.78:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /srv/www/Example/Pub_html
WSGIScriptAlias / /srv/www/Example/example.wsgi
<Directory /srv/www/Example/Example>
Order allow,deny
Allow from all
</Directory>
Alias /robots.txt /srv/www/Example/robots.txt
Alias /favicon.ico /srv/www/Example/favicon.ico
....
</VirtualHost>
Thanks in advance!
UPDATE: I did a quick fix here by creating another Django project which shares database tables and template folder with the existing one. It works, but it's not really DRY :)
I'd do this with a reverse proxy, I reckon. If you have mod_proxy and mod_proxy_html available, try something like this:
<VirtualHost 12.34.56.78:80>
ServerName info.example.com
ProxyPass / http://example.com/info
SetOutputFilter proxy-html
<Location />
ProxyPassReverse /info/
ProxyHTMLURLMap /info/ /
</Location>
ProxyHTMLURLMap http://example.com/info /
</VirtualHost>
the details may not be quite right, so let me know if you can't get it to work.

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.