如何使用mod_wsgi在单个域下托管多个Django项目? [英] how to use mod_wsgi for hosting multiple django projects under single domain?

查看:47
本文介绍了如何使用mod_wsgi在单个域下托管多个Django项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个django项目,我想将它们托管在同一个域下例如: example.com/one<br> example.com/two

我搜索了各种解决方案,并找到了下面的链接,这些链接对我有很大帮助.可以托管多个Django项目在同一个域下?

从上面的阅读中,我知道我需要 mod_wsgi ,但是我很困惑在哪里安装此 mod_wsgi -我是否需要在每个目录下安装项目文件夹(每个myenv都分开),或者应该只安装一次.请帮助我如何以及在何处安装此 mod_wsgi ,最后如何在同一域名下托管多个项目.

解决方案

安装 mod_wsgi 取决于您的主机操作系统.检查说明.如果您使用的是CentOS或RedHat,建议您查看IUS社区.他们为Python 3.6和 mod_wsgi 提供了yum可安装软件包的存储库.您安装的 mod_wsgi 的版本必须针对您正在虚拟环境中运行的Python版本进行编译.

然后,您需要正确配置 VirtualHost .如果您的主机是根主机,则它必须位于定义的最后.这是一个示例:

 < VirtualHost *:443>超时300SSLEngine开启ServerName mysite.example.com#设置为全局应用程序组WSGIApplicationGroup%{GLOBAL}#将授权传递到WSGI应用,以获取Django REST框架令牌认证WSGIPassAuthorization开启WSGIDaemonProcess subsite-develop-https python-home =/web/subsite-develop/venv request-timeout = 300 user = apache group = apacheWSGIProcessGroup子站点-开发-httpsWSGIScriptAlias/subsite/web/subsite-develop/config/wsgi.py process-group = subsite-develop-https<目录/web/subsite-develop/config>要求所有授予</目录>别名/subsite/static//web/subsite-develop/static/<目录/web/subsite-develop/static>标头始终设置为Access-Control-Allow-Origin"*"要求所有授予</目录>WSGIDaemonProcess django-mysite-develop-https python-home =/web/django-mysite-develop/venv request-timeout = 300 user = apache group = apacheWSGIProcessGroup django-mysite-develop-httpsWSGIScriptAlias//web/django-mysite-develop/config/wsgi.py process-group = django-mysite-develop-https<目录/web/django-mysite-develop/config>要求所有授予</目录>别名/static//web/django-mysite-develop/static/<目录/web/django-mysite-develop/static>标头始终设置为Access-Control-Allow-Origin"*"要求所有授予</目录>别名/media//var/media/mysite-www/<目录/var/media/mysite-www>要求所有授予</目录></VirtualHost> 

此示例将在/subsite/托管一个站点,在根目录/托管另一个站点.请注意,根站点排在最后.这也意味着您将无法在根项目中使用路由/subsite/,因为Apache将通过 WSGIScriptAlias 定义将其转移到.>

这也适用于具有TLS的网站;如果您不使用TLS,则可能必须将 443 切换为 80 ,并删除 SSLEngine On . WSGIPassAuthorization 适用于Django REST Framework令牌,您也可以将其删除,但是我将其保留为更完整的示例.这是针对Apache 2.4+的,当他们切换到需要所有授予的语法.

IUS社区(如果在RedHat/CentOS上): https://ius.io/

I have multiple django projects and i want to host them under same domain eg: example.com/one<br> example.com/two

I have searched for various solutions and found the below given link which helped me alot. Is it possible to host multiple django projects under the same domain?

From the above reading , I get to know that I need mod_wsgi for this but I am confused that where to install this mod_wsgi - Do i need to install under every project folder (seperate for every myenv) or it should be installed only once . Please help me in how and where to install this mod_wsgi and finally how to host multiple projects under same domain name.

解决方案

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/

这篇关于如何使用mod_wsgi在单个域下托管多个Django项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆