【问题标题】:how to use mod_wsgi for hosting multiple django projects under single domain?如何使用 mod_wsgi 在单个域下托管多个 django 项目?
【发布时间】:2019-06-14 07:45:02
【问题描述】:

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

我搜索了各种解决方案,发现下面给出的链接对我有很大帮助。 Is it possible to host multiple django projects under the same domain?

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

【问题讨论】:

  • 首先,mod_wsgi 与在单个域中托管多个项目完全没有关系。其次,如果您确实想使用它来服务您的项目,请遵循它的安装说明;什么不清楚?
  • 我的意思是我上网了很多,之后我了解了 mod_wsgi 在单个主机上托管多个项目的方式。我也阅读了安装 mod_wsgi 的方法,现在我知道如何安装,但问题在于安装它的位置,因为我为每个项目创建了单独的虚拟环境。所以我需要在每个虚拟环境中安装它吗?
  • 就像我说的,您阅读了说明。 mod_wsgi 是为 Apache 安装的,它与虚拟环境无关。

标签: python django apache mod-wsgi


【解决方案1】:

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

然后您需要正确配置您的VirtualHost。如果您在根目录中有一个主机,则它必须在您的定义中排在最后。这是一个例子:

<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>

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

这也适用于具有 TLS 的站点;如果您不使用 TLS,您可能需要将 443 切换为 80,并删除 SSLEngine OnWSGIPassAuthorization 用于 Django REST Framework 令牌,您也可以将其删除,但我将其留作更完整的示例。这适用于 Apache 2.4+,当他们切换到 Require all granted 语法时。

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

【讨论】:

  • 你的解释是我发现的最好的。但我有一个问题:我们是否必须将它放在一个文件中.conf?我把它分成两个单独的文件,但它不起作用。
  • @TacianoMoraisSilva 我无法确认 100%,但我也只能在单个文件中使用它。对于单个 ServerName 的上下文,可能需要将其保存在单个文件中。
【解决方案2】:

我会告诉你我们在项目中的表现。我们有一个具有不同路线的 Django 项目。例如/players/tablet。我们将项目托管在两个 Docker 容器中。我们有 NGINX 作为我们的反向代理。 NGINX 根据路由将请求重定向到适当的容器。 NGINX 暴露在世人面前。但是,我不确定它是否对您有用。

【讨论】:

  • 您好,感谢您的回复,但我完全是初学者,真的不知道如何使用这个 docker 容器和 nginx
  • 换句话说,我们正在运行两台服务器,每个路由一台。我们运行另一台服务器(NGINX),它将根据路由决定上述两台服务器。您实际上想在哪里托管?您遵循多少层?
  • 谢谢,我现在明白这个概念了。会尝试这种方式。
猜你喜欢
  • 1970-01-01
  • 2012-05-20
  • 2013-12-05
  • 2020-09-15
  • 2014-07-14
  • 2017-01-30
  • 2016-01-14
  • 1970-01-01
  • 2016-04-14
相关资源
最近更新 更多