Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Sites in Django

Does anyone know how to add multiple domains to Django. I've tried following the guides here Multiple Sites under single Django project with no luck.

My configuration looks like this

Settings

/opt/django/project/settings.py

/opt/django/project/domain1_settings.py

Url

/opt/django/project/urls.py

/opt/django/project/domain1_urls.py

wsgi

/opt/django/project/domain1/domain1.wsgi

Apache

/etc/httpd/conf.d/django.conf

<VirtualHost * >
  ServerName domain1.co.uk
  ServerAlias www.domain1.co.uk direct.domain1.co.uk
  WSGIDaemonProcess domain1 processes=5 python-path=/usr/bin/python threads=1
  WSGIScriptAlias / /opt/django/project/domain1/domain1.wsgi
  ErrorLog logs/domain1-error.log
  CustomLog logs/domain1-access.log common
</VirtualHost>

When I restart apache i see no errors but when I go to the site I am sent to the (non django) domain that is configured within the main httpd.conf.

Thanks,

like image 654
felix001 Avatar asked Feb 17 '13 16:02

felix001


People also ask

Can a Django project have multiple apps?

Any Django project consists of multiple applications.

What is Django contrib sites?

contrib.sites is installed and returns either the current Site object or a RequestSite object based on the request. It looks up the current site based on request. get_host() if the SITE_ID setting is not defined. Both a domain and a port may be returned by request.


1 Answers

This answer makes the assumption that you want to have two domain names each running separate Django projects, but being hosted from the same Apache server. If this isn't the case, please refine your question!

To start with you'll need two VirtualHost entries in your apache conf (let's call your sites domain1.co.uk and domain2.co.uk)

# Virtual hosts setup
NameVirtualHost *
<VirtualHost *>
    ServerName domain1.co.uk

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

<VirtualHost *>
    ServerName domain2.co.uk

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

You'll also need 2 wsgi files (pointed two in the conf above)

/opt/django/project/domain1/domain1.wsgi
/opt/django/project/domain1/domain2.wsgi

and will look something like

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

os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings' 
# or     project.domain1_settings
application = WSGIHandler()

Onto the settings.py make sure that both settings files have difference SITE_ID = 1 or SITE_ID = 2 and that you point to the correct urls.py

ROOT_URLCONF = 'urls'

or

ROOT_URLCONF = 'domain1_urls'

Much of this question has been sourced from personal experience and this blog post. Your project directories and domain names seem to be a little confusing, I've done my best to fit them into the correct places here, but you will need to adjust for your own purposes.

Additional

If you have two sites running from the same server, you will have to be very careful to maintain consistency over project directories, static file directories and settings files etc. From your question you say your settings files are /opt/django/project/settings.py and /opt/django/project/domain1_settings.py This is quite confusing as it seems that you have one project directory (/opt/django/project). I would strongly recommend stronger separation; as I describe above, maybe setting your projects (domain1 and domain2) in directories

/opt/django/project/domain1/
/opt/django/project/domain2/

with corresponding settings files

/opt/django/project/domain1/settings.py
/opt/django/project/domain2/settings.py

etc. This should make it easier to spot what is going wrong where.

like image 86
danodonovan Avatar answered Nov 15 '22 09:11

danodonovan