根据URL设置各种环境(开发、生产)

发布于 2024-11-01 09:32:09 字数 762 浏览 0 评论 0原文

我正在尝试根据 URL 在 Drupal 中设置环境。 例如,如果我转到 mysite.local,它将使用 localdb 并将站点名称更改为“Local Mysite”;如果我访问 mysite.com,它将自动切换为使用 Productiondb 并将名称设置为“Mysite”。

这是我用于大多数基于 MVC 的框架的类似设置:

define('DEVELOPMENT', 'mysite.local');
define('PRODUCTION', 'mysite.com');

switch ($_SERVER['SERVER_NAME']) {
  case DEVELOPMENT:
    // development server
    $config['base_url']    = "http://mysite.local";
    $config['title']    = "DEVELOPMENT Mysite";
    $config['debug']    = 1;
    break;

  default:
    // live server
    $config['base_url']    = "http://mysite.com/";
    $config['title']    = "Mysite";
    $config['debug']    = 0;
    break;
}

Drupal7 中是否已经有类似的设置(我不想使用不同的站点,只想为同一站点使用不同的设置),并且是否存在某种约定需要进行切换(我目前正在考虑 settings.php)。

I am trying to setup environments in Drupal based on the URL.
For example, if I go to mysite.local, it will use localdb and it will change the name of the site to "Local Mysite"; if I go to mysite.com, it will switch automatically to use productiondb and set the name to "Mysite".

This is a similar setup I use for most MVC based frameworks:

define('DEVELOPMENT', 'mysite.local');
define('PRODUCTION', 'mysite.com');

switch ($_SERVER['SERVER_NAME']) {
  case DEVELOPMENT:
    // development server
    $config['base_url']    = "http://mysite.local";
    $config['title']    = "DEVELOPMENT Mysite";
    $config['debug']    = 1;
    break;

  default:
    // live server
    $config['base_url']    = "http://mysite.com/";
    $config['title']    = "Mysite";
    $config['debug']    = 0;
    break;
}

Is there something like that in Drupal7 already (I don't want to use different sites, only different settings for the same site), and is there some sort of convention where this switch needs to happen (I am currently thinking about settings.php).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

め可乐爱微笑 2024-11-08 09:32:09

就我个人而言,我不会将settings.php提交到版本控制中(settings.default.php是),然后只在每个环境中基于settings.default.php保留一个自定义settings.php文件。

但是,如果您更喜欢以这种方式设置环境,那么类似的内容可以在您的sites/default/settings.php 文件中使用。

define('DEVELOPMENT', 'mysite.local');
define('PRODUCTION', 'mysite.com');

switch ($_SERVER['SERVER_NAME']) {
  case DEVELOPMENT:
    // development server
    $db_url = 'mysql://user:pass@localhost/mydb_dev';
    $db_prefix = '';
    $base_url = 'http://mysite.local';
    $conf = array(
     'site_name' => 'Development Environment',
    );
    break;

  default:
    // live server
    $db_url = 'mysql://user:pass@localhost/mydb_prod';
    $db_prefix = '';
    $base_url = 'http://mysite.com';
    $conf = array(
     'site_name' => 'My Site',
    );
    break;
}

请记住,对于您在此处使用的每个变量,如果它们是在 settings.php 的其他部分中定义的,则需要注释掉它们。

我还应该补充一点,我认为用于开发环境的多站点是一个坏主意,我想出于同样的原因,我更喜欢每个环境都有自己的自定义 settings.php 文件。在大多数情况下,我更喜欢保持代码可移植,因为除了我运行代码的环境的设置文件之外,我不需要对任何环境的任何代码或文件系统引用。

正如其他人在这里建议的那样,为您需要开发和部署的每个环境使用 Drupal 的多站点功能,管理起来会很疯狂。

Personally I don't commit settings.php into version control, (settings.default.php is), then just keep a custom settings.php file based off settings.default.php in each environment.

However, if you prefer to setup your environments that way, then something like this would work in your sites/default/settings.php file.

define('DEVELOPMENT', 'mysite.local');
define('PRODUCTION', 'mysite.com');

switch ($_SERVER['SERVER_NAME']) {
  case DEVELOPMENT:
    // development server
    $db_url = 'mysql://user:pass@localhost/mydb_dev';
    $db_prefix = '';
    $base_url = 'http://mysite.local';
    $conf = array(
     'site_name' => 'Development Environment',
    );
    break;

  default:
    // live server
    $db_url = 'mysql://user:pass@localhost/mydb_prod';
    $db_prefix = '';
    $base_url = 'http://mysite.com';
    $conf = array(
     'site_name' => 'My Site',
    );
    break;
}

Remember, for each of these vars you use here, you need to comment out if they are defined in other parts of settings.php.

I should also add that I think multi-site for the purpose of a development environment is a bad idea, I guess for the same reason I prefer each environment having it's own custom settings.php file. In most cases, I prefer to keep the code portable in that I do not need any code or file system references to any environment except in a settings file for the environment I'm running the code on.

Using Drupal's multi-site functionality for every environment you need to develop and stage on, as others are suggesting here, would be insane to manage.

缱绻入梦 2024-11-08 09:32:09

您所描述的内容在 Drupal 中称为多站点配置
简而言之,在多站点配置中,您将 Drupal 设置为根据用于访问站点的域名使用不同的配置文件。它允许对所有指向同一服务器的不同域名使用相同的代码,就像您希望使用不同域名访问同一 Web 服务器的情况一样。

您还对 从同一代码库运行多个站点(多站点)中报告的内容感兴趣网站)

What you are describing is called multi-site configuration, in Drupal.
Said with few words, in a multi-site configuration, you are setting Drupal to use different configuration files basing on the domain name used to access the site. It allows to use the same code for different domain names that point all to the same server, as in your case, where you want to access the same web server using different domain names.

You are also interested in what reported in Run multiple sites from the same code base (multi-site).

小耗子 2024-11-08 09:32:09

我认为使用单独的网站正是您想要的。我有类似的设置,不同的站点都有自己的 settings.php 文件。但后来我对模块和主题目录进行了符号链接,这样我就没有重复的文件。但如果您正在设置一种开发环境,您将希望将它们分开。

所以我有这样的想法:

drupal/sites/mysite.com/themes
drupal/sites/mysite.com/modules
drupal/sites/mysite.com/settings.php

drupal/sites/mysite.local/themes => ../../mysite.com/themes
drupal/sites/mysite.local/modules => ../../mysite.com/modules
drupal/sites/mysite.local/settings.php

drupal/sites/default => mysite.com

Drupal 将根据 URL 使用正确的站点配置。

I think that using separate sites is what you want. I have a similar setup, and the different sites each have their own settings.php file. But then I symlinked the modules and themes directory so that I don't have duplicate files. But if you are setting up one environment for development, you will want to keep them separate.

So I have something like this:

drupal/sites/mysite.com/themes
drupal/sites/mysite.com/modules
drupal/sites/mysite.com/settings.php

drupal/sites/mysite.local/themes => ../../mysite.com/themes
drupal/sites/mysite.local/modules => ../../mysite.com/modules
drupal/sites/mysite.local/settings.php

drupal/sites/default => mysite.com

Drupal will use the correct site configuration based on the URL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文