在同一台服务器上为同一项目的不同版本加载配置文件的最佳方法是什么?

时间:2021-10-25 20:08:51

I have a large php project that relies on two levels of config files.

我有一个大型的php项目,它依赖于两个级别的配置文件。

In my project folder I have a default.config.ini that is loaded and then merged with a site specific config file.

在我的项目文件夹中,我有一个default.config.ini,它被加载,然后与特定于站点的配置文件合并。

At the moment the code is reading an environment variable PROJECT_CONFIG that points to the specific config file. This works fine for each developer working on their own machine. The problem arises when we move the project to the server and want to have three instances of the same project: Dev, Stage, Live.

目前,代码正在读取指向特定配置文件的环境变量PROJECT_CONFIG。这适用于在自己的机器上工作的每个开发人员。当我们将项目移动到服务器并希望拥有同一项目的三个实例时,就会出现问题:Dev,Stage,Live。

We now no longer can use the global env var since it needs to be different of each subdomain (project is setup as dev.domain.com, stage.domain.com and www.domain.com).

我们现在不再可以使用全局env var,因为它需要与每个子域不同(项目设置为dev.domain.com,stage.domain.com和www.domain.com)。

I have considered converting the server variable HTTP_HOST into an env var name and using that to set the right config (i.e. when a user requested a page from dev.domain.com, the code would look for an env var called dev_domain_com), but I wanted to see what other people are doing and what they recommend.

我已经考虑将服务器变量HTTP_HOST转换为env var名称并使用它来设置正确的配置(即当用户从dev.domain.com请求页面时,代码将查找名为dev_domain_com的env var),但是我想看看其他人在做什么以及他们推荐什么。

Any advice would be greatly appreciated, thanks in advance

任何建议都将非常感谢,提前谢谢

3 个解决方案

#1


Use apache's SetEnv directive to set your PROJECT_CONFIG in the container configuring access to the application instance:

使用apache的SetEnv指令在容器中设置PROJECT_CONFIG,以配置对应用程序实例的访问:

SetEnv PROJECT_CONFIG /src/dev/app.config.php

#2


David Schmitt's idea is the most elegant. I could see a case for using $_SERVER['HTTP_HOST'] to determine which server you're on and set the path accordingly. One such case could be if you do not have privileges to modify the server's virtual host configuration files. For instance:

大卫施密特的想法是最优雅的。我可以看到使用$ _SERVER ['HTTP_HOST']确定你所在的服务器并相应地设置路径的情况。如果您没有权限修改服务器的虚拟主机配置文件,则可能就是这种情况。例如:

<?php
  switch($_SERVER['HTTP_HOST']){
    case 'dev.domain.com':
      $path = '/home/dev_user/project.config.ini';
      break;
    case 'stage.domain.com':
      $path = '/home/stage_user/project.config.ini';
      break;
    case 'www.domain.com':
      $path = '/home/live_user/project.config.ini';
      break;
    default:
      $path = '';
  }
  if(!defined('PROJECT_CONFIG')){
    define('PROJECT_CONFIG', $path);
  }
  unset($path);
?>

Note: Depending on your server configuration you may want to replace $_SERVER['HTTP_HOST'] with $_SERVER['SERVER_NAME'].

注意:根据您的服务器配置,您可能希望将$ _SERVER ['HTTP_HOST']替换为$ _SERVER ['SERVER_NAME']。

#3


I think your current solution is just fine. The environment settings are there for a reason after all. I've seen your way of doing things used for a large scale proprietary cms I used to work on.

我认为你目前的解决方案很好。毕竟环境设置是有原因的。我已经看到了你曾经用过的大型专有cms做事的方式。

I need to do the same thing with having dev/stage/production sites that load different configurations and plan to use environment settings and have it detect the hostname as a fallback if the env setting isn't set.

我需要做同样的事情,让dev / stage / production站点加载不同的配置并计划使用环境设置,如果没有设置env设置,让它检测主机名作为后备。

#1


Use apache's SetEnv directive to set your PROJECT_CONFIG in the container configuring access to the application instance:

使用apache的SetEnv指令在容器中设置PROJECT_CONFIG,以配置对应用程序实例的访问:

SetEnv PROJECT_CONFIG /src/dev/app.config.php

#2


David Schmitt's idea is the most elegant. I could see a case for using $_SERVER['HTTP_HOST'] to determine which server you're on and set the path accordingly. One such case could be if you do not have privileges to modify the server's virtual host configuration files. For instance:

大卫施密特的想法是最优雅的。我可以看到使用$ _SERVER ['HTTP_HOST']确定你所在的服务器并相应地设置路径的情况。如果您没有权限修改服务器的虚拟主机配置文件,则可能就是这种情况。例如:

<?php
  switch($_SERVER['HTTP_HOST']){
    case 'dev.domain.com':
      $path = '/home/dev_user/project.config.ini';
      break;
    case 'stage.domain.com':
      $path = '/home/stage_user/project.config.ini';
      break;
    case 'www.domain.com':
      $path = '/home/live_user/project.config.ini';
      break;
    default:
      $path = '';
  }
  if(!defined('PROJECT_CONFIG')){
    define('PROJECT_CONFIG', $path);
  }
  unset($path);
?>

Note: Depending on your server configuration you may want to replace $_SERVER['HTTP_HOST'] with $_SERVER['SERVER_NAME'].

注意:根据您的服务器配置,您可能希望将$ _SERVER ['HTTP_HOST']替换为$ _SERVER ['SERVER_NAME']。

#3


I think your current solution is just fine. The environment settings are there for a reason after all. I've seen your way of doing things used for a large scale proprietary cms I used to work on.

我认为你目前的解决方案很好。毕竟环境设置是有原因的。我已经看到了你曾经用过的大型专有cms做事的方式。

I need to do the same thing with having dev/stage/production sites that load different configurations and plan to use environment settings and have it detect the hostname as a fallback if the env setting isn't set.

我需要做同样的事情,让dev / stage / production站点加载不同的配置并计划使用环境设置,如果没有设置env设置,让它检测主机名作为后备。