How to put Django within a subdirectory of a website? - django

I want to run WordPress at mysite.com/, because it's easier to edit sales copy and such. I want to run my Django site within mysite.com/members/
httpd.conf:
<VirtualHost *:80>
ServerName mysite.com
ServerAlias www.mysite.com
DocumentRoot /var/www/mysite.com
WSGIDaemonProcess mysite python-path=/var/www/mysite.com/mysite:/var/www/mysite.com/
WSGIProcessGroup mysite
WSGIScriptAlias / /var/www/mysite.com/mysite/mysite/wsgi.py
</VirtualHost>
What exactly do I need to do so that Django runs within the /members/ directory on my domain/website?

Use:
WSGIScriptAlias /members /var/www/mysite.com/mysite/mysite/wsgi.py
Do be aware that by doing that, since you have made the mistake of setting DocumentRoot to be a parent directory of your source code, people will be able to download the source code, including sensitive information in the settings module.
So, do not set DocumentRoot to be what you have. Have it refer to an en empty directory, of the default htdocs directory for the whole server.

To follow up on the previous answer and comments, here's an example of how I might do this if I had to:
<VirtualHost *:80>
# WordPress
ServerName mysite.com
ServerAlias www.mysite.com
DocumentRoot /var/www/mysite.com
# Django
WSGIDaemonProcess mysite python-path=/var/django/django_project/virtualenv/django_project
WSGIProcessGroup mysite
WSGIScriptAlias /members /var/django/django_project/django_project/wsgi.py
Alias /members/static/ /var/django/django_project/static/
</VirtualHost>
Definitely understand the security concerns raised in comments, however.

Related

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

Issue with my Ubuntu Apache Conf file. (Forbidden You don't have permission to access / on this server.)

This below is the conf file for https://github.com/treeio/treeio
I used this method: http://goo.gl/KTdlUT , to configure treeio to my Azure Based Ubuntu VM but still it says: Forbidden You don't have permission to access / on this server.)
<virtualhost *:80>
ServerAdmin abcd#xyz.com
ServerName abcd.net
ServerAlias abcd.net
DocumentRoot "/home/User/treeio"
<Directory /home/Userk/treeio/>
<Directory /home/User/treeio/>
Order allow,deny
Allow from all
</Directory>
WSGIDaemonProcess treeio.djangoserver processes=2 threads=15 display-name=%{GRO$
WSGIProcessGroup treeio.djangoserver
WSGIScriptAlias / /home/User/treeio/wsgi
ErrorLog "/home/User/treeio/log/error.log"
CustomLog "/home/User/treeio/log/access.log" combined
</virtualhost>
It seems like some configurations prefer Django projects to be installed in the /var/www/ directory, even though up until now the home folder had been the recommended location. This seems to make sense from a security perspective.
In addition, Microsoft has the simplest step-by-step instructions which actually work with Unbunto 14.04 in Azure:
http://azure.microsoft.com/en-us/documentation/articles/virtual-machines-python-django-web-app-linux/

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.

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.

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.