尝试在同一台 apache2 服务器上托管 2 个 django 网站时遇到问题
Trouble trying to host 2 django webs on the same apache2 server
我一直在为此烦恼,我不知道我还应该做什么。
对于上下文,我试图在同一台 Apache2 服务器上托管 2 个 django 网站。我能够轻松设置 1 个网站,但每当我尝试添加第二个网站时,一切都会中断。
目前,两个网站都位于 /home/my_usr 目录中。
所以我们会有 /home/my_usr/web1 和 /home/my_usr/web2.
我的 web1-web2.conf 文件看起来像:
WSGIDaemonProcess web1 python-home=/home/my_usr/web1/env python-path=/home/my_usr/web1
WSGIProcessGroup web1
WSGIDaemonProcess web2 python-home=/home/my_usr/web2/env python-path=/home/my_usr/web2
WSGIProcessGroup web2
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that the server uses to
# identify itself. This is used when creating redirection URLs. In the context of virtual hosts, the
# ServerName specifies what hostname must appear in the request's Host: header to match this virtual
# host. For the default virtual host (this file) this value is not decisive as it is used as a last
# resort host regardless. However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin webmaster@localhost
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg. It is also possible to configure the loglevel for particular modules,
# e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are enabled or disabled at a global level,
# it is possible to include a line for only one particular virtual host. For example the following
# line enables the CGI configuration for this host only after it has been globally disabled with
# "a2disconf".
#Include conf-available/serve-cgi-bin.conf
Alias /web1/static /home/my_usr/web1/static
<Directory /home/my_usr/web1/static>
Require all granted
</Directory>
Alias /media /home/my_usr/web1/media
<Directory /home/my_usr/web1/media>
Require all granted
</Directory>
<Directory /home/my_usr/web1/web1>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIScriptAlias /web1 /home/my_usr/web1/web1/wsgi.py
Alias /web2/static/ /home/my_usr/web2/static
<Directory /home/my_usr/web2/static>
Require all granted
</Directory>
Alias /media /home/my_usr/web2/media
<Directory /home/my_usr/web2/media>
Require all granted
</Directory>
<Directory /home/my_usr/web2//web>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIScriptAlias /web2 /home/my_usr/web2/web2/wsgi.py
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
有了这个 conf 文件,在我执行 sudo service apache2 restart
之后,如果我继续 host/web1 web1 工作得很好,但是如果我想访问至 host/web2 我收到一条错误消息,此错误在 error.log 文件中:
[Fri Jul 02 11:45:45.210666 2021] [mpm_prefork:notice] [pid 29254] AH00163: Apache/2.4.46 (Debian) mod_wsgi/4.6.5 Python/3.7 configured -- resuming normal operations
[Fri Jul 02 11:45:45.210780 2021] [core:notice] [pid 29254] AH00094: Command line: '/usr/sbin/apache2'
[Fri Jul 02 11:46:13.416556 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] mod_wsgi (pid=29256): Failed to exec Python script file '/home/my_usr/web2/web2/wsgi.py'.
[Fri Jul 02 11:46:13.416650 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] mod_wsgi (pid=29256): Exception occurred processing WSGI script '/home/my_usr/web2/web2/wsgi.py'.
[Fri Jul 02 11:46:13.417397 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] Traceback (most recent call last):
[Fri Jul 02 11:46:13.417464 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] File "/home/my_usr/web2/web2/wsgi.py", line 24, in <module>
[Fri Jul 02 11:46:13.417517 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] application = get_wsgi_application()
[Fri Jul 02 11:46:13.417526 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] File "/home/my_usr/web2/env/lib/python3.7/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
[Fri Jul 02 11:46:13.417530 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] django.setup(set_prefix=False)
[Fri Jul 02 11:46:13.417546 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] File "/home/my_usr/web2/env/lib/python3.7/site-packages/django/__init__.py", line 19, in setup
[Fri Jul 02 11:46:13.417550 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
[Fri Jul 02 11:46:13.417555 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] File "/home/my_usr/web2/env/lib/python3.7/site-packages/django/conf/__init__.py", line 82, in __getattr__
[Fri Jul 02 11:46:13.417559 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] self._setup(name)
[Fri Jul 02 11:46:13.417564 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] File "/home/my_usr/web2/env/lib/python3.7/site-packages/django/conf/__init__.py", line 69, in _setup
[Fri Jul 02 11:46:13.417567 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] self._wrapped = Settings(settings_module)
[Fri Jul 02 11:46:13.417572 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] File "/home/my_usr/web2/env/lib/python3.7/site-packages/django/conf/__init__.py", line 170, in __init__
[Fri Jul 02 11:46:13.417575 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] mod = importlib.import_module(self.SETTINGS_MODULE)
[Fri Jul 02 11:46:13.417580 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] File "/usr/lib/python3.7/importlib/__init__.py", line 127, in import_module
[Fri Jul 02 11:46:13.417583 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] return _bootstrap._gcd_import(name[level:], package, level)
[Fri Jul 02 11:46:13.417588 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
[Fri Jul 02 11:46:13.417593 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] File "<frozen importlib._bootstrap>", line 983, in _find_and_load
[Fri Jul 02 11:46:13.417598 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
[Fri Jul 02 11:46:13.417603 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
[Fri Jul 02 11:46:13.417608 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
[Fri Jul 02 11:46:13.417612 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] File "<frozen importlib._bootstrap>", line 983, in _find_and_load
[Fri Jul 02 11:46:13.417617 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
[Fri Jul 02 11:46:13.417634 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] ModuleNotFoundError: No module named 'web1'
然而,如果我执行 sudo service apache2 restart
并直接进入 host/web2 现在 web2 工作得很好但是如果在那之后我尝试进入 host/web1 它显示 web2.
所以基本上如果我重新启动 apache2 服务然后转到 host/web1 然后 host/web2 我得到一条消息错误和已发布的日志。
如果我重新启动 apache2 服务然后转到 host/web2 然后 host/web1 我得到 web2 内容即使 url 是 host/web2
有什么线索吗?
提前致谢
好的,我明白了!希望这对将来的人有帮助。
所以我的 .conf 文件从一开始就很好,但给我带来麻烦的是我的 django 项目的 .settings 文件。
感谢 this 文章,我修复了它。
我有这样的设置文件:
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
并将其更改为:
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
我一直在为此烦恼,我不知道我还应该做什么。 对于上下文,我试图在同一台 Apache2 服务器上托管 2 个 django 网站。我能够轻松设置 1 个网站,但每当我尝试添加第二个网站时,一切都会中断。 目前,两个网站都位于 /home/my_usr 目录中。 所以我们会有 /home/my_usr/web1 和 /home/my_usr/web2.
我的 web1-web2.conf 文件看起来像:
WSGIDaemonProcess web1 python-home=/home/my_usr/web1/env python-path=/home/my_usr/web1
WSGIProcessGroup web1
WSGIDaemonProcess web2 python-home=/home/my_usr/web2/env python-path=/home/my_usr/web2
WSGIProcessGroup web2
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that the server uses to
# identify itself. This is used when creating redirection URLs. In the context of virtual hosts, the
# ServerName specifies what hostname must appear in the request's Host: header to match this virtual
# host. For the default virtual host (this file) this value is not decisive as it is used as a last
# resort host regardless. However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin webmaster@localhost
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg. It is also possible to configure the loglevel for particular modules,
# e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are enabled or disabled at a global level,
# it is possible to include a line for only one particular virtual host. For example the following
# line enables the CGI configuration for this host only after it has been globally disabled with
# "a2disconf".
#Include conf-available/serve-cgi-bin.conf
Alias /web1/static /home/my_usr/web1/static
<Directory /home/my_usr/web1/static>
Require all granted
</Directory>
Alias /media /home/my_usr/web1/media
<Directory /home/my_usr/web1/media>
Require all granted
</Directory>
<Directory /home/my_usr/web1/web1>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIScriptAlias /web1 /home/my_usr/web1/web1/wsgi.py
Alias /web2/static/ /home/my_usr/web2/static
<Directory /home/my_usr/web2/static>
Require all granted
</Directory>
Alias /media /home/my_usr/web2/media
<Directory /home/my_usr/web2/media>
Require all granted
</Directory>
<Directory /home/my_usr/web2//web>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIScriptAlias /web2 /home/my_usr/web2/web2/wsgi.py
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
有了这个 conf 文件,在我执行 sudo service apache2 restart
之后,如果我继续 host/web1 web1 工作得很好,但是如果我想访问至 host/web2 我收到一条错误消息,此错误在 error.log 文件中:
[Fri Jul 02 11:45:45.210666 2021] [mpm_prefork:notice] [pid 29254] AH00163: Apache/2.4.46 (Debian) mod_wsgi/4.6.5 Python/3.7 configured -- resuming normal operations
[Fri Jul 02 11:45:45.210780 2021] [core:notice] [pid 29254] AH00094: Command line: '/usr/sbin/apache2'
[Fri Jul 02 11:46:13.416556 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] mod_wsgi (pid=29256): Failed to exec Python script file '/home/my_usr/web2/web2/wsgi.py'.
[Fri Jul 02 11:46:13.416650 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] mod_wsgi (pid=29256): Exception occurred processing WSGI script '/home/my_usr/web2/web2/wsgi.py'.
[Fri Jul 02 11:46:13.417397 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] Traceback (most recent call last):
[Fri Jul 02 11:46:13.417464 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] File "/home/my_usr/web2/web2/wsgi.py", line 24, in <module>
[Fri Jul 02 11:46:13.417517 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] application = get_wsgi_application()
[Fri Jul 02 11:46:13.417526 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] File "/home/my_usr/web2/env/lib/python3.7/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
[Fri Jul 02 11:46:13.417530 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] django.setup(set_prefix=False)
[Fri Jul 02 11:46:13.417546 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] File "/home/my_usr/web2/env/lib/python3.7/site-packages/django/__init__.py", line 19, in setup
[Fri Jul 02 11:46:13.417550 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
[Fri Jul 02 11:46:13.417555 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] File "/home/my_usr/web2/env/lib/python3.7/site-packages/django/conf/__init__.py", line 82, in __getattr__
[Fri Jul 02 11:46:13.417559 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] self._setup(name)
[Fri Jul 02 11:46:13.417564 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] File "/home/my_usr/web2/env/lib/python3.7/site-packages/django/conf/__init__.py", line 69, in _setup
[Fri Jul 02 11:46:13.417567 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] self._wrapped = Settings(settings_module)
[Fri Jul 02 11:46:13.417572 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] File "/home/my_usr/web2/env/lib/python3.7/site-packages/django/conf/__init__.py", line 170, in __init__
[Fri Jul 02 11:46:13.417575 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] mod = importlib.import_module(self.SETTINGS_MODULE)
[Fri Jul 02 11:46:13.417580 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] File "/usr/lib/python3.7/importlib/__init__.py", line 127, in import_module
[Fri Jul 02 11:46:13.417583 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] return _bootstrap._gcd_import(name[level:], package, level)
[Fri Jul 02 11:46:13.417588 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
[Fri Jul 02 11:46:13.417593 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] File "<frozen importlib._bootstrap>", line 983, in _find_and_load
[Fri Jul 02 11:46:13.417598 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
[Fri Jul 02 11:46:13.417603 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
[Fri Jul 02 11:46:13.417608 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
[Fri Jul 02 11:46:13.417612 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] File "<frozen importlib._bootstrap>", line 983, in _find_and_load
[Fri Jul 02 11:46:13.417617 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
[Fri Jul 02 11:46:13.417634 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] ModuleNotFoundError: No module named 'web1'
然而,如果我执行 sudo service apache2 restart
并直接进入 host/web2 现在 web2 工作得很好但是如果在那之后我尝试进入 host/web1 它显示 web2.
所以基本上如果我重新启动 apache2 服务然后转到 host/web1 然后 host/web2 我得到一条消息错误和已发布的日志。
如果我重新启动 apache2 服务然后转到 host/web2 然后 host/web1 我得到 web2 内容即使 url 是 host/web2
有什么线索吗?
提前致谢
好的,我明白了!希望这对将来的人有帮助。 所以我的 .conf 文件从一开始就很好,但给我带来麻烦的是我的 django 项目的 .settings 文件。 感谢 this 文章,我修复了它。
我有这样的设置文件:
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
并将其更改为:
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'