views:

1760

answers:

2

Hello, I want to host several sites with under the same server which uses Debian 5, say I have site1,site2 and site3 and assume my ip is 155.55.55.1 155.55.55.1:80 for my site1 , script resides at /opt/django/site1/ 155.55.55.1:8080 for my site2 , script resides at /opt/django/site2/ 155.55.55.1:8090 for my site3 , script resides at /opt/django/site3/

Here is my current apache default

<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>

And here is my wsgi config for my site1 that resides under /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()

How can I add site2 and site3 that are django based sites and will be served as same as site1?

+2  A: 

The docs tell you exactly how to do that:

http://docs.djangoproject.com/en/dev/howto/deployment/modpython/#multiple-django-installations-on-the-same-apache

Paul McMillan
They are not using mod_python, they are using mod_wsgi.
Graham Dumpleton
+3  A: 

Your ServerName/ServerAlias directives are wrong. ServerName should be hostname. You probably should just delete ServiceAlias.

Then just do the obvious and duplicate VirtualHost/Listen directives, just changing the port number and locations of scripts in the file system.

Finally, do not set DocumentRoot to be where your Django code is as it makes it easier to accidentally expose your source code to download if you stuff up Apache configuration. So, just remove DocumentRoot directive from VirtualHost for Django sites.

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>

I have also add missing Directory directive for allowing access to static files. You should review paths however.

Make sure you read:

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

for further information.


UPDATE 1

BTW, since you are using PHP in same Apache, you would be much better off using mod_wsgi daemon mode and push each Django instance out into its own separate process. That allows those processes to be multithreaded, even though main Apache processes are forced to be single threaded because of PHP. End result will be much much less memory being used than if running multiple Django instances in each process under embedded mode with prefork MPM. Your Django code just needs to be thread safe. Configuration in addition to above would be to add WSGIDaemonProcess/WSGIProcessGroup to each Django VirtualHost, where name of daemon process group is different for each VirtualHost.

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

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

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

This also allows you to more easily restart each Django instance without restart whole of Apache. Read:

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

Graham Dumpleton
wow thank you for your gentle and detailed explanation Graham! I am running my sites internally for my company so it has no domain but an internal dns and can be accessable only locally via http://mysite/ , in this case shall I change ServerName www.example.com to ServerName mysite ?
Hellnar
Yep. That should work.
mlissner