Running multiple Django projects on one Apache instance with mod_wsgi

I did not yet find a page with a concise but complete description of how to run multiple Django instances/projects on one server with Apache. This method is similar to the one in the Django docs, but the docs version does not run the Django instance in a daemon process, therefore it does not work out of the box to run multiple sites in parallel.

I found the following points are the most important:

  • Set SESSION_COOKIE_NAME  in the Django settings, so that there is no conflict between the cookies of your sites (especially when running on the same domain)
  • The STATIC_URL  path might also need to be adjusted to point to the correct subdirectory, e.g. /site1/static
  • The Apache site config must specify a different process-group for each Django site, leading to a sites.conf file like this:
# /etc/apache2/sites-available/site1-site2.conf
WSGIDaemonProcess site1 python-home=/var/www/site1 python-path=/var/www/site1
WSGIProcessGroup site1

WSGIDaemonProcess site2 python-home=/var/www/site2 python-path=/var/www/site2
WSGIProcessGroup site2

# works with SSL too
<VirtualHost *:80>
    Alias /site1/static/ /var/www/site1/myapp/static/
    
    <Directory /site1/static>
        Require all granted
    </Directory>
    
    WSGIScriptAlias /site1 /var/www/site1/myproj/wsgi.py process-group=site1
    
    <Directory /var/www/site1/myproj>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>


    Alias /site2/static/ /var/www/site2/myapp/static/
    
    <Directory /site2/static>
        Require all granted
    </Directory>
    
    WSGIScriptAlias /site2 /var/www/site2/myproj/wsgi.py process-group=site2
    
    <Directory /var/www/site2/myproj>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>
</VirtualHost>

There was no problem using the same strategy with a VirtualHost that uses SSL.

This was also posted as a Github gist.

2 comments

Leave a Reply

Your email address will not be published. Required fields are marked *