简体   繁体   中英

How do I use multiple settings file in Django with multiple sites on one server?

I have an ec2 instance running Ubuntu 14.04 and I want to host two sites from it. On my first site I have two settings file, production_settings.py and settings.py (for local development). I import the local settings into the production settings and override any settings with the production settings file.

Since my production settings file is not the default settings.py name, I have to create an environment variable

DJANGO_SETTINGS_MODULE='site1.production_settings'

However because of this whenever I try to start my second site it says

No module named site1.production_settings

I am assuming that this is due to me setting the environment variable. Another problem is that I won't be able to use different settings file for different sites.

How do I start use two different settings file for two different websites?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM