8

Is it possible and correct to have multiple sites under single django project. So that there will be globally shared sittings file,urls files along with global shared 'apps' for all the sites and a common admin interface for all the sites under the single django project. Each site might have its own setting,urls and templates that will be overridden or imported dynamically into the parent settings file.

Could anyone please tell me how to achieve this using Python2.6 + Django 1.2.1 + Apache2.2 + mod_wsgi. As I am a bit confused with the virtualhost need to be supplied while using the mod_wsgi.

As per the above requirement I am planning to keeping the wsgi file in the main django project directory so that the main settings will be imported and within the main setting I am planning to import the requested sites settings dynamically.Is it possible this way using Apache + mod_wsgi. Please advice.

OR

Instead of global django project do I have to make a globally shared module for importing the global setting and url details into site specific settings and urls.

I would like to make better reuse of the code rather than making redundant changes in each sites.

Please advice.

Joseph
  • 183
  • 1
  • 4
  • 14

2 Answers2

11

This following is the same way that I did it. I borrowed from http://michal.karzynski.pl/blog/2010/10/19/run-multiple-websites-one-django-project/

Basically, you'll create a virtualhost entry on your http.conf file for each domain.

# Virtual hosts setup
NameVirtualHost *
<VirtualHost *>
    ServerName example1.com

    WSGIDaemonProcess APPLICATION_NAME processes=5 python-path=/home/USERNAME/webapps/APPLICATION_NAME:/home/USERNAME/webapps/APPLICATION_NAME/lib/python2.6 threads=1
    WSGIScriptAlias / /home/USERNAME/webapps/APPLICATION_NAME/domain1.wsgi
</VirtualHost>

<VirtualHost *>
    ServerName example2.com


    WSGIDaemonProcess APPLICATION_NAME_www processes=5 python-path=/home/USERNAME/webapps/APPLICATION_NAME:/home/USERNAME/webapps/APPLICATION_NAME/lib/python2.6 threads=1
    WSGIScriptAlias / /home/USERNAME/webapps/APPLICATION_NAME/domain2.wsgi
</VirtualHost>

Then you'll want to create two different wsgi files for each domain and put them in the directory that holds your project. The WSGIScriptAlias is the path to the wsgi file so make sure they are the same...

Example Wsgi file:

import os
import sys
from django.core.handlers.wsgi import WSGIHandler

os.environ['DJANGO_SETTINGS_MODULE'] = 'PROJECT_NAME.domain1_settings' # or PROJECT_NAME.domain2_settings
application = WSGIHandler()

Then you'll want to create two extra settings files... so you'll have

settings.py
domain1_settings.py
domain2_settings.py

domain1_settings.py and domain2_settings.py will import settings.py:

example of domain1_settings.py:

from settings import *

SITE_ID = 1

ROOT_URLCONF = 'domain1_urls'

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',

# other apps specific to this domain
)

Finally, you'll want to create the two separate urls files..

domain1_urls.py and domain2_urls.py

domain1_urls.py will be the default for site_id 1, and domain2_urls.py will be the default for site_id 2.

teewuane
  • 5,524
  • 5
  • 37
  • 43
7

Yes, this is entirely possible. The sites can even share data.

The sites framework enables this - for documentation, see here:

https://docs.djangoproject.com/en/1.11/ref/contrib/sites/

Irfan Ashraf
  • 2,430
  • 21
  • 20
andrewmu
  • 14,276
  • 4
  • 39
  • 37
  • Hi Andrew, thanks, but could you please explain how the apache virtual host will be created for each site using mod_wsgi and how the requests will be handled and passed into its respective sites dynamically using mod_wsgi. Won't each site have its own settings and urls? Please advice – Joseph Jul 30 '10 at 09:39
  • I haven't done it, but I'd expect you would define a virtual host for each site, configured to all use the same django project. Then within the application, I imagine you would use `Site.objects.get_current()` to select e.g. the template dir, etc for the individual sites. – andrewmu Jul 30 '10 at 10:49
  • Could you please explain how can we configure virtual sites for all the sites to configure to use the same django project. I am really confused in that section. Please advice – Joseph Jul 30 '10 at 12:48
  • If you can set up two or more virtual hosts in apache (there is good documentation for this) and give them the same WSGI settings, does that work? – andrewmu Jul 30 '10 at 13:13