[Django]-Setting up several Django apps on one server

2👍

Make two configuration files in /etc/apache2/sites-available folder. Give them proper logical names according to your sites (e.g. example1.com , example12.com etc). Use a2ensite command to enable both of them and restart your apache server.

Each of your config file should look something like this:

<Virtualhost *:8032>
ServerName localhost 
ServerAdmin webmaster@example.com
DocumentRoot "/usr/local/www/djcode/test"
 <Directory "/usr/local/www/djcode/test">
     Options +ExecCGI
     Order allow,deny
     Allow from all
 </Directory>
 Alias /site_media "/usr/local/www/djcode/test/site_media/"

 Alias /media "/usr/local/www/djcode/test/site_media/media/"
 WSGIDaemonProcess test user=www group=www processes=2 threads=5
 WSGIProcessGroup test
 AddHandler wsgi-script .wsgi
 RewriteEngine On
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteRule ^(.*)$ /test.wsgi/$1 [QSA,L]
 </Virtualhost *:8032>

You’ll also need to add up some directives to serve static content. Serving static content from apache has overhead so it’ll be a better idea if you server it using some lightweight webserver such as Lighttpd or Nginx and connect to apache using reverse proxy to serve the django based content. Here’s a tutorial on using NginX

Since you are using mod_wsgi, you can run each site as different user as well so that two of them may not access each other’s data. This is useful if the two sites belong to different stakeholders.

2👍

It is not clear whether you want them to both be hosted under the same VirtualHost or not. Others have gone off and told you to use separate VirtualHost’s but that isn’t necessary and it can be done under the same VirtualHost. Some have provided a configuration using mod_python when you were actually using mod_wsgi. You also technically didn’t need the Alias directives for the static media, although where you stored them may need to change depending on URL you expected to be able to use to access them.

That all said, for your current configuration, because you have used AddHandler to map .wsgi files you can already host multiple applications, you would just need to create multiple .wsgi files in the document directory and use the appropriate URL to access them. Further configuration could be added to avoid needing to specify the ‘.wsgi’ extensions in the URL.

I can give a proper answer if you do the following:

  • Say whether they need to be under the same VirtualHost.

  • Say what URL within the VirtualHost each distinct application should be accessible as.

  • Say what media URL should be used for each distinct application.

  • Say whether each should run in a separate process, or whether them running in different sub interpreters of the same process is adequate. Running in separate processes would allow each to be restarted independently when making code changes.

1👍

You can create multiple Virtual Hosts in Apache, and modify the following for each app:

<VirtualHost *:80>
  DocumentRoot /var/www
  ServerName www.site.com

      <location "/<name>">
           SetHandler python-program
           PythonHandler django.core.handlers.modpython
           SetEnv DJANGO_SETTINGS_MODULE <app name>.settings
           PythonPath "['/path/to/app'] + sys.path"
       </location>
 </VirtualHost>

<VirtualHost *:80>
  DocumentRoot /var/www/site2
  ServerName www.site2.com

      <location "/<name2>">
           SetHandler python-program
           PythonHandler django.core.handlers.modpython
           SetEnv DJANGO_SETTINGS_MODULE <app2 name>.settings
           PythonPath "['/path/to/app2'] + sys.path"
       </location>
 </VirtualHost>

edit: also add the following to each Virtual Host

<location "/media">
    SetHandler None
</location>

<location "/admin_media">
    SetHandler None
</location>

<locationmatch ".(jpg|gif|png)$">
    SetHandler None
</locationmatch>
👤Jason

1👍

I’ve encountered something like this. Here’s some related questions, albeit not precisely on point:

Hope it helps, though.

0👍

For the admin media you can put the same alias in both virtual hosts, or if they need to be different setup a copy of them and have 2 different aliases.

Leave a comment