Forums

One Django project with 3 apps served by one PA's web app for 3 custom domains, is it possible?

Hi!

Is it possible to have one Django project with 3 apps served by one PA's web app for 3 custom domains?

I read few posts here with similar but not the same task. As opposed to those problems I do not mind just adding few more web app slots $2 each and deploy three more Django projects with custom domains.

My situation is that I have three websites targeted at slightly different audiences with 80% of shared code and content (same database of currencies, exchange rates, prices, etc, but with different sales points and marketing campaigns). Rolling out three separate Django projects is easy, but who wants to update and maintain that shared code later x 3 times per each commit...

Fortunately, since the project is one and the same, the (a) static files, and (b) environment variables, and (c) virtual environment will be the same, shared. I don't have to worry about that.

From what I got so far, it seems that I shall configure WSGI file to do:

  • if request comes from domain1, load same project with Django.settings1 (with site ID=1 and root urls serving App1 by default)
  • if request comes from domain2, load same project with Django.settings2 (with site ID=2 and root urls serving App2 by default)

Questions:

  1. Is this the right way to do it? I read about dynamicsites, but not sure if I need to refactor the project for the new extension if I can only rewrite few lines in WSGI file.

  2. If so, could you help me and tell how to write the "routing" lines in WSGI? (Unfortunately, I am not good at WSGI configuration, never did it)

It should be pretty easy to do what you want, and it sounds like you've pretty much worked it out :-) Each domain would need a separate web app on the "Web" tab, and each would have its own WSGI file. But those WSGI files could all point to exactly the same source code.

For the settings, let's say that domain1 has a WSGI file like this

import os
import sys

path = '/home/builtinclouds/something'
if path not in sys.path:
    sys.path.append(path)

os.environ['DJANGO_SETTINGS_MODULE'] = 'domain1.settings'
# Any other environment variables here

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

All you'd need to do to create domain2 would be to create a web app on the "Web" tab using "manual configuration" then put this into the new domain's WSGI file:

import os
import sys

path = '/home/builtinclouds/something'
if path not in sys.path:
    sys.path.append(path)

os.environ['DJANGO_SETTINGS_MODULE'] = 'domain2.settings'
# Any other environment variables here

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

My guess is, that's pretty much what you were thinking :-) Probably the best thing to do is just to try it out, and let us know if you hit any problems -- we'll be happy to help.

Thanks! Can't believe it was so obvious! I'l try it now.

Cool! Let us know if you have any problems.

Well, it worked quick and easy! I have successfully deployed a multi-app Django project with multiple (dummy) domains per each app.

Now more "practical" questions:

  1. Since each WSGI runs its own instance of the Django project, do I understand right, that each domain-app runs in its own thread?

  2. If I want to scale this solution, shall I add more workers to each web app, or to any one of them (since the code and everything is the same)?

  3. Database: now the apps share one DB and so will be in production, because I need some shared content. However, with this setup when I override database credentials per each app-domain.settings, do I understand right, that I can share the code and have securely different content?

  1. yes, each app will have it's own uWSGI vassals which has it's own web workers
  2. if you are seeing more traffic, then yes, adding more workers will be the solution. right now you don't have fine-grained control on how to delegate the workers yet (ie. you can just tweak the # of workers per webapp)
  3. not sure what you mean here.

Thanks!

This is exactly what I wanted to know.