Windows上的apache上带有mod_wsgi的多个django站点

时间:2017-06-02 02:05:04

标签: django windows apache wsgi

我有一些令人头疼的问题,在Windows Server 2012上为多个站点设置Apache24和Python27以及wsgi(对于django 1.11.1)。设置为64位。网络搜索是矛盾的,所以我想我试一试(我在学习django的过程中)。现在我已经设置了它,我想我会发布它以防万一它会帮助其他django新手。

我发现您可以在此设置中使用vc10 msvcrt dll msvcr100.dll,这应该需要vc9(众多帖子)。我把它放在apache bin目录中,它对我来说很好。可从https://www.dll-files.com/msvcr100.dll.html下载。

我的设置如下。 project /是项目目录,我有2个站点site1和site2,每个站点都有自己的数据库。

目录结构:

./src:
project/
site1.sqlite3
site2.sqlite3

./src/project:
__init__.py
manage.py
site1/
site2/
settings.py
static/
urls.py
wsgi.py

settings.py:

...
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
...
INSTALLED_APPS = [
    'site1.apps.Site1Config',
    'site2.apps.Site2Config',
...
]
...
ROOT_URLCONF = 'urls'
...
WSGI_APPLICATION = 'wsgi.application'
...
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'site1.sqlite3'),        
    },
   'site1': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'site1.sqlite3'),
    },
    'site2': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'site2.sqlite3'),
    }         
}

默认用于所有落在裂缝中的东西,例如:认证

...
DATABASE_ROUTERS = ['site1.router.Router', 'site2.router.Router']
The routers are used to assign databases to apps -- see below.
...
STATIC_URL = '/static/'
STATIC_ROOT = '/djangoapache/src/project/static/'
Statics are gathered by python manage.py collectstatic
...

wsgi.py:

...
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
...

urls.py:

...
urlpatterns = [
    url(r'^site1/|accounts/', include('site1.urls')),
    url(r'^site2/', include('site2.urls')),
    url(r'^admin/', include(admin.site.urls)), 
]

帐户是指我的黑客版本的django身份验证框架。     ...

manage.py:

...
if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
...

site1/routers.py:
...
class Router(object):
    """
    A router to control all database operations on models in the
    site1 application.
    """
    def db_for_read(self, model, **hints):
        """
        Attempts to read site1 models go to site1 database
        """
        if model._meta.app_label == 'site1':
            return 'site1'
        return None

    def db_for_write(self, model, **hints):
        """
        Attempts to write site1 models go to the site1 db.
        """
        if model._meta.app_label == 'site1':
            return 'site1'
        return None

    def allow_relation(self, obj1, obj2, **hints):
        """
        Allow relations if a model in the site1 app is involved.
        """
        if obj1._meta.app_label == 'site1' or \
            obj2._meta.app_label == 'site1':
            return True
        return None

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        """
        Make sure the site1 app only appears in the site1 db
        database.
        """
        if app_label == 'site1':
            return db == 'site1'
        return None
...

的httpd.conf:

...
WSGIPythonHome /Python27
WSGIPythonPath /root/src/project

<VirtualHost *:8030>
... 
    Alias /static/ /root/src/project/static/
    WSGIScriptAlias / /root/src/project/wsgi.py

    <Directory  /root/src/project>
        Require all granted
    </Directory>
...    
</VirtualHost>
...

我还需要设置文件权限,以便Apache可以读取和/或写入必要的内容。

0 个答案:

没有答案