Multi-Server Environment Solution with CodeIgniter Website

I have a local, intermediate, and production environment for my CodeIgniter based site. Increasingly, I find every time I deploy the version, I have more and more small codes to change due to server options.

Which would be a good (and quick) solution that I could add that would allow me to set these variables just by using one parameter. Where would it be best to insert this into index.php, some kind of hook?

+3
source share
5 answers

define a constant "LIVE" that has the value TRUE or FALSE based on the current domain (put this in the index.php file)

if(strpos($_SERVER['HTTP_HOST'], 'mylivesite.com'))
{
    define('LIVE', TRUE);
}
else
{
    define('LIVE', FALSE);
}

, ,

if(LIVE)
{
    $active_group = "production";
}
else
{
    $active_group = "test";
}

ive 5

+2

Apache, , PHP :

<VirtualHost *:80>
    DocumentRoot /path/to/site
    ServerName local.mysite.com
    ErrorLog /path/to/error_log
    CustomLog /path/to/access_log common
    <Directory /path/to/site>
        SetEnv ENVIRONMENT local
        RewriteEngine On
        Options FollowSymLinks Indexes
        AllowOverride AuthConfig Options FileInfo
    </Directory>
</VirtualHost>

, index.php:

// always default to production for safety
$environment = 'production';

// check for an environment override
if (function_exists('apache_getenv') && apache_getenv("ENVIRONMENT")) {
  $environment = apache_getenv("ENVIRONMENT");
} else if (getenv("ENVIRONMENT")) {
  $environment = getenv("ENVIRONMENT");
}

// set the environment constant
define('ENVIRONMENT', $environment);

application/config/[file].php .

Alternative...

multi-environment - (.. .gitignore), . file_get_contents() .

+6

CodeIgniter doc :

Gobal config.php :

$active_group = "test";

database.php

$db['test']['hostname'] = "localhost";
$db['test']['username'] = "root";
$db['test']['password'] = "";
$db['test']['database'] = "database_name";
...

$db['production']['hostname'] = "example.com";
$db['production']['username'] = "root";
$db['production']['password'] = "";
$db['production']['database'] = "database_name";
...

. .

+1

, - , , , . , , , config.php, .

+1

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


All Articles