Setting up different environments (development, production) based on the URL

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

This is a similar setup that 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 similar in Drupal7 already (I don’t want to use different sites, only different settings for the same site), and is there some kind of agreement in which this switch should happen (I'm thinking about settings.php now )

+6
source share
3 answers

Personally, I don't commit settings.php in version control (settings.default.php is), and then just save my own settings.php file based on settings.default.php in each environment.

However, if you prefer to set up your environment this way, then something like this will 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 that for each of these vars that you use here, you need to comment on whether they are defined in other parts of settings.php.

I should also add that I believe that a multi-site site with the goal of creating an environment is a bad idea, for the same reason I prefer that each environment has its own settings.php file. In most cases, I prefer to keep portable code in that I don’t need links to any code or file system for any environment, except in the settings file for the environment in which I run the code.

Using Drupal's multisite functionality for each environment that needs to be developed and implemented, as others suggest, will be insane to manage.

+4
source

What you are describing is called a multisite configuration , in Drupal.
A few words, in a multi-site configuration, you install Drupal to use different configuration files based on the domain name used to access the site. It allows you to use the same code for different domain names that point to the same server, as in your case, when you want to access the same web server using different domain names.

You are also interested in what is reported in Run multiple sites from the same code base (multisite) .

+1
source

I think that using separate sites is what you want. I have a similar setup, and different sites have their own settings.php file. But then I linked the module directory so that I did not have duplicate files. But if you are creating one development environment, you will want to keep it 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 URL-based site configuration.

0
source

Source: https://habr.com/ru/post/886052/


All Articles