开发者

Setting various environments (development, production) based on the URL

开发者 https://www.devze.com 2023-02-26 06:23 出处:网络
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.c

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 w开发者_开发问答ill 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).


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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号