How To Use Mod_wsgi For Hosting Multiple Django Projects Under Single Domain?
Solution 1:
Installing mod_wsgi
depends on what your host OS is. Check the instructions. If you're using CentOS or RedHat, I'd recommend looking at IUS Community; they provide a repository with yum installable packages for Python 3.6 and mod_wsgi
. The version of mod_wsgi
you install has to be compiled against the same version of Python you are running into your virtual environment.
Then you need to configure your VirtualHost
properly. If you have a host at the root, it has to come last in your definition. Here's an example:
<VirtualHost *:443>
TimeOut 300
SSLEngine On
ServerName mysite.example.com
# Set to the lobal Application Group
WSGIApplicationGroup %{GLOBAL}
# Pass Authorizations through to the WSGI app for Django REST Framework Token Auth
WSGIPassAuthorization On
WSGIDaemonProcess subsite-develop-https python-home=/web/subsite-develop/venv request-timeout=300 user=apache group=apache
WSGIProcessGroup subsite-develop-https
WSGIScriptAlias /subsite /web/subsite-develop/config/wsgi.py process-group=subsite-develop-https
<Directory /web/subsite-develop/config>
Require all granted
</Directory>
Alias /subsite/static/ /web/subsite-develop/static/
<Directory /web/subsite-develop/static>
Header always set Access-Control-Allow-Origin "*"Require all granted
</Directory>
WSGIDaemonProcess django-mysite-develop-https python-home=/web/django-mysite-develop/venv request-timeout=300 user=apache group=apache
WSGIProcessGroup django-mysite-develop-https
WSGIScriptAlias / /web/django-mysite-develop/config/wsgi.py process-group=django-mysite-develop-https
<Directory /web/django-mysite-develop/config>
Require all granted
</Directory>
Alias /static/ /web/django-mysite-develop/static/
<Directory /web/django-mysite-develop/static>
Header always set Access-Control-Allow-Origin "*"Require all granted
</Directory>
Alias /media/ /var/media/mysite-www/
<Directory /var/media/mysite-www>
Require all granted
</Directory>
</VirtualHost>
This example will host one site at /subsite/
and another at the root, /
. Notice that the root site comes last. It also means that you won't be able to use the route /subsite/
within the root project, since Apache will have diverted it to via the WSGIScriptAlias
definition.
This is also for a site with TLS; you may have to switch the 443
to 80
, and remove SSLEngine On
if you're not using TLS. The WSGIPassAuthorization
is for Django REST Framework tokens, you can probably remove it as well, but I've left it for a more complete example. This is for Apache 2.4+, when they switched to the Require all granted
syntax.
IUS Community, if on RedHat/CentOS: https://ius.io/
Solution 2:
I'll tell you how we did in our project. We have a single Django project with different routes. For example /players
, /tablet
. We hosted our project in two Docker containers. We have NGINX as our reverse proxy. NGINX redirects the request to the appropriate container based on the route. NGINX is exposed to the world. But, I'm not sure if it is useful for you or not.
Post a Comment for "How To Use Mod_wsgi For Hosting Multiple Django Projects Under Single Domain?"