简体   繁体   中英

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: 123.456.789
  • Domain 1: www.example.de for Project1
  • Domain 2: www.test.de for Proejct2

The DNS for both domains is pointing on the same IP Address. I'm using apache2 with mod_wsgi in Daemon mode. Each Project has its own directory in ~/. I've also installed a virtaulenv with python inside the Project directorys

The problem is that no matter how I configurate the apache2.conf, both domains point to Project1. What is weird is that they even do when it when I comment all the custom settings in apache2.conf. Can someone please explain this?

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

<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

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:

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

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. This will confirm whether file being read.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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