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

时间:2021-03-21 23:24:42

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".

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

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

这是我对大多数基于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;
}

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).

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

3 个解决方案

#1


4  

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.

我个人不提交设置。将php放入版本控制(settings.default.php is),然后保持一个自定义设置。php文件在每个环境中基于settings.default.php。

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

但是,如果您喜欢以这种方式设置您的环境,那么类似的东西将在您的站点/默认/设置中工作。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;
}

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

记住,对于这里使用的每个vars,如果在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.

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

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.

使用Drupal的多站点功能来管理需要开发和开发的每个环境,就像其他人在这里建议的那样,这是非常疯狂的。

#2


1  

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.

在Drupal中,您所描述的是多站点配置。简单地说,在多站点配置中,您将Drupal设置为基于用于访问站点的域名使用不同的配置文件。它允许对指向相同服务器的不同域名使用相同的代码,就像您希望使用不同的域名访问相同的web服务器一样。

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

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

#3


0  

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.

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

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.

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

#1


4  

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.

我个人不提交设置。将php放入版本控制(settings.default.php is),然后保持一个自定义设置。php文件在每个环境中基于settings.default.php。

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

但是,如果您喜欢以这种方式设置您的环境,那么类似的东西将在您的站点/默认/设置中工作。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;
}

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

记住,对于这里使用的每个vars,如果在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.

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

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.

使用Drupal的多站点功能来管理需要开发和开发的每个环境,就像其他人在这里建议的那样,这是非常疯狂的。

#2


1  

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.

在Drupal中,您所描述的是多站点配置。简单地说,在多站点配置中,您将Drupal设置为基于用于访问站点的域名使用不同的配置文件。它允许对指向相同服务器的不同域名使用相同的代码,就像您希望使用不同的域名访问相同的web服务器一样。

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

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

#3


0  

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.

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

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.

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