[Answer]-How do I use multiple settings file in Django with multiple sites on one server?

1👍

Edit: I missed the apache tag on this so I’ve updated my answer accordingly

Your problem is when running your Django app, site2, the python interpreter does not know about any of the modules in site1 because it’s not listed in your PYTHONPATH.

I would highly recommend doing a short amount of reading up on how PYTHONPATH works before you continue: http://www.stereoplex.com/blog/understanding-imports-and-pythonpath

Now, there are many ways to resolve this, but we’ll cover 3:

Modify your apache virtualhost configuration for site2:

Be sure to read the docs here, first, for differences between mod_wsgi v1 and v2 but as you’re running ubuntu 14.04, you should be using mod_wsgi v2 anyway.

https://code.google.com/p/modwsgi/wiki/ConfigurationDirectives#WSGIPythonPath

<virtualhost *:80>
    # your existing config directives
    WSGIPythonPath /path/to/site1
</virtualhost>

This has the advantage of not modifying any of your application code and, potentially having invalid directories in your PYTHONPATH when running site2 on your development machine.

Append the path in your python code

Python sys.path – appending PYTHONPATH

In your site2.wsgi file, before you start your Django app, add the following code.

import sys
sys.path.append('/path/to/site1')

Simple, and works. This approach will also not cause you problems when moving between development (using manage.py runserver) and production.

Create a simlink

How to symlink a file in Linux?

Another simple choice is to simply simlink your production_settings.py into site2, and and then set your DJANGO_SETTTINGS_MODULE=’site2.production_settings’

ln -s /path/to/site1/site1/production_settings.py /path/to/site2/site2/production_settings.py 

If you’re developing on a windows machine, this is probably the most problematic of the 3 approaches, so if you’re pushing to the server using git or any other version control system, make sure to add your new simlink to your .gitignore file or your VCS’s equivalent.

Leave a comment