我想在使用Debian 5的同一服务器下托管多个站点,假设我有site1site2site3,并假设我的IP是155.55.55.1:

site1: 155.55.55.1:80  , script at /opt/django/site1/
site2: 155.55.55.1:8080, script at /opt/django/site2/
site3: 155.55.55.1:8090, script at /opt/django/site3/

以下是我的apache默认设置:

<VirtualHost *:80>
    ServerName /
    ServerAlias  */
    DocumentRoot /opt/django/site1/
    LogLevel warn
    WSGIScriptAlias / /opt/django/site1/apache/django.wsgi
    Alias /media /opt/django/site1/media/statics
    Alias /admin_media  /home/myuser/Django-1.1/django/contrib/admin/media 
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "/usr/share/phpmyadmin"
    ServerName /phpmyadmin
    Alias /phpmyadmin /usr/share/phpmyadmin
    <Directory /usr/share/phpmyadmin>
        Options Indexes FollowSymLinks
        AllowOverride None
        Order Deny,Allow
        Allow from all
    </Directory>
</VirtualHost>

这是我的wsgi配置,site1页,/opt/django/site1/apache/django.wsgi页:

import os, sys
import django.core.handlers.wsgi

sys.path.append('/opt/django')
sys.path.append('/opt/django/site1')

os.environ['DJANGO_SETTINGS_MODULE'] = 'site1.settings'
application = django.core.handlers.wsgi.WSGIHandler()

我如何添加site2site3,它们是基于Django的站点,将像site1一样提供服务?

推荐答案

您的ServerName/ServerAlias指令错误.服务器名应为主机名.您可能只需要删除ServerAlias即可.

然后只需执行明显且重复的VirtualHost/Listen指令,只需更改文件系统中脚本的端口号和位置.

最后,不要将DocumentRoot设置为Django代码所在的位置,因为这样在填充Apache配置时更容易意外地公开源代码以供下载.所以,只需从Django站点的VirtualHost中删除DocumentRoot指令.

Listen 80

<VirtualHost *:80>
ServerName www.example.com
WSGIScriptAlias / /opt/django/site1/apache/django.wsgi
Alias /media /opt/django/site1/media/statics
Alias /admin_media  /home/myuser/Django-1.1/django/contrib/admin/media

<Directory opt/django/site1/apache>
Order allow,deny
Allow from all
</Directory>

<Directory /home/myuser/Django-1.1/django/contrib/admin/media>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>

Listen 8080

<VirtualHost *:8080>
ServerName www.example.com
WSGIScriptAlias / /opt/django/site2/apache/django.wsgi
Alias /media /opt/django/site2/media/statics
Alias /admin_media  /home/myuser/Django-1.1/django/contrib/admin/media

<Directory opt/django/site2/apache>
Order allow,deny
Allow from all
</Directory>

<Directory /home/myuser/Django-1.1/django/contrib/admin/media>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>

Listen 8090

<VirtualHost *:8090>
ServerName www.example.com
WSGIScriptAlias / /opt/django/site3/apache/django.wsgi
Alias /media /opt/django/site3/media/statics
Alias /admin_media  /home/myuser/Django-1.1/django/contrib/admin/media

<Directory opt/django/site3/apache>
Order allow,deny
Allow from all
</Directory>

<Directory /home/myuser/Django-1.1/django/contrib/admin/media>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>

我还添加了允许访问静电文件的缺失目录指令.但是,您应该判断路径.

确保阅读:

http://code.google.com/p/modwsgi/wiki/IntegrationWithDjangohttp://code.google.com/p/modwsgi/wiki/ConfigurationGuidelines#Hosting_Of_Static_Files

了解更多信息.


更新1

顺便说一句,因为您在同一个Apache中使用PHP,所以最好使用mod_wsgi守护程序模式,并将每个Django实例推出到自己的单独进程中.这使得这些进程可以是多线程的,即使主要的Apache进程因为PHP而被迫是单线程的.最终的结果是,与使用prefork MPM在嵌入式模式下在每个进程中运行多个Django实例相比,使用的内存要少得多.Django代码只需要是线程安全的.除了上述配置之外,还需要将WSGIDaemonProcess/WSGIProcessGroup添加到每个Django VirtualHost,其中每个VirtualHost的守护进程组的名称不同.

<VirtualHost *:80>
WSGIDaemonProcess site1 display-name=%{GROUP}
WSGIProcessGroup site1
... existing stuff
</VirtualHost>

<VirtualHost *:8080>
WSGIDaemonProcess site2 display-name=%{GROUP}
WSGIProcessGroup site2
... existing stuff
</VirtualHost>

<VirtualHost *:8090>
WSGIDaemonProcess site3 display-name=%{GROUP}
WSGIProcessGroup site3
... existing stuff
</VirtualHost>

这还允许您更轻松地重新启动每个Django实例,而无需重新启动整个Apache.阅读:

http://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide#Delegation_To_Daemon_Processhttp://code.google.com/p/modwsgi/wiki/ReloadingSourceCode

Django相关问答推荐

使用override_sets构建一个预装饰的类,以更快的客户端.登录?

Django相关对象引用模板

创建用户/将用户登录到客户端服务器的标准OAuth/OIDC流程是什么?

当使用django-tinymce时,我在哪里指定referer?

Django中每个类型/代理的最新行

Django 中主键的隐式 UUID 自动字段

如何将我的函数添加到值参数?

Django:如何在表单 clean() 方法的 django 验证错误中添加 超链接?

Django ORM,按天分组

django 用一个提交按钮提交两种不同的表单

如何在 Django 视图中获取 URL 参数?

如何运行克隆的 Django 元素?

如何将 Django forms.ChoiceField 呈现为 Twitter Bootstrap 下拉菜单

Django中视图的多个decorator :执行顺序

jinja2模板引擎中的这个-是做什么的?

Django:访问给定字段的 Select 元组

如何创建一个在复选框右侧显示复选框标签的 Django 表单?

在 Django 中测试different layers的最佳实践是什么?

Django:以 10 为底的 int() 的无效文字

django rest 框架:从序列化程序 validate() 方法设置字段级错误