Recently I’ve had to deploy two Django projects using the same Apache server, with some added restrictions that made me impossible to apply all the solutions I found over the internet.

I developed a REST API using Django and everything worked like a charm, so good that when I had the need to develop a blog/web/shop I started looking for some python and Django based options.

My choice was to use Mezzanine with a custom theme and some models modifications.

The production server had to run on a third party cloud based solution and run over SSL.

Now was time to move code to the production server and the first thing that came to my mind was to deploy both projects using the same apache, something like this:

WSGIScriptAlias /api /var/www/api/proj/wsgi.py
WSGIScriptAlias / /var/www/web/wsgi.py

It seemed so obvious that I was surprised when, after some tests, I saw it was not working as expected. What is going wrong here?.

My first guess was that maybe wsgi had trouble deciding what python project to execute as the root path “/” also englobes “/api”, so I decided to clearly differentiate both turning “/” into “/web”.
So the second try I did was configuring the apache like this:

WSGIScriptAlias /api /var/www/api/proj/wsgi.py
WSGIScriptAlias /web /var/www/web/wsgi.py

Same luck this time! So I decided to look further into the problem and I saw WSGI had one instance of Django trying to run both projects.
The first project who received a get after an apache reset was the one working while the other failed with the “proj.settings” not found internal server error.

After googling around a little bit I found everyone was doing separate virtualhosts for each Django project. It seemed the right way of deploying multiple Django projects, the bad news were that I could not make separate virtual hosts. VirtualHosts can be made based on port, ip, domain or subdomains. I could not make separate VirtualHosts due to some restrictions, anyway I found some people having the same issue with separate VirtualHosts for each Django project, so I didn’t like it.

As a last chance I reviewed mod wsgi documentation and I saw the WSGIDaemonProcess and WSGIProcessGroup directives, that was what I needed!
So finally I could run each project in it’s own instance in it’s own process, great!.

The solution is something like this:

#(outside the virtualhost)
WSGIPythonPath /var/www/path/web:/var/www/path/api
#(Inside the virtualhost)
WSGIDaemonProcess api_wsgi user=user group=group
WSGIScriptAlias /api /var/www/path/api/wsgi.py
<Location /api>
WSGIProcessGroup api_wsgi
</Location>
 
WSGIDaemonProcess web_wsgi user=user group=group
WSGIScriptAlias /web /var/www/path/web/wsgi.py
<Location /web>
#SetEnv DJANGO_SETTINGS_MODULE project.settigs
WSGIProcessGroup web_wsgi
</Location>

Note As the commented SetEnv directive can help you set environment variables per project if needed.

4 thoughts on “Deploying multiple Django projects problems and solution

  1. Is it frowned upon to use multiple wsgi scripts on the same server? Should I try to create one that will handle all the projects? How would it look like, and how would the apache be configured to work with it? Is my intended structure above completely wrong?

  2. If you’re trying to actually deploy applications like that, create a single overarching Django project and use the urlconfs to include each Django app at a different sub url.

Leave a Reply

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