将 Django 站点放在另一个 Django 站点的子目录中
Put Django site in sub directory of another django site
站点在本地主机上运行良好,但是当我转到 localhost/site2
时,它只加载根 Django 站点的 404 页面。
<VirtualHost *:80>
ServerName localhost
#site1.com
#ServerAlias www.site1.com
ErrorLog /var/www/virtualenv-2.7/django-error-log
Alias /static/ /var/www/virtualenv-2.7/site1/mainapp/static/
Alias /media/ /var/www/virtualenv-2.7/site1/media/
WSGIDaemonProcess site1 python-path=/var/www/virtualenv-2.7/site1:/var/www/virtualenv-2.7/lib/python2.7/site-packages
WSGIProcessGroup site1
WSGIScriptAlias / /var/www/virtualenv-2.7/site1/site1/wsgi.py
<Directory /var/www/virtualenv-2.7/site1>
<Files wsgi.py>
Allow from all
</Files>
</Directory>
WSGIDaemonProcess site2 python-path=/var/www/virtualenv-2.7/site2:/var/www/virtualenv-2.7/lib/python2.7/site-packages
WSGIProcessGroup site2
WSGIScriptAlias /site2 /var/www/virtualenv-2.7/site2/site2/wsgi.py
<Directory /var/www/virtualenv-2.7/site2>
<Files wsgi.py>
Allow from all
</Files>
</Directory>
</VirtualHost>
WSGISocketPrefix /var/run/wsgi
WSGIPythonPath /var/www/virtualenv-2.7/site1:var/www/virutalenv-2.7/lib/python2.7/site-packages:/var/www/virutalenv-2.7/site2:
WSGIPythonHome /var/www/virtualenv-2.7
你不能 运行 两个站点或一个 django 应用程序在一个 VirtualHost
中,这个配置是错误的。
您将需要两个不同的虚拟主机来执行此操作,如下所示:
<VirtualHost *:80>
ServerName site1.com
ServerAlias www.site1.com
ErrorLog /var/www/virtualenv-2.7/django-error-log-from-site1
Alias /static/ /var/www/virtualenv-2.7/site1/mainapp/static/
Alias /media/ /var/www/virtualenv-2.7/site1/media/
WSGIDaemonProcess site1 python-path=/var/www/virtualenv-2.7/site1:/var/www/virtualenv-2.7/lib/python2.7/site-packages
WSGIProcessGroup site1
WSGIScriptAlias / /var/www/virtualenv-2.7/site1/site1/wsgi.py
<Directory /var/www/virtualenv-2.7/site1>
<Files wsgi.py>
Allow from all
</Files>
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName site2.com
ServerAlias www.site2.com
ErrorLog /var/www/virtualenv-2.7/django-error-log-from-site2
Alias /static/ /var/www/virtualenv-2.7/site2/mainapp/static/
Alias /media/ /var/www/virtualenv-2.7/site2/media/
WSGIDaemonProcess site2 python-path=/var/www/virtualenv-2.7/site2:/var/www/virtualenv-2.7/lib/python2.7/site-packages
WSGIProcessGroup site2
WSGIScriptAlias /site2 /var/www/virtualenv-2.7/site2/site2/wsgi.py
<Directory /var/www/virtualenv-2.7/site2>
<Files wsgi.py>
Allow from all
</Files>
</Directory>
</VirtualHost>
如果您在本地系统上对此进行测试,那么根据您的 OS 您可能需要进行以下配置以进行试用。
- 在Windows中:
C:\Windows\System32\Drivers\etc\hosts
- 在Linux/Mac中:
/etc/hosts
在此文件中添加以下行:
127.0.0.1 site1.com
127.0.0.1 site2.com
如果您使用的是域名,那么A Records
指向所需的服务器 IP。
现在,当您在浏览器中打开 site1.com
时,将首先使用 VH 配置,同样对于 site2.com
,当将其键入 URL.
时
再想一想。你真正需要的是 conditional 配置。在 Apache 2.4 中可以使用 <If>
语句 (docs here)
因此您的配置将转换为
<VirtualHost *:80>
ServerName localhost
#site1.com
#ServerAlias www.site1.com
ErrorLog /var/www/virtualenv-2.7/django-error-log
<If "%{QUERY_STRING} =~ ^/site1">
Alias /static/ /var/www/virtualenv-2.7/site1/mainapp/static/
Alias /media/ /var/www/virtualenv-2.7/site1/media/
WSGIDaemonProcess site1 python-path=/var/www/virtualenv-2.7/site1:/var/www/virtualenv-2.7/lib/python2.7/site-packages
WSGIProcessGroup site1
WSGIScriptAlias / /var/www/virtualenv-2.7/site1/site1/wsgi.py
<Directory /var/www/virtualenv-2.7/site1>
<Files wsgi.py>
Allow from all
</Files>
</Directory>
</If>
<Else>
Alias /static/ /var/www/virtualenv-2.7/site2/mainapp/static/
Alias /media/ /var/www/virtualenv-2.7/site2/media/
WSGIDaemonProcess site2 python-path=/var/www/virtualenv-2.7/site2:/var/www/virtualenv-2.7/lib/python2.7/site-packages
WSGIProcessGroup site2
WSGIScriptAlias /site2 /var/www/virtualenv-2.7/site2/site2/wsgi.py
<Directory /var/www/virtualenv-2.7/site2>
<Files wsgi.py>
Allow from all
</Files>
</Directory>
</Else>
</VirtualHost>
我没有安装 apache 2.4 的系统来测试,所以可能会遗漏一些东西。
站点在本地主机上运行良好,但是当我转到 localhost/site2
时,它只加载根 Django 站点的 404 页面。
<VirtualHost *:80>
ServerName localhost
#site1.com
#ServerAlias www.site1.com
ErrorLog /var/www/virtualenv-2.7/django-error-log
Alias /static/ /var/www/virtualenv-2.7/site1/mainapp/static/
Alias /media/ /var/www/virtualenv-2.7/site1/media/
WSGIDaemonProcess site1 python-path=/var/www/virtualenv-2.7/site1:/var/www/virtualenv-2.7/lib/python2.7/site-packages
WSGIProcessGroup site1
WSGIScriptAlias / /var/www/virtualenv-2.7/site1/site1/wsgi.py
<Directory /var/www/virtualenv-2.7/site1>
<Files wsgi.py>
Allow from all
</Files>
</Directory>
WSGIDaemonProcess site2 python-path=/var/www/virtualenv-2.7/site2:/var/www/virtualenv-2.7/lib/python2.7/site-packages
WSGIProcessGroup site2
WSGIScriptAlias /site2 /var/www/virtualenv-2.7/site2/site2/wsgi.py
<Directory /var/www/virtualenv-2.7/site2>
<Files wsgi.py>
Allow from all
</Files>
</Directory>
</VirtualHost>
WSGISocketPrefix /var/run/wsgi
WSGIPythonPath /var/www/virtualenv-2.7/site1:var/www/virutalenv-2.7/lib/python2.7/site-packages:/var/www/virutalenv-2.7/site2:
WSGIPythonHome /var/www/virtualenv-2.7
你不能 运行 两个站点或一个 django 应用程序在一个 VirtualHost
中,这个配置是错误的。
您将需要两个不同的虚拟主机来执行此操作,如下所示:
<VirtualHost *:80>
ServerName site1.com
ServerAlias www.site1.com
ErrorLog /var/www/virtualenv-2.7/django-error-log-from-site1
Alias /static/ /var/www/virtualenv-2.7/site1/mainapp/static/
Alias /media/ /var/www/virtualenv-2.7/site1/media/
WSGIDaemonProcess site1 python-path=/var/www/virtualenv-2.7/site1:/var/www/virtualenv-2.7/lib/python2.7/site-packages
WSGIProcessGroup site1
WSGIScriptAlias / /var/www/virtualenv-2.7/site1/site1/wsgi.py
<Directory /var/www/virtualenv-2.7/site1>
<Files wsgi.py>
Allow from all
</Files>
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName site2.com
ServerAlias www.site2.com
ErrorLog /var/www/virtualenv-2.7/django-error-log-from-site2
Alias /static/ /var/www/virtualenv-2.7/site2/mainapp/static/
Alias /media/ /var/www/virtualenv-2.7/site2/media/
WSGIDaemonProcess site2 python-path=/var/www/virtualenv-2.7/site2:/var/www/virtualenv-2.7/lib/python2.7/site-packages
WSGIProcessGroup site2
WSGIScriptAlias /site2 /var/www/virtualenv-2.7/site2/site2/wsgi.py
<Directory /var/www/virtualenv-2.7/site2>
<Files wsgi.py>
Allow from all
</Files>
</Directory>
</VirtualHost>
如果您在本地系统上对此进行测试,那么根据您的 OS 您可能需要进行以下配置以进行试用。
- 在Windows中:
C:\Windows\System32\Drivers\etc\hosts
- 在Linux/Mac中:
/etc/hosts
在此文件中添加以下行:
127.0.0.1 site1.com
127.0.0.1 site2.com
如果您使用的是域名,那么A Records
指向所需的服务器 IP。
现在,当您在浏览器中打开 site1.com
时,将首先使用 VH 配置,同样对于 site2.com
,当将其键入 URL.
再想一想。你真正需要的是 conditional 配置。在 Apache 2.4 中可以使用 <If>
语句 (docs here)
因此您的配置将转换为
<VirtualHost *:80>
ServerName localhost
#site1.com
#ServerAlias www.site1.com
ErrorLog /var/www/virtualenv-2.7/django-error-log
<If "%{QUERY_STRING} =~ ^/site1">
Alias /static/ /var/www/virtualenv-2.7/site1/mainapp/static/
Alias /media/ /var/www/virtualenv-2.7/site1/media/
WSGIDaemonProcess site1 python-path=/var/www/virtualenv-2.7/site1:/var/www/virtualenv-2.7/lib/python2.7/site-packages
WSGIProcessGroup site1
WSGIScriptAlias / /var/www/virtualenv-2.7/site1/site1/wsgi.py
<Directory /var/www/virtualenv-2.7/site1>
<Files wsgi.py>
Allow from all
</Files>
</Directory>
</If>
<Else>
Alias /static/ /var/www/virtualenv-2.7/site2/mainapp/static/
Alias /media/ /var/www/virtualenv-2.7/site2/media/
WSGIDaemonProcess site2 python-path=/var/www/virtualenv-2.7/site2:/var/www/virtualenv-2.7/lib/python2.7/site-packages
WSGIProcessGroup site2
WSGIScriptAlias /site2 /var/www/virtualenv-2.7/site2/site2/wsgi.py
<Directory /var/www/virtualenv-2.7/site2>
<Files wsgi.py>
Allow from all
</Files>
</Directory>
</Else>
</VirtualHost>
我没有安装 apache 2.4 的系统来测试,所以可能会遗漏一些东西。