如何基于子域运行两个单独的Django站点?

时间:2022-09-09 07:32:56

I have two Django sites. One is a staging site and the other is production. I want the staging site to be visible at beta.ialexander.io and the production one at ialexander.io. I'm using mod_wsgi with Apache. Here are the two .conf files:

我有两个Django站点。一个是临时站点,另一个是生产。我希望在beta.ialexander.io和ialexander.io上的生产站点上可以看到暂存站点。我正在使用mod_wsgi和Apache。这是两个.conf文件:

ialexander.conf:

# WSGI Stuff
WSGIPythonPath /var/www/Ullmannite

<VirtualHost *:80>
  # WSGI Stuff
  WSGIScriptAlias / /var/www/Ullmannite/Ullmannite/wsgi.py

  # Identification
  ServerName ialexander.io 
  ServerAdmin Alexander2475914@gmail.com

  # Public files
  Alias /assets /var/www/Ullmannite/public

  # Logs
  ErrorLog ${APACHE_LOG_DIR}/iAlexander-Error.log
  CustomLog ${APACHE_LOG_DIR}/iAlexander-Access.log combined

  # Access
  DocumentRoot "/var/www/Ullmannite/public"

  <Directory "/var/www/Ullmannite/Ullmannite">
    <Files "wsgi.py">
      Require all granted
    </Files>
  </Directory>

  <Directory "/var/www/Ullmannite/public">
    Require all granted
  </Directory>
</VirtualHost>

000-ialexander-staging.conf:

# WSGI Stuff
WSGIPythonPath /var/www/Ullmannite-Staging

<VirtualHost *:80>
  # WSGI Stuff
  WSGIScriptAlias /beta /var/www/Ullmannite-Staging/Ullmannite/wsgi.py

  # Identification
  ServerName ialexander.io
  ServerAdmin Alexander2475914@gmail.com

  # Public files
  Alias /assets /var/www/Ullmannite-Staging/public

  # Logs
  ErrorLog ${APACHE_LOG_DIR}/iAlexander-Staging-Error.log
  CustomLog ${APACHE_LOG_DIR}/iAlexander-Staging-Access.log combined

  # Access
  DocumentRoot "/var/www/Ullmannite-Staging/public"

  <Directory "/var/www/Ullmannite-Staging/Ullmannite">
    <Files "wsgi.py">
      Require all granted
    </Files>
  </Directory>

  <Directory "/var/www/Ullmannite-Staging/public">
    Require all granted
  </Directory>
</VirtualHost>

I've been struggling with this for weeks now. Originally in 000-ialexander-staging.conf I had WSGIScriptAlias / /var/www/Ullmannite-Staging/Ullmannite/wsgi.py and ServerName beta.ialexander.io, but that didn't give me what I need. I don't remember exactly what happened because this was around two weeks ago.

我已经挣扎了几个星期了。最初在000-ialexander-staging.conf中我有WSGIScriptAlias / /var/www/Ullmannite-Staging/Ullmannite/wsgi.py和ServerName beta.ialexander.io,但这并没有给我我需要的东西。我不记得到底发生了什么事,因为这是两周前的事。

Currently I have 000-ialexander-staging.conf disabled because it breaks the other one (ialexander.conf). However, when I have 000-ialexander-staging.conf enabled and I visit http://ialexander.io/beta I see the production site.

目前我禁用了000-ialexander-staging.conf,因为它打破了另一个(ialexander.conf)。但是,当我启用000-ialexander-staging.conf并访问http://ialexander.io/beta时,我会看到生产站点。

I know there's a glitch where you have to have the sites which have WSGIScriptAlias values in order. If I have the first one where WSGIScriptAlias is set to / before the second one it'll send that one, first. That's the reason I've named 000-ialexander-staging.conf with the 000- prefix. I wanted to make sure Apache is reading that one first, because it's the one that has a WSGIScriptAlias value of /beta.

我知道有一个小问题,你必须拥有按顺序具有WSGIScriptAlias值的站点。如果我有第一个将WSGIScriptAlias设置为/在第二个之前的那个,它将首先发送那个。这就是我用000-前缀命名000-ialexander-staging.conf的原因。我想确保Apache首先读取那个,因为它是具有/ beta的WSGIScriptAlias值的那个。

Why are these things happening and how can I get the production site to return at ialexander.io but the staging one at beta.ialexander.io?

为什么会发生这些事情?如何让生产网站返回ialexander.io,但是在beta.ialexander.io上进行暂存?

1 个解决方案

#1


0  

here is what I do in one httpd.conf file for Windows:

这是我在Windows的一个httpd.conf文件中所做的:

*note:

  1. NO WSGIPythonPath outside VirtualHosts - If it exists, two VirtualHosts cannot have different python-django configurations
  2. VirtualHosts之外没有WSGIPythonPath - 如果存在,两个VirtualHosts不能有不同的python-django配置

  3. there are separated two django projects
  4. 有两个django项目分开

  5. domains we want to separate are (www.)ialexander.io and beta.ialexander.io
  6. 我们想要分离的域名是(www.。)ialexander.io和beta.ialexander.io

  7. No virtualenv used. (I am not sure whether this is going to be issue...)

    没有使用virtualenv。 (我不确定这是否会成为问题...)

    # for the public site
    <VirtualHost *:80>
    DocumentRoot "/var/www/Ullmannite/public"
    ServerName ialexander.io 
    ServerAlias www.ialexander.io 
    
    WSGIScriptAlias / /var/www/Ullmannite/Ullmannite/wsgi.py
    # ...other things the same...
    
    </VirtualHost>
    
    # for the staging site
    <VirtualHost *:80>
    DocumentRoot "/var/www/Ullmannite-Staging/public"
    ServerName beta.ialexander.io 
    
    WSGIScriptAlias / /var/www/Ullmannite-Staging/Ullmannite/wsgi.py
    # ...other things the same...
    
    </VirtualHost>
    

#1


0  

here is what I do in one httpd.conf file for Windows:

这是我在Windows的一个httpd.conf文件中所做的:

*note:

  1. NO WSGIPythonPath outside VirtualHosts - If it exists, two VirtualHosts cannot have different python-django configurations
  2. VirtualHosts之外没有WSGIPythonPath - 如果存在,两个VirtualHosts不能有不同的python-django配置

  3. there are separated two django projects
  4. 有两个django项目分开

  5. domains we want to separate are (www.)ialexander.io and beta.ialexander.io
  6. 我们想要分离的域名是(www.。)ialexander.io和beta.ialexander.io

  7. No virtualenv used. (I am not sure whether this is going to be issue...)

    没有使用virtualenv。 (我不确定这是否会成为问题...)

    # for the public site
    <VirtualHost *:80>
    DocumentRoot "/var/www/Ullmannite/public"
    ServerName ialexander.io 
    ServerAlias www.ialexander.io 
    
    WSGIScriptAlias / /var/www/Ullmannite/Ullmannite/wsgi.py
    # ...other things the same...
    
    </VirtualHost>
    
    # for the staging site
    <VirtualHost *:80>
    DocumentRoot "/var/www/Ullmannite-Staging/public"
    ServerName beta.ialexander.io 
    
    WSGIScriptAlias / /var/www/Ullmannite-Staging/Ullmannite/wsgi.py
    # ...other things the same...
    
    </VirtualHost>