简体   繁体   English

如何在一台服务器上的多个站点中使用Django中的多个设置文件?

[英]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. 我有一个运行Ubuntu 14.04的ec2实例,我想从中托管两个站点。 On my first site I have two settings file, production_settings.py and settings.py (for local development). 在我的第一个站点上,我有两个设置文件production_settings.py和settings.py(用于本地开发)。 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 由于我的生产设置文件不是默认的settings.py名称,因此我必须创建一个环境变量

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 编辑:我错过了apache标签,所以我相应地更新了答案

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. 您的问题是在运行Django应用程序site2时,python解释器不知道site1中的任何模块,因为它未在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 在继续之前,我强烈建议您对PYTHONPATH的工作方式进行简短的阅读: http : //www.stereoplex.com/blog/understanding-imports-and-pythonpath

Now, there are many ways to resolve this, but we'll cover 3: 现在,有很多方法可以解决此问题,但我们将介绍3:

Modify your apache virtualhost configuration for site2: 修改site2的apache虚拟主机配置:

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. 首先,请务必阅读此处的文档,以了解mod_wsgi v1和v2之间的差异,但在运行ubuntu 14.04时,无论如何都应使用mod_wsgi v2。

https://code.google.com/p/modwsgi/wiki/ConfigurationDirectives#WSGIPythonPath 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. 这样做的好处是,在开发计算机上运行site2时,不修改任何应用程序代码,并且PYTHONPATH中的目录可能无效。

Append the path in your python code 在您的python代码中附加路径

Python sys.path - appending PYTHONPATH Python sys.path-附加PYTHONPATH

In your site2.wsgi file, before you start your Django app, add the following code. 在您的site2.wsgi文件中,在启动Django应用之前,添加以下代码。

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. 在开发(使用manage.py runserver)和生产之间移动时,这种方法也不会给您带来问题。

Create a simlink 创建一个simlink

How to symlink a file in Linux? 如何在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' 另一个简单的选择是简单地将您的production_settings.py链接到site2,然后设置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. 如果您是在Windows机器上进行开发,则这可能是3种方法中最棘手的问题,因此,如果要使用git或任何其他版本控制系统推送到服务器,请确保将新的simlink添加到.gitignore文件或您的VCS等效文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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