63

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:

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/

Here is my 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 site1, at /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, which are Django-based sites and will be served like site1?

Serjik
  • 10,543
  • 8
  • 61
  • 70
Hellnar
  • 62,315
  • 79
  • 204
  • 279

2 Answers2

110

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

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

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

atp
  • 30,132
  • 47
  • 125
  • 187
Graham Dumpleton
  • 57,726
  • 6
  • 119
  • 134
  • 2
    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 Oct 13 '09 at 05:57
  • 1
    I also like to add python-path to the WSGIDaemonProcess line to take advantage of virtualenv and localize my python site-packages somewhere intelligent – rjmoggach Oct 22 '12 at 01:50
  • 1
    @mogga If using mod_wsgi 3.4, you are better off using new python-home option instead of python-path. – Graham Dumpleton Oct 22 '12 at 04:46
  • @GrahamDumpleton you told that `DocumentRoot` should not be in virtualhost but then where do i put it then. or there is no need for Document root for django apps. Does same thing hold for php virualconf – user1958218 Jul 05 '13 at 06:26
  • 2
    I did not say 'DocumentRoot should not be in virtualhost'. I said 'do not set DocumentRoot to be where your Django code is'. DocumentRoot refers to a directory. If WSGIScrtipAlias wasn't defined, Apache will serve files up from the directory listed against DocumentRoot. It would thus be unwise to have DocumentRoot refer to a directory where your source code is, because if WSGIScriptAlias wasn't getting applied for some reason, people could download your source code. – Graham Dumpleton Jul 08 '13 at 04:51
  • 1
    On CentOS 6, I was getting `Permission denied: mod_wsgi...Unable to connect to WSGI daemon process...`. The solution was to to add `WSGISocketPrefix /var/run/wsgi` to my main apache conf file, i.e. outside of the `VirtualHost` settings. Also, running mod_wsgi in daemon mode solved some `can't pickle...` errors I was seeing. See [this](http://serverfault.com/questions/338826/apache-permission-denied-on-mod-wsgi-fixed-with-wsgisocketprefix-but-why) and [this](https://bitbucket.org/mchaput/whoosh/issue/314/picklingerror-cant-pickle-apache-windows-7). – Banjer Aug 19 '13 at 19:05
2

Putting all virtualHost configuration in one place works fine, but Debian has its own concept by separating them in a file for each site in /etc/apache2/sites-available, which are activated by symlinking them in ../sites-enabled. In this way a server-admin could also assign separate access rights to the config file for each of the site-admin unix users, scripts can check if a site is active etc.

Basically it would be nice to have one central howto for Django-Admin installations, the current multitude of separate docs, links and blog articles is not really helpful for the proliferation of Django.

Ed Doerr
  • 61
  • 5