0

I've been to this site countless times but never made an account. I'm working on my first big project, so, first question:

I intend to make a few websites that will be sharing the same droplet instance from hosting provider (DO).

Wagtail offers multiple-sites on a single codebase. It lets you manage all sites from a single dashboard.

Apache lets you create multiple domains/individual websites - known as 'virtual hosts' - and directs the user to the specific directory where the requested website is located.

The websites I'm working on will share a single database to a certain degree (one of the sites will act as checkout page for some of the other sites, for example, where cart contents are retrieved via api/jason (I don't know yet))

Wagtail seems to be the best choice but I want to make sure Apache isn't required regardless of how I solve this problem (ex: if DO requires apache to be able to serve my websites as opposed to directly serving from django*, for example)

*I meant in a single Apache2 instance.

tvesday

markwalker_
  • 12,078
  • 7
  • 62
  • 99
tvesday
  • 1
  • 2

1 Answers1

0

The solution to this is essentially to get your web server, apache or nginx, to pass all requests from any subdomain to your django application.

Taking an example from this answer the apache config might look like this;

<VirtualHost *:80>
  DocumentRoot /var/www/app1
  ServerName app1.example.com
</VirtualHost>

<VirtualHost *:80>
  DocumentRoot /var/www/example
  ServerName example.com
</VirtualHost>

<VirtualHost *:80>
  DocumentRoot /var/www/wildcard
  ServerName other.example.com
  ServerAlias *.example.com
</VirtualHost>

Once you have a request sent to your django application the sites framework can translate the request into what your application is configured to do for that sub domain.

Django's Site object defines a name and a domain which the framework will get from the database using the request object, for example;

from django.contrib.sites.shortcuts import get_current_site

def my_view(request):
    current_site = get_current_site(request)
    if current_site.domain == 'foo.com':
        # Do something
        pass
    else:
        # Do something else.
        pass

You can link your database models to a Site using ForeignKey or ManyToMany relationships depending on what suits the model.

Generally with a multisite approach you have a settings file per site and these include a setting explicitly for SITE_ID to link to the site in your database.

However...

There is a django application called django-multisite which I've used recently for a similar application which creates it's own Site objects. This allows you to use a single settings file by effectively creating a dynamic SITE_ID. I much prefer this approach, although I have found a bug when migrating from an empty database because the app queries the Site table, which obviously doesn't exist. At this point I've raised the issue & fixed in a fork but the package author hasn't responded.

markwalker_
  • 12,078
  • 7
  • 62
  • 99
  • I had never deployed before so I just thought of apache2 but I'm actually using nginx. This whole answer helps a lot though, so thx. I'm reading https://www.serverlab.ca/tutorials/linux/web-servers-linux/how-to-configure-multiple-domains-with-nginx-on-ubuntu/ – tvesday Aug 02 '20 at 03:32
  • @tvesday good choice. I don't think I'll ever go back to apache after moving to nginx – markwalker_ Aug 02 '20 at 08:13
  • Do I need to add files such as /var/www/specificdomain.com or just let gunicorn handle the request directly from project file through /var/www/html since it will serve the main project (django) via symlinks (sites-available/sites-enabled)? – tvesday Aug 04 '20 at 06:48