Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 261
Andrey Voev Avatar asked Apr 16 '11 16:04

Andrey Voev


People also ask

What is a production environment in web development?

Simply put, a production environment is where the latest versions of software, products, or updates are pushed live to the intended users. Think of it as a final phase of production. This is the environment where the end user can see, experience, and interact with the new product.

What are the different environments in software development?

A Development Environment; A Beta Environment; and. A Production Environment.

How do you deploy multiple environments?

Create a repo and push your code to Gitlab and then we are ready to work on the deployment process. You create a separate branch for every environment. Under Project's Settings > CI/CD > Variables. Upload the sensitive data by encoding it and saving the private key too in the variable.


1 Answers

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.

like image 175
Coder1 Avatar answered Oct 10 '22 00:10

Coder1