[英]How to deploy multiple Django sites with DRY configuration?
Currently I am deploying more than 30 websites which are operated by a single application, written in Django.目前我正在部署 30 多个由单个应用程序操作的网站,这些应用程序是用 Django 编写的。 The sites are combined into smaller groups.
这些站点被组合成更小的组。 For example, there is a group Europe, which includes websites Germany, France, Switzerland, etc. Those groups are 7.
例如,有一个欧洲组,其中包括德国、法国、瑞士等网站,这些组是 7。
For deployment uwsgi (operating in Emperior mode), nginx (uwsgi passing to the group socket) and supervisord (for task workers) are used.对于部署 uwsgi(在 Emperior 模式下运行),使用 nginx(uwsgi 传递到组套接字)和 supervisord(用于任务工作人员)。 Application is using a single database for all websites.
应用程序为所有网站使用单一数据库。
The structure of settings files in the project currently is as follows:目前项目中设置文件的结构如下:
project.settings.general_settings
project.settings.<group_name>
So far I thought that it would be enough to simply have 7 uwsgi and 7 nginx configuration files (for each group), until I realized some of the websites will also need custom Django settings, and separate DJANGO_SETTINGS_MODULE
will have to be defined for each website (some of those websites have different languages lets say).到目前为止,我认为只需要 7 个 uwsgi 和 7 个 nginx 配置文件(每个组)就足够了,直到我意识到某些网站还需要自定义 Django 设置,并且必须为每个网站定义单独的
DJANGO_SETTINGS_MODULE
(其中一些网站有不同的语言,可以说)。
Things to keep in mind:要记住的事情:
DJANGO_SETTINGS_MODULE
is specified in both Nginx and uwsgi configuration files.DJANGO_SETTINGS_MODULE
在 Nginx 和 uwsgi 配置文件中都有指定。 Is there a way to avoid this duplication? What was considered so far:到目前为止的考虑:
LANGUAGES
, LANGUAGE_CODE
, etc.) stored in the database and dynamically modified for each request.LANGUAGES
、 LANGUAGE_CODE
等)存储在数据库中,并针对每个请求进行动态修改。 The idea was rejected, as it obviously sucks .If you're using emperor mode and your uWSGI configs differs only in socket and DJANGO_SETTINGS_MODULE
, consider using one config file for all sites.如果您使用的是皇帝模式并且您的 uWSGI 配置仅在套接字和
DJANGO_SETTINGS_MODULE
有所不同,请考虑对所有站点使用一个配置文件。 You will start new site basically by creating symlink in uWSGI emperor directory and special variables inside that config will construct socket names and DJANGO_SETTINGS_MODULE
variable based on symlink name.您将基本上通过在 uWSGI 皇帝目录中创建符号链接来启动新站点,并且该配置中的特殊变量将基于符号链接名称构造套接字名称和
DJANGO_SETTINGS_MODULE
变量。
If you're only worried about urlpatterns, language and other values that can be determined at request, you can use django-hosts
instead of built-in sites module.如果您只担心可以根据请求确定的 urlpatterns、语言和其他值,您可以使用
django-hosts
而不是内置的站点模块。 That will also allow you to serve multiple sites on one workers pool.这也将允许您在一个工作人员池上为多个站点提供服务。 You can also create some middleware for that purpose.
您还可以为此创建一些中间件。
Note that you can also use environment variables (which can be set in uWSGI config file) to set some of settings.请注意,您还可以使用环境变量(可以在 uWSGI 配置文件中设置)来设置一些设置。 Just use
os.environ
in your settings file to extract them.只需在您的设置文件中使用
os.environ
来提取它们。
And last, but not least - settings file can be set dynamically in wsgi.py file based on some other variables.最后但并非最不重要的 - 设置文件可以根据其他一些变量在 wsgi.py 文件中动态设置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.