mod_wsgi icon indicating copy to clipboard operation
mod_wsgi copied to clipboard

Apache code to allow hosting of two different websites via WSGI via Django on Windows

Open kleebauer opened this issue 2 years ago • 3 comments

Hi together,

I am currently learning how to host two different websites with one public ip with Apache, WGSI and Django on Windows. Who can help me further:

This article was already helpful: http://blog.dscpl.com.au/2012/10/requests-running-in-wrong-django.html

Below is my code, currently I only can see the website fairvalex.com and get a bad request 400 error for quantdepot.com. Each website on its own is working well.

<VirtualHost *:80>
    ServerName fairvalex.com
    ServerAlias fairvalex.com

    DocumentRoot "C:/Users/leona/OneDrive/Dokumente/PythonScripts/website/Django/fairvalex"

    WSGIApplicationGroup %{GLOBAL}

    WSGIScriptAlias / "C:/Users/leona/OneDrive/Dokumente/PythonScripts/website/Django/fairvalex/fairvalex/wsgi.py"
    <Directory "C:/Users/leona/OneDrive/Dokumente/PythonScripts/website/Django/fairvalex">
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>
    Alias /static "C:/Users/leona/OneDrive/Dokumente/PythonScripts/website/Django/fairvalex/static"
    <Directory "C:/Users/leona/OneDrive/Dokumente/PythonScripts/website/Django/fairvalex/static">
        Require all granted
    </Directory>
    ErrorLog "logs/fairvalex-error.log"
    CustomLog "logs/fairvalex-access.log" common
</VirtualHost>

<VirtualHost *:80>
    ServerName quantdepot.com
    ServerAlias quantdepot.com

    DocumentRoot "C:/Users/leona/OneDrive/Dokumente/PythonScripts/website/Django/quantdepot1"

    WSGIApplicationGroup %{GLOBAL}

    WSGIScriptAlias / "C:/Users/leona/OneDrive/Dokumente/PythonScripts/website/Django/quantdepot1/myproject/wsgi.py"
    <Directory "C:/Users/leona/OneDrive/Dokumente/PythonScripts/website/Django/quantdepot1">
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>
    Alias /static "C:/Users/leona/OneDrive/Dokumente/PythonScripts/website/Django/quantdepot1/static"
    <Directory "C:/Users/leona/OneDrive/Dokumente/PythonScripts/website/Django/quantdepot1/static">
        Require all granted
    </Directory>
    ErrorLog "logs/quantdepot1-error.log"
    CustomLog "logs/quantdepot1-access.log" common
</VirtualHost>

The other option using WSGIDaemonProcess is for me on windows not possible or I do not know how to install the module without pip.

Thank you so much in advance

Leo

kleebauer avatar Sep 21 '23 13:09 kleebauer

Don't set WSGIApplicationGroup %{GLOBAL} in both virtual hosts. The blog post explains what that directive is doing. For Windows, since there is only one process it can't be the same for both hosts.

Also ensure using os.environ['DJANGO_SETTINGS_MODULE'] to set that environment variable and not setdefault() as Django generated wsgi.py file. Since you don't post wsgi.py files I don't know if you have changed that.

Those are the two things you must do on Windows. So ensure that is correct and see how you go.

Also make sure you are checking the Apache error log for errors as you can't rely on just the error in the browser to know what is happening.

GrahamDumpleton avatar Sep 22 '23 06:09 GrahamDumpleton

thank you for the fast answer @GrahamDumpleton.

I took WSGIApplicationGroup %{GLOBAL} above the virtual hosts section and it works for each site separately.

For you I have included the wsgi.py files below, I have already replaced the setdefault().

For you, I also checked the error logs, there are none for this problem.

I did another experiment and added fairvalex.com to the allowed hosts of quantdepot.com and then I see on fairvalex.com the content of quantdepot.com. However, the actual content of fairvalex.com is still not accessible at the same time when I run the quantdepot.com website simultanously with a Windows server and a public IP.

I would like to know if there is a way to run 2 websites with one server and one public IP.

Below the wsgi.py files:

import os
import sys
import site

from django.core.wsgi import get_wsgi_application

# Add the path to your project's directory
sys.path.append('C:\\Users\\leona\\OneDrive\\Dokumente\\PythonScripts\\website\\Django\\quantdepot1')

# Set the environment variable for Django settings
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
#os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')

application = get_wsgi_application()

and:

import os
import sys
import site

from django.core.wsgi import get_wsgi_application

# Add the path to your project's directory
sys.path.append('C:\\Users\\leona\\OneDrive\\Dokumente\\PythonScripts\\website\\Django\\fairvalex')

#os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'fairvalex.settings')
# Set the environment variable for Django settings
os.environ['DJANGO_SETTINGS_MODULE'] = 'fairvalex.settings'

application = get_wsgi_application()

again changed vhost:

WSGIApplicationGroup %{GLOBAL}

<VirtualHost *:80>
    ServerName quantdepot.com
    ServerAlias quantdepot.com

    DocumentRoot "C:/Users/leona/OneDrive/Dokumente/PythonScripts/website/Django/quantdepot1"

    #WSGIApplicationGroup %{GLOBAL}

    WSGIScriptAlias / "C:/Users/leona/OneDrive/Dokumente/PythonScripts/website/Django/quantdepot1/myproject/wsgi.py"
    <Directory "C:/Users/leona/OneDrive/Dokumente/PythonScripts/website/Django/quantdepot1">
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>
    Alias /static "C:/Users/leona/OneDrive/Dokumente/PythonScripts/website/Django/quantdepot1/static"
    <Directory "C:/Users/leona/OneDrive/Dokumente/PythonScripts/website/Django/quantdepot1/static">
        Require all granted
    </Directory>
    ErrorLog "logs/quantdepot1-error.log"
    CustomLog "logs/quantdepot1-access.log" common
</VirtualHost>

<VirtualHost *:80>
    ServerName fairvalex.com
    ServerAlias fairvalex.com

    DocumentRoot "C:/Users/leona/OneDrive/Dokumente/PythonScripts/website/Django/fairvalex"

    #WSGIApplicationGroup %{GLOBAL}

    WSGIScriptAlias / "C:/Users/leona/OneDrive/Dokumente/PythonScripts/website/Django/fairvalex/fairvalex/wsgi.py"
    <Directory "C:/Users/leona/OneDrive/Dokumente/PythonScripts/website/Django/fairvalex">
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>
    Alias /static "C:/Users/leona/OneDrive/Dokumente/PythonScripts/website/Django/fairvalex/static"
    <Directory "C:/Users/leona/OneDrive/Dokumente/PythonScripts/website/Django/fairvalex/static">
        Require all granted
    </Directory>
    ErrorLog "logs/fairvalex-error.log"
    CustomLog "logs/fairvalex-access.log" common
</VirtualHost>

kleebauer avatar Sep 22 '23 08:09 kleebauer

Are you making sure you do a complete stop and start of the Apache service. Relying on mod_wsgi reloading, or even perhaps an Apache reload may not be enough on Windows.

GrahamDumpleton avatar Sep 26 '23 00:09 GrahamDumpleton