1

I have an EC2 AWS server on which I would like to host a couple of Django applications. Each of these apps has its own URL. For instance,

example1.com example2.com

By itself, example1.com works. The problem is getting example2.com to work with it at the same time.

When I visit example2.com, I get an error:

DisallowedHost at /
Invalid HTTP_HOST header: 'example2.com'. You may need to add 'example2.com' to ALLOWED_HOSTS.
Request Method: GET
Request URL:    http://example2.com
Django Version: 1.9.13
Exception Type: DisallowedHost
Exception Value:    
Invalid HTTP_HOST header: 'example2.com'. You may need to add 'example2.com' to ALLOWED_HOSTS.
Exception Location: /var/www/vhosts/example1/example1-env/lib/python3.5/site-packages/django/http/request.py in get_host, line 109
Python Executable:  /usr/bin/python3
Python Version: 3.5.1
Python Path:    
['/usr/lib64/python3.5',
 '/usr/lib64/python3.5/plat-linux',
 '/usr/lib64/python3.5/lib-dynload',
 '/usr/local/lib64/python3.5/site-packages',
 '/usr/local/lib/python3.5/site-packages',
 '/usr/lib64/python3.5/dist-packages',
 '/usr/lib/python3.5/dist-packages',
 '/var/www/vhosts/example1/',
 '/var/www/vhosts/example1/example1-env/lib/python3.5/site-packages']
Server time:    Wed, 14 Jun 2017 20:31:27 +0000

As you can see, somehow Apache is trying to use the virtual environment of example1.com when it serves example2.com. How could I correct that? Each one should be served with its own virtualenv.

Here is the Apache configuration file:

    <VirtualHost *:80>
        # This is name based virtual hosting. So place an appropriate server name
        #   here. Example: django.devsrv.local
        ServerName  example1.com

        WSGIDaemonProcess example1 python-home=/var/www/vhosts/example1/example1-env
        WSGIProcessGroup %{GLOBAL}

        # Insert the full path to the wsgi.py-file here
        WSGIScriptAlias / /var/www/vhosts/example1/example1/wsgi.py

        <Directory /var/www/vhosts/example1/>
            Require all granted
        </Directory>

        Alias /static/ /var/www/vhosts/example1/static/

        <Directory /var/www/vhosts/example1/static/>
        Order deny,allow
        Allow from all
        </Directory>

        Alias /media/ /var/www/vhosts/example1/media/
        <Directory /var/www/vhosts/example1/media/>
        Order deny,allow
        Allow from all
        </Directory>

    </VirtualHost>

    <VirtualHost *:80>
        # This is name based virtual hosting. So place an appropriate server name
        #   here. Example: django.devsrv.local
        ServerName  example2.com
        WSGIDaemonProcess example2 python-home=/var/www/vhosts/example2/example2-env
        WSGIProcessGroup %{GLOBAL}

        # Insert the full path to the wsgi.py-file here
        WSGIScriptAlias / /var/www/vhosts/example2/example2/wsgi.py

        <Directory /var/www/vhosts/example2/>
            Require all granted
        </Directory>

        Alias /static/ /var/www/vhosts/example2/static/

        <Directory /var/www/vhosts/example2/static/>
        Order deny,allow
        Allow from all
        </Directory>

        Alias /media/ /var/www/vhosts/example2/media/
        <Directory /var/www/vhosts/example2/media/>
        Order deny,allow
        Allow from all
        </Directory>
  </VirtualHost>

Edit: Having read some suggestions in the comments, I have come to this. This still does not work.

ServerName  example1.com

WSGIDaemonProcess example1 display-name=%{GROUP} python-path=/var/www/vhosts/example1/ python-home=/var/www/vhosts/example1/example1-env/
WSGIApplicationGroup %{GLOBAL}
WSGIProcessGroup example1

# Insert the full path to the wsgi.py-file here
WSGIScriptAlias / /var/www/vhosts/example1/example1/wsgi.py process-group=example1

...

ServerName  example2.com

WSGIDaemonProcess example2 display-name=%{GROUP} python-home=/var/www/vhosts/example2/example2-env/ python-path=/var/www/vhosts/example2/
WSGIApplicationGroup %{GLOBAL}
WSGIProcessGroup example2

# Insert the full path to the wsgi.py-file here
WSGIScriptAlias / /var/www/vhosts/example2/example2/wsgi.py process-group=example2
MadPhysicist
  • 5,401
  • 11
  • 42
  • 107
  • This error has nothing to do with virtual environment. You need to add example2.com to allowed hosts in your settings.py. – Jan Giacomelli Jun 14 '17 at 21:22
  • You are missing the point. That will not solve it as the error comes from example1 env Python – MadPhysicist Jun 14 '17 at 22:10
  • 1
    You are setting ``WSGIProcessGroup`` wrongly. It should be the name of the daemon process group for the virtual host, not ``%{GLOBAL}``. You probably meant ``WSGIApplicationGroup %{GLOBAL}``, which is recommended, but you still need the ``WSGIProcessGroup`` as well. Read http://blog.dscpl.com.au/2012/10/requests-running-in-wrong-django.html – Graham Dumpleton Jun 15 '17 at 06:53
  • @GrahamDumpleton If I understand correctly, can I set `WSGIApplicationGroup %{GLOBAL}` and then give `WSGIPRocessGroup` arbitrary names of my choosing? – MadPhysicist Jun 15 '17 at 14:44
  • I have made some edits to show what I have tried (amongst other things). Still not able to get it working. – MadPhysicist Jun 15 '17 at 18:58
  • Are each of those latter examples still inside of separate ``VirtualHost`` definitions? They need to be. – Graham Dumpleton Jun 16 '17 at 06:03
  • Yes. I have figured out what the problem was. I am about to post an answer describing it. Would be curious to hear your opinion on the cause. – MadPhysicist Jun 16 '17 at 16:03

1 Answers1

1

The following configuration worked for me. In short, it serves two different Django applications at example1.com and example2.com using their respective virtual environments.

As you can see, inserting the ServerAlias AND ServerName made all the difference, alongside a couple of other corrections by suggested by the community.

Apache configuration:

<IfModule !wsgi_module>
LoadModule wsgi_module modules/mod_wsgi.so
</IfModule>

<VirtualHost *:80>

    ServerName  www.example1.com
    ServerAlias example1.com

    WSGIDaemonProcess example1 display-name=%{GROUP} python-path=/var/www/vhosts/example1/ python-home=/var/www/vhosts/example1/example1-env/
    WSGIApplicationGroup %{GLOBAL}
    WSGIProcessGroup example1


    # Insert the full path to the wsgi.py-file here
    WSGIScriptAlias / /var/www/vhosts/example1/example1/wsgi.py process-group=example1

    <Directory /var/www/vhosts/example1/>
        Require all granted
    </Directory>

    Alias /static/ /var/www/vhosts/example1/static/

    <Directory /var/www/vhosts/example1/static/>
    Order deny,allow
    Allow from all
    </Directory>

    Alias /media/ /var/www/vhosts/example1/media/
    <Directory /var/www/vhosts/example1/media/>
    Order deny,allow
    Allow from all
    </Directory>
</VirtualHost>

<VirtualHost *:80>

    ServerName  www.example2.com
    ServerAlias example2.com

    WSGIDaemonProcess example2 display-name=%{GROUP} python-home=/var/www/vhosts/example2/example2-env/ python-path=/var/www/vhosts/example2/
    WSGIApplicationGroup %{GLOBAL}
    WSGIProcessGroup example2

    # Insert the full path to the wsgi.py-file here
    WSGIScriptAlias / /var/www/vhosts/example2/example2/wsgi.py process-group=example2

    <Directory /var/www/vhosts/example2/>
        Require all granted
    </Directory>

    Alias /static/ /var/www/vhosts/example2/static/

    <Directory /var/www/vhosts/example2/static/>
    Order deny,allow
    Allow from all
    </Directory>

    Alias /media/ /var/www/vhosts/example2/media/
    <Directory /var/www/vhosts/example2/media/>

</VirtualHost>
MadPhysicist
  • 5,401
  • 11
  • 42
  • 107
  • 1
    You don't need ``ServerAlias`` for virtual hosts to work. You only need the ``ServerAlias`` if you genuinely also need it to be visiting via host name without ``www``. The ``WSGIProcessGroup`` is redundant as you have ``process-group`` option to ``WSGIScriptAlias``. You could have use ``application-group`` option to ``WSGIScriptAlias`` to eliminate ``WSGIApplicationGroup`` as well, although do be aware that using both those options to ``WSGIScriptAlias`` will enable preloading of the WSGI script on process startup. – Graham Dumpleton Jun 16 '17 at 21:30