多个 Django 项目 apache 虚拟主机

multiple Django projects apache virtual hosts

我是 apache 的新手。 所以我试图 运行 在一个 Ubuntu 服务器上使用一个 IP 地址至少两个不同的 django 项目。

两个域的 DNS 指向同一个 IP 地址。 我在守护进程模式下使用带有 mod_wsgi 的 apache2。 每个项目在 ~/. 我还在项目目录

中安装了带有 python 的 virtaulenv

问题是无论我如何配置 apache2.conf,两个域都指向 Project1。 奇怪的是,当我在 apache2.conf 中评论所有自定义设置时,他们甚至会这样做。 有人可以解释一下吗?

这是我的 apache2.conf

Listen 80
<VirtualHost *:80>
    ServerName www.example.de
    WSGIScriptAlias / /home/user/example/example-project/wsgi.py
    Alias /static/ /home/user/example/static/
    WSGIPythonPath /home/user/example
    <Directory /home/user/example/static>
        Require all granted
    </Directory>
    <Directory /home/user/example/example>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>
    WSGIDaemonProcess example.de python-path=/home/user/example:/home/user/example/exampleenv/lib/python2.7/site-packages
    WSGIProcessGroup example.de
</VirtualHost>

Listen 8080
<VirtualHost *:80>
    Servername www.test.de
    WSGIScriptAlias / /home/user/test/test-project/wsgi.py
    Alias /static/ /home/user/test/static/
    <Directory /home/user/test/static>
        Require all granted
    </Directory>
    <Directory /home/user/test/test-project>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>
    WSGIDaemonProcess test.de python-path=/home/user/test:/home/user/test/testenv/lib/python2.7/site-packages
    WSGIProcessGroup test.de
</VirtualHost>

即使我的配置有问题,为什么我在博客中评论所有内容时它仍然 运行? apache 还从哪里获取信息?

<VirtualHost *:80>

ServerName www.example1.com
ServerAlias example1.com
ServerAdmin admin@example1.com

DocumentRoot "/var/www/example1"

WSGIScriptAlias / /var/www/example1/example1/wsgi.py
WSGIDaemonProcess www.example1.com python-path=/var/www/example1:/usr/local/lib/python2.7/site-packages

<Location />
WSGIProcessGroup www.example1.com
</Location>

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

ErrorLog /var/www/logs/example1.log

</VirtualHost>


<VirtualHost *:80>

ServerName www.example2.com
ServerAlias exolcorporation.com
ServerAdmin admin@example2.com

DocumentRoot "/var/www/example2"

WSGIScriptAlias / /var/www/example2/example2/wsgi.py
WSGIDaemonProcess www.example2.com python-path=/var/www/example2:/usr/local/lib/python2.7/site-packages

<Location />
WSGIProcessGroup www.example2.com
</Location>

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

ErrorLog /var/www/logs/example2.log

</VirtualHost>

试试这个替换路径和域名 在 WSGIDaemonProcess 使用你的 virtualenv 的路径 我没有在这个例子中使用 virtualenv 这段代码在 aws ubuntu

上对我来说很好用

为什么您的请求可能最终被错误的 Django 实例处理的主题已在博客 post 中详细记录在:

该博客中描述了多种可能的情况 post 关于可能出错的地方。

在你的情况下,这听起来像是第二个虚拟主机,如果在一个单独的文件中,可能没有启用,因此网络服务器甚至没有读取该文件。结果是,如果使用基于名称的虚拟主机找不到合适的匹配项,Apache 会将所有请求发送到第一个 VirtualHost。

我建议向第二个虚拟主机添加语法错误('xxx' 单独一行)并查看 Apache 在尝试启动时是否输出错误。这将确认是否正在读取文件。