Multiple Django Projects on Apache Server with mod_wsgi 正在尝试加载错误的项目库?

huangapple go评论95阅读模式
英文:

Multiple Django Projects on Apache Server with mod_wsgi is trying to load wrong project libraries?

问题

I'm trying to run multiple Django projects on one server. The server is Windows and I am using mod_wsgi with Apache.

Normally, each project lives on its own server and everything works great - but for dev/staging purposes I am trying to just serve them all from the same server (I've done this successfully with other groups of projects on another server, but they were Flask applications)

My structure looks similar to this:

  1. ProjectA (this project contains the authentication / login / logout)
  2. - appa1 (shared templates / static files)
  3. - appa2
  4. ProjectB
  5. - appa1 (shared templates / static files)
  6. - appb1
  7. - appb2
  8. ProjectC
  9. - appa1 (shared templates / static files)
  10. - appc1
  11. - appc2

My VirtualHost setup is pretty straightforward:

  1. <VirtualHost dev.projecta.mydomain.com:80>
  2. ServerName dev.projecta.mydomain.com
  3. Redirect / https://dev.projecta.mydomain.com
  4. </VirtualHost>
  5. <VirtualHost dev.projecta.mydomain.com:443>
  6. ServerName dev.projecta.mydomain.com
  7. WSGIScriptAlias / "C:/projecta/projecta.wsgi"
  8. CustomLog "logs/projecta_access.log" common
  9. ErrorLog "logs/projecta_error.log"
  10. SSLEngine on
  11. SSLCertificateFile "C:/path/to/projecta_cert/cert.cer"
  12. SSLCertificateKeyFile "C:/path/to/projecta_cert/project.key"
  13. <Directory C:\projecta>
  14. Require all granted
  15. </Directory>
  16. </VirtualHost>
  17. <VirtualHost dev.projectb.mydomain.com:80>
  18. ServerName dev.projectb.mydomain.com
  19. Redirect / https://dev.projectb.mydomain.com
  20. </VirtualHost>
  21. <VirtualHost dev.projectb.mydomain.com:443>
  22. ServerName dev.projectb.mydomain.com
  23. WSGIScriptAlias / "C:/projectb/projectb.wsgi"
  24. CustomLog "logs/projectb_access.log" common
  25. ErrorLog "logs/projectb_error.log"
  26. SSLEngine on
  27. SSLCertificateFile "C:/path/to/projectb_cert/cert.cer"
  28. SSLCertificateKeyFile "C:/path/to/projectb_cert/project.key"
  29. <Directory C:\projectb>
  30. Require all granted
  31. </Directory>
  32. </VirtualHost>
  33. <VirtualHost dev.projectc.mydomain.com:80>
  34. ServerName dev.projectc.mydomain.com
  35. Redirect / https://dev.projectc.mydomain.com
  36. </VirtualHost>
  37. <VirtualHost dev.projectc.mydomain.com:443>
  38. ServerName dev.projectc.mydomain.com
  39. WSGIScriptAlias / "C:/projectc/projectc.wsgi"
  40. CustomLog "logs/projectc_access.log" common
  41. ErrorLog "logs/projectc_error.log"
  42. SSLEngine on
  43. SSLCertificateFile "C:/path/to/projectc_cert/cert.cer"
  44. SSLCertificateKeyFile "C:/path/to/projectc_cert/project.key"
  45. <Directory C:\projectc>
  46. Require all granted
  47. </Directory>
  48. </VirtualHost>

When I access ProjectA in the browser (https://dev.projecta.mydomain.com) everything fires up and works as I would expect. I login and everything works.

When I visit a link to ProjectB I get a 500 error - the error is recorded in projectb_error.log and states this:

  1. [Wed May 10 09:13:49.594211 2023] [wsgi:error] [pid 17300:tid 1400] [client 9.26.10.10:15429] ModuleNotFoundError: No module named 'appa2', referer: https://dev.projectb.mydomain.com/

There is no appa2 in ProjectB; so I'm not sure why it's still trying to access it.

If I restart Apache and visit ProjectB first? Everything loads fine for ProjectB (this is important to keep in mind).

When I visit a link to any other project (for example: ProjectA) I get a 500 error - the error is recorded in projecta_error.log and states this:

  1. [Wed May 10 09:15:49.594211 2023] [wsgi:error] [pid 17300:tid 1400] [client 9.26.10.10:15429] ModuleNotFoundError: No module named 'appb1', referer: https://dev.projecta.mydomain.com/

In essence, when I restart Apache - the first site I visit loads fine, but the subsequent sites I visit show a similar error in the logs (stating that it can't find a module that is specific to the previous site).

It works when these projects are on different servers - but not on the same server. That's the only difference.

The .wsgi for each project isn't very complex either:

  1. import os
  2. import platform
  3. import sys
  4. from pathlib import Path
  5. # Activate the project specific virtual environment
  6. path = Path(__file__).resolve().parent.parent
  7. # Set the path to the project root in the system PATH
  8. sys.path.insert(0, str(path))
  9. activate_dir = "Scripts" if platform.system() == "Windows" else "bin"
  10. activate_script = "activate_this.py"
  11. activate_path = path / "venv" / activate_dir / activate_script
  12. if activate_path.is_file():
  13. exec(
  14. compile(open(activate_path).read(), activate_path, "exec"),
  15. dict(__file__=activate_path),
  16. )
  17. # Import django-environ which is installed in the venv
  18. import environ
  19. ROOT_DIR = Path(__file__).parent.parent
  20. # `projecta` is changed to `projectb` and `projectc` in each project appropriately
  21. env = environ.Env(
  22. DJANGO_SETTINGS_MODULE=(str, "projecta.settings.base"),
  23. )
  24. env.read_env(ROOT_DIR / ".env")
  25. # Load up the django application
  26. settings_module = env.str("DJANGO_SETTINGS_MODULE")
  27. if not settings_module:
  28. # `projecta` is changed to `projectb` and `projectc` in each project appropriately
  29. os.environ["DJANGO_SETTINGS_MODULE"] = "projecta.settings.base"
  30. from django.core.wsgi import get_wsgi_application
  31. application = get_wsgi_application()

I've double-checked my .env files to ensure they are correct.

I'm not sure how best to continue troubleshooting this - I am not sure if this error is related to Apache and how it's set up, or more related to Django and perhaps sessions or cookies causing an issue? Maybe it's mod_wsgi?

I've not run into something as odd as this before.

TL;DR: Apache loads first site fine - 500's on all subsequent projects because it's trying to reference a module specific to the first site visited. If Apache is restarted the second site will load fine, but any other site will 500 with references to a module specific to the second site.

英文:

I'm trying to run multiple Django projects on one server. The server is Windows and I am using mod_wsgi with Apache.

Normally, each project lives on it's own server and everything works great - but for dev/staging purposes I am trying to just serve them all from the same server (I've done this successfully with other groups of projects on another server, but they were Flask applications)

My structure looks similar to this:

  1. ProjectA (this project contains the authentication / login / logout)
  2. - appa1 (shared templates / static files)
  3. - appa2
  4. ProjectB
  5. - appa1 (shared templates / static files)
  6. - appb1
  7. - appb2
  8. ProjectC
  9. - appa1 (shared templates / static files)
  10. - appc1
  11. - appc2

My VirtualHost setup is pretty straightforward:

  1. &lt;VirtualHost dev.projecta.mydomain.com:80&gt;
  2. ServerName dev.projecta.mydomain.com
  3. Redirect / https://dev.projecta.mydomain.com
  4. &lt;/VirtualHost&gt;
  5. &lt;VirtualHost dev.projecta.mydomain.com:443&gt;
  6. ServerName dev.projecta.mydomain.com
  7. WSGIScriptAlias / &quot;C:/projecta/projecta.wsgi&quot;
  8. CustomLog &quot;logs/projecta_access.log&quot; common
  9. ErrorLog &quot;logs/projecta_error.log&quot;
  10. SSLEngine on
  11. SSLCertificateFile &quot;C:/path/to/projecta_cert/cert.cer&quot;
  12. SSLCertificateKeyFile &quot;C:/path/to/projecta_cert/project.key&quot;
  13. &lt;Directory C:\projecta&gt;
  14. Require all granted
  15. &lt;/Directory&gt;
  16. &lt;/VirtualHost&gt;
  17. &lt;VirtualHost dev.projectb.mydomain.com:80&gt;
  18. ServerName dev.projectb.mydomain.com
  19. Redirect / https://dev.projectb.mydomain.com
  20. &lt;/VirtualHost&gt;
  21. &lt;VirtualHost dev.projectb.mydomain.com:443&gt;
  22. ServerName dev.projectb.mydomain.com
  23. WSGIScriptAlias / &quot;C:/projectb/projectb.wsgi&quot;
  24. CustomLog &quot;logs/projectb_access.log&quot; common
  25. ErrorLog &quot;logs/projectb_error.log&quot;
  26. SSLEngine on
  27. SSLCertificateFile &quot;C:/path/to/projectb_cert/cert.cer&quot;
  28. SSLCertificateKeyFile &quot;C:/path/to/projectb_cert/project.key&quot;
  29. &lt;Directory C:\projectb&gt;
  30. Require all granted
  31. &lt;/Directory&gt;
  32. &lt;/VirtualHost&gt;
  33. &lt;VirtualHost dev.projectc.mydomain.com:80&gt;
  34. ServerName dev.projectc.mydomain.com
  35. Redirect / https://dev.projectc.mydomain.com
  36. &lt;/VirtualHost&gt;
  37. &lt;VirtualHost dev.projectc.mydomain.com:443&gt;
  38. ServerName dev.projectc.mydomain.com
  39. WSGIScriptAlias / &quot;C:/projectc/projectc.wsgi&quot;
  40. CustomLog &quot;logs/projectc_access.log&quot; common
  41. ErrorLog &quot;logs/projectc_error.log&quot;
  42. SSLEngine on
  43. SSLCertificateFile &quot;C:/path/to/projectc_cert/cert.cer&quot;
  44. SSLCertificateKeyFile &quot;C:/path/to/projectc_cert/project.key&quot;
  45. &lt;Directory C:\projectc&gt;
  46. Require all granted
  47. &lt;/Directory&gt;
  48. &lt;/VirtualHost&gt;

When I access ProjectA in the browser (https://dev.projecta.mydomain.com) everything fires up and works as I would expect. I login and everything works.

When I visit a link to ProjectB I get a 500 error - the error is recorded in projectb_error.log and states this:

  1. [Wed May 10 09:13:49.594211 2023] [wsgi:error] [pid 17300:tid 1400] [client 9.26.10.10:15429] ModuleNotFoundError: No module named &#39;appa2&#39;\r, referer: https://dev.projectb.mydomain.com/

There is no appa2 in ProjectB; so I'm not sure why it's still trying to access it.

If I restart Apache and visit ProjectB first? Everything loads fine for ProjectB (this is important to keep in mind).

When I visit a link to any other project (for example: ProjectA) I get a 500 error - the error is recorded in projecta_error.log and states this:

  1. [Wed May 10 09:15:49.594211 2023] [wsgi:error] [pid 17300:tid 1400] [client 9.26.10.10:15429] ModuleNotFoundError: No module named &#39;appb1&#39;\r, referer: https://dev.projecta.mydomain.com/

In essence, when I restart Apache - the first site I visit loads fine, but the subsequent sites I visit show a similar error in the logs (stating that it can't find a module that is specific to the previous site).

It works when these projects are on different servers - but not on the same server. That's the only difference.

The .wsgi for each project isn't very complex either:

  1. import os
  2. import platform
  3. import sys
  4. from pathlib import Path
  5. # Activate the project specific virtual environment
  6. path = Path(__file__).resolve().parent.parent
  7. # Set the path to the project root in the system PATH
  8. sys.path.insert(0, str(path))
  9. activate_dir = &quot;Scripts&quot; if platform.system() == &quot;Windows&quot; else &quot;bin&quot;
  10. activate_script = &quot;activate_this.py&quot;
  11. activate_path = path / &quot;venv&quot; / activate_dir / activate_script
  12. if activate_path.is_file():
  13. exec(
  14. compile(open(activate_path).read(), activate_path, &quot;exec&quot;),
  15. dict(__file__=activate_path),
  16. )
  17. # Import django-environ which is installed in the venv
  18. import environ
  19. ROOT_DIR = Path(__file__).parent.parent
  20. # `projecta` is changed to `projectb` and `projectc` in each project appropriately
  21. env = environ.Env(
  22. DJANGO_SETTINGS_MODULE=(str, &quot;projecta.settings.base&quot;),
  23. )
  24. env.read_env(ROOT_DIR / &quot;.env&quot;)
  25. # Load up the django application
  26. settings_module = env.str(&quot;DJANGO_SETTINGS_MODULE&quot;)
  27. if not settings_module:
  28. # `projecta` is changed to `projectb` and `projectc` in each project appropriately
  29. os.environ[&quot;DJANGO_SETTINGS_MODULE&quot;] = &quot;projecta.settings.base&quot;
  30. from django.core.wsgi import get_wsgi_application
  31. application = get_wsgi_application()

I've double checked my .env files to ensure they are correct.

I'm not sure how best to continue troubleshooting this - I am not sure if this error is related to Apache and how it's setup, or more related to Django and perhaps sessions or cookies causing an issue? Maybe it's mod_wsgi?

I've not ran into something as odd as this before.

TL;DR: Apache loads first site fine - 500's on all subsequent projects because it's trying to reference a module specific to the first site visited. If Apache is restarted the second site will load fine, but any other site will 500 with references to a module specific to the second site.

答案1

得分: 1

尝试为每个应用添加不同的应用程序组:

  1. WSGIScriptAlias / "d:/.... /wsgi.py" application-group=app_name1

关于mod_wsgi和虚拟主机的更多提示,请参考此处的回答:https://stackoverflow.com/questions/75141768/how-to-deploy-multiple-django-apps-on-apache-in-windows/75145306#75145306

更新:这很可能与错误的settings.py文件有关。

尝试以下操作:
更改:

  1. # 加载Django应用程序
  2. settings_module = env.str("DJANGO_SETTINGS_MODULE")
  3. if not settings_module:
  4. # 在每个项目中将`projecta`更改为相应的`projectb`和`projectc`
  5. os.environ["DJANGO_SETTINGS_MODULE"] = "projecta.settings.base"

为:

  1. os.environ["DJANGO_SETTINGS_MODULE"] = "projecta.settings.base"
英文:

try to add different application groups for each app

  1. WSGIScriptAlias / &quot;d:/.... /wsgi.py&quot; application-group=app_name1

and some more hints about mod_wsgi and virtual hosts in the answer here: https://stackoverflow.com/questions/75141768/how-to-deploy-multiple-django-apps-on-apache-in-windows/75145306#75145306

update: most certainly this has to do with wrong settings.py file.

try the following:<br>
change:

  1. # Load up the django application
  2. settings_module = env.str(&quot;DJANGO_SETTINGS_MODULE&quot;)
  3. if not settings_module:
  4. # `projecta` is changed to `projectb` and `projectc` in each project appropriately
  5. os.environ[&quot;DJANGO_SETTINGS_MODULE&quot;] = &quot;projecta.settings.base&quot;

to simply

  1. os.environ[&quot;DJANGO_SETTINGS_MODULE&quot;] = &quot;projecta.settings.base&quot;

huangapple
  • 本文由 发表于 2023年5月11日 01:02:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76220959.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定