Apache部署多个django site project

按照TUT-Apache+Django+mod_wsgi教学文件中在apache中部署一个django site已经OK,如何在Apache下部署多个django site呢

背景

nuMaps India准备部署在同一台DZM(192.168.200.241)机器上,按照先前的apache配置并不能同时运行两个wsgi django site,原因在于httpd.conf配置“WSGIScriptAlias / /path/to/mysite/django.wsgi”将所有的请求转发到一个django wsgi application上

先前numaps.garmin.com/numaps.garmin.com.cn/numaps.garmin.com.tw三个域名对应到同一台DMZ主机上,并且在Apache上也没有对这三个域名做额外处理,这三个域名都用于nuMaps China回报系统和GarMaps API

之后India回报系统域名numaps.garmin.co.in(待定)也会映射到192.168.200.241上,那么如何区分不同HTTP Host的Request,答案就是使用Apache Virtual Host

配置

  •  注释或者删除原先配置行
    #WSGIScriptAlias / /path/to/mysite/django.wsgi
    #<Directory /path/to/mysite>
    #       Order Deny,Allow
    #       Allow from all
    #</Directory>
  • 去掉默认virtual host的include注释,如果默认没有virtual host,手动添加配置
    Include conf/extra/httpd-vhosts.conf
  • 配置virtual host
    NameVirtualHost *:80        #配置NameVirtualHost,用于VirtualHost指令
    <VirtualHost *:80>#配置特定IP或者任意IP下特定端口,此处"*:80"参数必须是前面配置好的NamedVirtualHost
        ServerAdmin webmaster@dummy-host2.garmin.com.tw
        DocumentRoot "C:/Documents and Settings/gis/My Documents/nuMapIndia/"
        ServerName numaps.garmin.com #注意此域名对应不同site,目前将该域名用于测试India回报系统。ServerName的域名一定要和DNS中配置的域名一致
        WSGIScriptAlias / "C:/Documents and Settings/gis/My Documents/nuMapIndia/nuMapIndia/wsgi.py"
    </VirtualHost>
    <VirtualHost *:80>
        ServerAdmin webmaster@dummy-host2.garmin.com.tw
        DocumentRoot "C:/Documents and Settings/gis/My Documents/mySite/"
        ServerName numaps.garmin.com.cn
        WSGIScriptAlias / "C:/Documents and Settings/gis/My Documents/mySite/wsgi.py" #WSGIScriptAlias配置还是原来一致
    </VirtualHost>
    <VirtualHost *:80>
        ServerAdmin webmaster@dummy-host2.garmin.com.tw
        DocumentRoot "C:/Documents and Settings/gis/My Documents/mySite/"
        ServerName numaps.garmin.com.tw
        WSGIScriptAlias / "C:/Documents and Settings/gis/My Documents/mySite/wsgi.py"
    </VirtualHost>
    #配置目录权限
    <Directory "C:/Documents and Settings/gis/My Documents/nuMapIndia/">
        Order deny,allow
        Allow from all
    </Directory>
    <Directory "C:/Documents and Settings/gis/My Documents/mySite/">
        Order deny,allow
        Allow from all
    </Directory>

错误

配置完成,启动apache,先访问第一个site没有问题,但是再访问第二个site时出现500错误
错误日志如下

[Thu Apr 18 20:08:03 2013] [error] [client 10.127.9.123] mod_wsgi (pid=7592): Exception occurred processing WSGI script 'C:/Documents and Settings/gis/My Documents/mySite/wsgi.py'.
[Thu Apr 18 20:08:03 2013] [error] [client 10.127.9.123] Traceback (most recent call last):
[Thu Apr 18 20:08:03 2013] [error] [client 10.127.9.123]   File "C:\\Python27\\lib\\site-packages\\django\\core\\handlers\\wsgi.py", line 236, in __call__
[Thu Apr 18 20:08:03 2013] [error] [client 10.127.9.123]     self.load_middleware()
[Thu Apr 18 20:08:03 2013] [error] [client 10.127.9.123]   File "C:\\Python27\\lib\\site-packages\\django\\core\\handlers\\base.py", line 51, in load_middleware
[Thu Apr 18 20:08:03 2013] [error] [client 10.127.9.123]     mod = import_module(mw_module)
[Thu Apr 18 20:08:03 2013] [error] [client 10.127.9.123]   File "C:\\Python27\\lib\\site-packages\\django\\utils\\importlib.py", line 35, in import_module
[Thu Apr 18 20:08:03 2013] [error] [client 10.127.9.123]     __import__(name)
[Thu Apr 18 20:08:03 2013] [error] [client 10.127.9.123]   File "C:\\Python27\\lib\\site-packages\\django\\contrib\\auth\\middleware.py", line 3, in <module>
[Thu Apr 18 20:08:03 2013] [error] [client 10.127.9.123]     from django.contrib.auth.backends import RemoteUserBackend
[Thu Apr 18 20:08:03 2013] [error] [client 10.127.9.123]   File "C:\\Python27\\lib\\site-packages\\django\\contrib\\auth\\backends.py", line 3, in <module>
[Thu Apr 18 20:08:03 2013] [error] [client 10.127.9.123]     from django.contrib.auth.models import Permission
[Thu Apr 18 20:08:03 2013] [error] [client 10.127.9.123]   File "C:\\Python27\\lib\\site-packages\\django\\contrib\\auth\\models.py", line 8, in <module>
[Thu Apr 18 20:08:03 2013] [error] [client 10.127.9.123]     from django.db import models
[Thu Apr 18 20:08:03 2013] [error] [client 10.127.9.123]   File "C:\\Python27\\lib\\site-packages\\django\\db\\__init__.py", line 40, in <module>
[Thu Apr 18 20:08:03 2013] [error] [client 10.127.9.123]     backend = load_backend(connection.settings_dict['ENGINE'])
[Thu Apr 18 20:08:03 2013] [error] [client 10.127.9.123]   File "C:\\Python27\\lib\\site-packages\\django\\db\\__init__.py", line 34, in __getattr__
[Thu Apr 18 20:08:03 2013] [error] [client 10.127.9.123]     return getattr(connections[DEFAULT_DB_ALIAS], item)
[Thu Apr 18 20:08:03 2013] [error] [client 10.127.9.123]   File "C:\\Python27\\lib\\site-packages\\django\\db\\utils.py", line 93, in __getitem__
[Thu Apr 18 20:08:03 2013] [error] [client 10.127.9.123]     backend = load_backend(db['ENGINE'])
[Thu Apr 18 20:08:03 2013] [error] [client 10.127.9.123]   File "C:\\Python27\\lib\\site-packages\\django\\db\\utils.py", line 27, in load_backend
[Thu Apr 18 20:08:03 2013] [error] [client 10.127.9.123]     return import_module('.base', backend_name)
[Thu Apr 18 20:08:03 2013] [error] [client 10.127.9.123]   File "C:\\Python27\\lib\\site-packages\\django\\utils\\importlib.py", line 35, in import_module
[Thu Apr 18 20:08:03 2013] [error] [client 10.127.9.123]     __import__(name)
[Thu Apr 18 20:08:03 2013] [error] [client 10.127.9.123]   File "C:\\Python27\\lib\\site-packages\\django\\db\\backends\\sqlserver_ado\\base.py", line 6, in <module>
[Thu Apr 18 20:08:03 2013] [error] [client 10.127.9.123]     import dbapi as Database
[Thu Apr 18 20:08:03 2013] [error] [client 10.127.9.123]   File "C:\\Python27\\lib\\site-packages\\django\\db\\backends\\sqlserver_ado\\dbapi.py", line 49, in <module>
[Thu Apr 18 20:08:03 2013] [error] [client 10.127.9.123]     import pythoncom
[Thu Apr 18 20:08:03 2013] [error] [client 10.127.9.123]   File "C:\\Python27\\lib\\site-packages\\pythoncom.py", line 2, in <module>
[Thu Apr 18 20:08:03 2013] [error] [client 10.127.9.123]     import pywintypes
[Thu Apr 18 20:08:03 2013] [error] [client 10.127.9.123]   File "C:\\Python27\\lib\\site-packages\\win32\\lib\\pywintypes.py", line 124, in <module>
[Thu Apr 18 20:08:03 2013] [error] [client 10.127.9.123]     __import_pywin32_system_module__("pywintypes", globals())
[Thu Apr 18 20:08:03 2013] [error] [client 10.127.9.123]   File "C:\\Python27\\lib\\site-packages\\win32\\lib\\pywintypes.py", line 114, in __import_pywin32_system_module__
[Thu Apr 18 20:08:03 2013] [error] [client 10.127.9.123]     assert sys.modules[modname] is old_mod
[Thu Apr 18 20:08:03 2013] [error] [client 10.127.9.123] AssertionError

解决方法

其他

    • 之前关于apache下同时运行两个django site共享环境变量的可能似乎没有问题
    • 在本地运行两个django site测试发现一个奇怪的问题:site访问正常,但是当登陆第一个site后,再去登陆第二个site,登陆成功后点获取回报列表提示未登录,这时再点击一个site的回报列表也提示未登录
      难道django session失效,共用session?共享内存数据了?重启机器后在本地测试没有出现这个问题。在192.168.200.241上测试完全就没有遇到这个问题。
posted @ 2013-04-25 10:51 重庆Debug 阅读(...) 评论(...) 编辑 收藏