简体   繁体   English

多个Django项目apache虚拟主机

[英]multiple Django projects apache virtual hosts

I'm a newbie to apache. 我是新手。 So I'm trying to run at least two different django projects on one Ubuntu Server with a single IP address. 因此,我试图在一台具有单个IP地址的Ubuntu服务器上运行至少两个不同的django项目。

  • IP: 123.456.789 IP:123.456.789
  • Domain 1: www.example.de for Project1 域1:Project1的www.example.de
  • Domain 2: www.test.de for Proejct2 域2:Proejct2的www.test.de

The DNS for both domains is pointing on the same IP Address. 两个域的DNS指向相同的IP地址。 I'm using apache2 with mod_wsgi in Daemon mode. 我在守护程序模式下将apache2与mod_wsgi一起使用。 Each Project has its own directory in ~/. 每个项目在〜/中都有自己的目录。 I've also installed a virtaulenv with python inside the Project directorys 我还在Project目录中安装了带Python的virtaulenv

The problem is that no matter how I configurate the apache2.conf, both domains point to Project1. 问题是,无论我如何配置apache2.conf,两个域都指向Project1。 What is weird is that they even do when it when I comment all the custom settings in apache2.conf. 奇怪的是,当我评论apache2.conf中的所有自定义设置时,他们甚至会这样做。 Can someone please explain this? 有人可以解释一下吗?

here is my 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>

Even if there is something wrong with my config, why does it still run when i comment everything in this blog? 即使我的配置有问题,当我在此博客中评论所有内容时,为什么它仍然可以运行? Where else is apache getting the information from? 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>

try this replacing paths and domain names in WSGIDaemonProcess use path of your virtualenv i didn't used virtualenv in this example this code works fine for me on aws ubuntu 试试这个替换WSGIDaemonProcess中的路径和域名的virtualenv的使用路径我在这个例子中没有使用virtualenv,此代码对我在aws ubuntu上正常工作

The topic of why your requests may end up being handled by the wrong Django instance has been documented in detail in the blog post at: 博客文章中详细记录了为什么您的请求可能最终被错误的Django实例处理的主题:

There are multiple possible situations described in that blog post as to what can go wrong. 该博客文章中描述了关于可能出问题的多种情况。

In your case it sounds like the virtual host for the second, if in a separate file, may not have been enabled and so the web server is not even reading the file. 在您的情况下,听起来好像是第二个虚拟主机(如果在单独的文件中)可能尚未启用,因此Web服务器甚至没有读取该文件。 The consequence is that Apache will send all requests to the first VirtualHost if it can't find an appropriate match using name based virtual hosting. 结果是,如果Apache使用基于名称的虚拟主机找不到合适的匹配项,它将把所有请求发送到第一个VirtualHost。

I would suggest adding a syntax error to the second virtual host ('xxx' on a line by itself) and see if Apache outputs an error when trying to start. 我建议将语法错误添加到第二个虚拟主机(单独一行上的“ xxx”),并查看Apache在尝试启动时是否输出错误。 This will confirm whether file being read. 这将确认是否正在读取文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM