Solution to multi server environment with a CodeIgniter website
I have a local, staging and production environment for my CodeIgniter based site. Increasingly I find everytime I deploy a versi开发者_JS百科on I have more and more little bits of code to change because of server variations.
What would be a good (and quick) solution I could add that would allow me to set these variables by just using one setting. Where would be the best place to insert this in the index.php, some sort of hook?
If you're using Apache, you can set an environmental variable which can be read by PHP in your Virtual Hosts file for the site:
<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>
So with that, you now can check for and set the server environment accordingly in your index.php file:
// 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);
With this setup, you now have the freedom to deploy your sites and add additional configuration parameters to your application/config/[file].php
files for each environment.
Alternative...
Another possibility for handling multi-environment setups is to create a file outside of the document root and is ignored by your version control system (i.e. .gitignore) which contains the value of the server environment. You could then just read that file via file_get_contents()
or equivalent.
define a "LIVE" constant which is TRUE or FALSE based on the current domain its on (put this in your index.php file)
if(strpos($_SERVER['HTTP_HOST'], 'mylivesite.com'))
{
define('LIVE', TRUE);
}
else
{
define('LIVE', FALSE);
}
and then check to see if you are live or not and assign the variables accordingly
if(LIVE)
{
$active_group = "production";
}
else
{
$active_group = "test";
}
ive been doing this with our 5 environment setup for the past year with no problems
An old question, but Codeigniter Reactor has support for environment variables built in now. You simply open up the index.php file and choose your environment. There is a post about them here: http://ilikekillnerds.com/2011/03/how-to-use-codeigniter-reactor-environment-variables/
The official CodeIgniter doc suggests this for multiple database environment:
Gobal variable in config.php file to set the environment:
$active_group = "test";
Multiple settings in 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";
...
See the doc for further details.
Having many little bits of code that change because of server variations is a bad sign that you're modifying code that's not supposed to be modified for the application. The only things that you're supposed to change between servers is your configuration variables located in config.php
, which should have default values for the developers to change depending on the environment.
精彩评论