Search code examples
djangomod-wsgiwsgidjango-wsgi

Configuring VirtualHost to run a second website, issue with <VirtualHost *:8080>, error with `Listen`


I am trying to figure out how to host a second Django website from my VM and I am wondering if somebody could see where I have made any obvious mistakes.

Currently whichever site is set to <VirtualHost *:80> works. I learned from this answer that I should specify the second website to <VirtualHost *:8080>. However when I try to use Listen I get the below error when I try to reload apache

Job for apache2.service failed. See 'systemctl status apache2.service' and 'journalctl -xn' for details.

  • Does anyone understand what might be going wrong?

  • Why does <VirtualHost *:80> but not <VirtualHost *:8080>?

  • And why do I get the error when I specify Listen?

I am using Debian 8.5, Apache 2.4.10 and mod-wsgi 4.3.0-1.

Listen 80
<VirtualHost *:80>

ServerName myserver.scss.tcd.ie/bias_experiment/

Alias /bias_experiment/static/ /var/www/bias_experiment/static/
<Directory /var/www/bias_experiment/static>
Order deny,allow
Allow from all
</Directory>

WSGIScriptAlias /bias_experiment /var/www/bias_experiment/src/bias_experiment/index.wsgi
<Directory /var/www/bias_experiment/src/bias_experiment>
<Files index.wsgi>
Order deny,allow
Allow from all
</Files>
</Directory>

</VirtualHost>


Listen 8080
<VirtualHost *:8080>

ServerName myserver.scss.tcd.ie/bias_experiment_two/

Alias /bias_experiment_two/static/ /var/www/bias_experiment_two/static/
<Directory /var/www/bias_experiment_two/static>
Order deny,allow
Allow from all
</Directory>

WSGIScriptAlias /bias_experiment_two /var/www/bias_experiment_two/src/bias_experiment/index.wsgi
<Directory /var/www/bias_experiment_two/src/bias_experiment>
<Files index.wsgi>
Order deny,allow
Allow from all
</Files>
</Directory>

</VirtualHost>

Any help is as always, much appreciated.


Solution

  • You can't set ServerName as you are. The ServerName directive must be a host name only else named based virtual hosts will not work when you have multiple VirtualHost definitions. The only reason anything would be handled at all as is is because when name based virtual hosts are not set up correctly, or no host name matches, Apache will send requests to the first VirtualHost found when the configuration was read. What you should be doing is have everything in one VirtualHost if you want them to be access via the same host name. Using different ports could be used, but is less convenient.

    <VirtualHost *:80>
    
    ServerName myserver.scss.tcd.ie
    
    WSGIDaemonProcess bias_experiment
    
    Alias /bias_experiment/static/ /var/www/bias_experiment/static/
    <Directory /var/www/bias_experiment/static>
    Order deny,allow
    Allow from all
    </Directory>
    
    WSGIScriptAlias /bias_experiment /var/www/bias_experiment/src/bias_experiment/index.wsgi \
        process-group=bias_experiment application-group=%{GLOBAL}
    
    <Directory /var/www/bias_experiment/src/bias_experiment>
    <Files index.wsgi>
    Order deny,allow
    Allow from all
    </Files>
    </Directory>
    
    WSGIDaemonProcess bias_experiment_two
    
    Alias /bias_experiment_two/static/ /var/www/bias_experiment_two/static/
    <Directory /var/www/bias_experiment_two/static>
    Order deny,allow
    Allow from all
    </Directory>
    
    WSGIScriptAlias /bias_experiment_two /var/www/bias_experiment_two/src/bias_experiment/index.wsgi \
        process-group=bias_experiment_two application-group=%{GLOBAL}
    
    <Directory /var/www/bias_experiment_two/src/bias_experiment>
    <Files index.wsgi>
    Order deny,allow
    Allow from all
    </Files>
    </Directory>
    
    </VirtualHost>
    

    To keep the WSGI applications separate, two separate daemon process groups are declared and each WSGI application delegated to a different process group.

    The two WSGI applications would then be accessed as:

    If these are Django sites, you likely will have additional setup changes you will need to make in the Django settings file, to allow both to run under the same host name and not interfere with each other.