Multiple Sites in Django

后端 未结 1 1362
逝去的感伤
逝去的感伤 2021-02-01 11:07

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 l

相关标签:
1条回答
  • 2021-02-01 11:39

    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.

    0 讨论(0)
提交回复
热议问题