fir3net
PPS-Firenetbanner-780.5x190-30-03-17

How to serve multiple domains from within a single Django project

Introduction

Within this article we will be showing you the necessary steps required to serve multiple domains from within a single Django project.
This example will based upon a project named 'myproject' and 2 domains named 'domain1' and 'domain2'. Below shows the file structure.

/opt/
`-- django
    |
    `-- myproject
        |-- __init__.py
        |-- domain1
        |   |-- __init__.py
        |   |-- domain1.wsgi
        |   |-- domain1_settings.py
        |   |-- domain1_urls.py
        |   |-- models.py
        |   |-- tests.py
        |   |-- views.py
        |-- domain2
        |   |-- __init__.py
        |   |-- domain2.wsgi
        |   |-- domain2_settings.py
        |   |-- domain2_urls.py
        |   |-- models.py
        |   |-- tests.py
        |   |-- views.py
        |-- manage.py
        |-- settings.py
        |-- templates
        |   |-- domain1-base.html
        |   |-- domain2-base.html
        `-- urls.py

Apache

First of create a new Apache configuration file within '/etc/httpd/conf.d/' named 'django.conf'

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

sys.path.append('/opt/django/myproject')

os.environ['DJANGO_SETTINGS_MODULE'] = 'domain1.domain1_settings' #->change to 'domain2.domain2_settings' for domain2
application = WSGIHandler()

Settings

Now you will need to create a settings file for each domain,

   /opt/django/myproject/domain1/domain1_settings.py
   /opt/django/myproject/domain2/domain2_settings.py

Below shows an example of '/opt/django/myproject/domain1/domain1_settings.py'. For the domain2 settings file amend the 'SITE_ID' to '2' and the 'ROOT_URLCONF' accordingly.

from settings import *

SITE_ID = 1   #-> change to SITE_ID = 1 for domain2

ROOT_URLCONF = 'domain1.domain1_urls'   #-> change to 'domain2.domain2_urls' for domain2

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

Urls

Next create the following files,

    /opt/django/myproject/domain1/domain1_urls.py
    /opt/django/myproject/domain2/domain2_urls.py

Below shows an example of '/opt/django/myproject/domain1/domain1_urls.py'. For the domain2 urls update the 'domain2_urls.py' file accordingly.

from django.conf.urls.defaults import patterns, include, url

urlpatterns = patterns('',
   url(r'^domain1_base/$', 'domain1.views.base'), #-> update accordingly for domain2
)

Final Step

Finally restart Apache (i.e '/etc/init.d/httpd restart'). You will now be able to browse to both domain1 and domain2 via your single project.

Note : Apache will need to be restarted once any of the above configuration files are amended.

Tags: Django, Apache

About the Author

RDonato

R Donato

Rick Donato is the Founder and Chief Editor of Fir3net.com. He currently works as a Principal Network Security Engineer and has a keen interest in automation and the cloud.

You can find Rick on Twitter @f3lix001