Windows Apache two django projects in separated venv

I need to host two separated apps, with separated venv and separated db, reason cut costs. Both of apps have been developed separately, now I have only single server to host them.

core is running on port 80 , portal on 8081

I am able to access both apps at some point of time, remotelly means outside of server. But after some time doesn't matter what port I provide I am able to access only sigle app. Seems static files are served properly as ico is changes, but content is the same so I see portal for example on both ports.

nut sure what trigers that and how to prevent it.

here are my files.

D:\Apache24\conf\httpd.conf

Define SRVROOT "d:/Apache24"
ServerRoot "${SRVROOT}"
Listen 10.192.28.86:80
Listen 10.192.28.86:8081
LoadModule actions_module modules/mod_actions.so
LoadModule alias_module modules/mod_alias.so
LoadModule allowmethods_module modules/mod_allowmethods.so
LoadModule asis_module modules/mod_asis.so
LoadModule auth_basic_module modules/mod_auth_basic.so
LoadModule authn_core_module modules/mod_authn_core.so
LoadModule authn_file_module modules/mod_authn_file.so
LoadModule authz_core_module modules/mod_authz_core.so
LoadModule authz_groupfile_module modules/mod_authz_groupfile.so
LoadModule authz_host_module modules/mod_authz_host.so
LoadModule authz_user_module modules/mod_authz_user.so
LoadModule autoindex_module modules/mod_autoindex.so
LoadModule cgi_module modules/mod_cgi.so
LoadModule dir_module modules/mod_dir.so
LoadModule env_module modules/mod_env.so
LoadModule include_module modules/mod_include.so
LoadModule isapi_module modules/mod_isapi.so
LoadModule log_config_module modules/mod_log_config.so
LoadModule mime_module modules/mod_mime.so
LoadModule negotiation_module modules/mod_negotiation.so
LoadModule setenvif_module modules/mod_setenvif.so
<IfModule unixd_module>
User daemon
Group daemon
</IfModule>
ServerAdmin admin@example.com
<Directory />
    AllowOverride none
    Require all denied
</Directory>
DocumentRoot "${SRVROOT}/htdocs"
<Directory "${SRVROOT}/htdocs">
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>
<IfModule dir_module>
    DirectoryIndex index.html
</IfModule>
<Files ".ht*">
    Require all denied
</Files>
ErrorLog "logs/error.log"
LogLevel debug
<IfModule log_config_module>
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common
    <IfModule logio_module>
      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
    </IfModule>
    CustomLog "logs/access.log" common
</IfModule>
<IfModule alias_module>
    ScriptAlias /cgi-bin/ "${SRVROOT}/cgi-bin/"
</IfModule>
<IfModule cgid_module>
</IfModule>
<Directory "${SRVROOT}/cgi-bin">
    AllowOverride None
    Options None
    Require all granted
</Directory>
<IfModule headers_module>
    RequestHeader unset Proxy early
</IfModule>
<IfModule mime_module>
    TypesConfig conf/mime.types
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
</IfModule>
<IfModule proxy_html_module>
Include conf/extra/proxy-html.conf
</IfModule>
<IfModule ssl_module>
SSLRandomSeed startup builtin
SSLRandomSeed connect builtin
</IfModule>
LoadFile "D:/appl/prod_core/prod_env/Scripts/python39.dll"
LoadModule wsgi_module "D:/appl/prod_core/prod_env/lib/site-packages/mod_wsgi/server/mod_wsgi.cp39-win_amd64.pyd"
WSGIPythonHome "D:/appl/prod_core/core"
WSGIPythonPath "D:/appl/prod_core/prod_env/Lib/site-packages"
Include conf/extra/httpd-vhosts.conf

D:\Apache24\conf\extra\httpd-vhosts.conf

    <VirtualHost *:80>
    ServerName servername:80
    WSGIPassAuthorization On
    
    ErrorLog "D:/Apache24/logs/apache.error.log"
    CustomLog "D:/Apache24/logs/apache.access.log" combined 
    
    WSGIApplicationGroup %{GLOBAL}

    WSGIScriptAlias /  "D:/appl/prod_core/core/core/wsgi.py"
    <Directory "D:/appl/prod_core/core/core">
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>

    Alias /static/ "D:/appl/prod_core/core/staticfiles/"
    <Directory "D:/appl/prod_core/core/staticfiles/">
        Require all granted
    </Directory>  
    
    Alias /media "D:/appl/prod_core/core/media/"
    <Directory "D:/appl/prod_core/core/media/">
        Require all granted
    </Directory>
    
    TraceEnable off
</VirtualHost>


<VirtualHost *:8081>
    ServerName servername:8081
    WSGIPassAuthorization On

    ErrorLog "D:/Apache24/logs/apache_project2.error.log"
    CustomLog "D:/Apache24/logs/apache_project2.access.log" combined

    # REQUIRED on Windows
    WSGIApplicationGroup %{RESOURCE}

    WSGIScriptAlias / "D:/appl/prod_portal/core/core/wsgi.py"
    <Directory "D:/appl/prod_portal/core/core">
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>

    Alias /static/ "D:/appl/prod_portal/core/staticfiles/"
    <Directory "D:/appl/prod_portal/core/staticfiles/">
        Require all granted
    </Directory>

    Alias /media/ "D:/appl/prod_portal/core/media/"
    <Directory "D:/appl/prod_portal/core/media/">
        Require all granted
    </Directory>

    TraceEnable off
</VirtualHost>

portal wsgi.py and core wsgi.py are not same file they are separated byt have same content.

import os
import sys
from django.core.wsgi import get_wsgi_application
from pathlib import Path

# Add project directory to the sys.path
path_home = str(Path(__file__).parents[1])

if path_home not in sys.path:
    sys.path.append(path_home)
# Add the app’s directory to the PYTHONPATH


os.environ['DJANGO_SETTINGS_MODULE'] = 'core.settings'
#os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings')

application = get_wsgi_application()

env folders D:/appl/prod_core/prod_env/ D:/appl/prod_portal/prod_env/

Thank you.

First, install Waitress in each virtual environment.

Core:

D:\appl\prod_core\prod_env\Scripts\python -m pip install waitress

Portal:

D:\appl\prod_portal\prod_env\Scripts\python -m pip install waitress

Now run each Django app on a local-only port (so they’re not exposed directly to the internet).

Core (localhost:8000):

cd /d D:\appl\prod_core\core
D:\appl\prod_core\prod_env\Scripts\waitress-serve --listen=127.0.0.1:8000 --threads=4 core.wsgi:application

Portal (localhost:8001):

cd /d D:\appl\prod_portal\core
D:\appl\prod_portal\prod_env\Scripts\waitress-serve --listen=127.0.0.1:8001 --threads=4 core.wsgi:application

(When you’re happy, you can run those in the background using NSSM or Task Scheduler.)

Next, update Apache so it stops trying to host Django via mod_wsgi, and instead behaves like a reverse proxy.

In D:\Apache24\conf\httpd.conf, comment out mod_wsgi (you won’t need it anymore):

# LoadFile "D:/appl/prod_core/prod_env/Scripts/python39.dll"
# LoadModule wsgi_module "D:/appl/prod_core/prod_env/lib/site-packages/mod_wsgi/server/mod_wsgi.cp39-win_amd64.pyd"
# WSGIPythonHome "D:/appl/prod_core/core"
# WSGIPythonPath "D:/appl/prod_core/prod_env/Lib/site-packages"

And enable the proxy modules:

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so

Now replace your httpd-vhosts.conf with this. The idea is simple:

  • Apache serves /static/ and /media/ directly from disk

  • everything else gets forwarded to the right Waitress process

<VirtualHost *:80>
    ServerName servername
    ProxyPreserveHost On

    Alias /static/ "D:/appl/prod_core/core/staticfiles/"
    <Directory "D:/appl/prod_core/core/staticfiles/">
        Require all granted
    </Directory>
    ProxyPass /static/ !

    Alias /media/ "D:/appl/prod_core/core/media/"
    <Directory "D:/appl/prod_core/core/media/">
        Require all granted
    </Directory>
    ProxyPass /media/ !

    ProxyPass        / http://127.0.0.1:8000/
    ProxyPassReverse / http://127.0.0.1:8000/

    ErrorLog  "D:/Apache24/logs/core.error.log"
    CustomLog "D:/Apache24/logs/core.access.log" combined
</VirtualHost>

<VirtualHost *:8081>
    ServerName servername
    ProxyPreserveHost On

    Alias /static/ "D:/appl/prod_portal/core/staticfiles/"
    <Directory "D:/appl/prod_portal/core/staticfiles/">
        Require all granted
    </Directory>
    ProxyPass /static/ !

    Alias /media/ "D:/appl/prod_portal/core/media/"
    <Directory "D:/appl/prod_portal/core/media/">
        Require all granted
    </Directory>
    ProxyPass /media/ !

    ProxyPass        / http://127.0.0.1:8001/
    ProxyPassReverse / http://127.0.0.1:8001/

    ErrorLog  "D:/Apache24/logs/portal.error.log"
    CustomLog "D:/Apache24/logs/portal.access.log" combined
</VirtualHost>

Finally, restart Apache

Вернуться на верх