Probably, each developer met with the task of running a single project on different computers. In the simple case, this is a developer's machine and a “combat” server. The situation is even more complicated if several people are involved in the development, and each has its own settings.
The difficulty here is that a different environment (access to the database, location of files, etc.), being “hard” written in the configuration file, forces us to constantly edit this file. In addition, the configuration files are often uploaded to the repository, and after the next code update, everything stops working for you.
To solve the problem, I used two methods.
The first - less successful, I think - is to bind the configuration file, or part of it, to the name of the host on which the project is launched.
')
My XML config looked like this (the fact that it is XML is not important - the essence is the same for any type of configuration).
<?xml version="1.0" encoding="UTF-8"?>
<root>
<!-- You can set just ip or host without "www." -->
<server ip="127.0.0.1" host="localhost">
<!-- Database params -->
<param name="db_host">dm9</param>
<param name="db_port">3307</param>
<param name="db_user">root</param>
<param name="db_pass">123</param>
<param name="db_name">dm9_idsrv</param>
…
</server>
<server ip="12.34.56.78" host="my-production-host.ru">
…
The disadvantages of this method were 2:
- different developers (with different environment parameters) may have the same host - say, localhost;
- in addition, when changing or adding a host, you have to edit the file.
After that, I began to use another method - the environment variable, which indicates on which host we are currently working.
Setting the environment variable is simple. For Apache, it is enough to add the string 'setEnv DEV_HOST DM9' to httpd.conf to create the environment variable DEV_HOST, the value of which is DM9 (the name or nickname of the developer). The environment variable can also be set separately for each virtual host. (By the way, you can tell how this is done under ngix, I will add.)
I prefer to set environment variables only for machines that are being developed. The absence of such a variable means that we are on a “combat” server.
Now my configuration file (for a PHP project) looks like this:
if (isset($_SERVER["DEV_HOST"]) AND $_SERVER["DEV_HOST"] === "DVS") //
{
$config["db_host"] = "localhost";
$config["db_user"] = "root";
$config["db_pass"] = "";
$config["db_name"] = "xtr_ru";
}
elseif (isset($_SERVER["DEV_HOST"]) AND $_SERVER["DEV_HOST"] === "DM9") //
{
$config["db_host"] = "localhost";
$config["db_user"] = "root";
$config["db_pass"] = "123";
$config["db_name"] = "xtr";
}
else //
{
$config["db_host"] = "localhost";
$config["db_user"] = "xtr";
$config["db_pass"] = "some_password";
$config["db_name"] = "xtr";
}
An experienced reader will notice that there is one catch in all this: what about running the scripts from the console? This is an important task - for example, scripts need to be able to run the scheduler.
There are also two options - as they say, an amateur.
The first is to run the scheduler script with a parameter equal to the value of the host (for the first method described above) or your environment variable (for the second method). If you are attached to a host name, only this method will work.
Here is an example of running a script, to which input is the host name (the contents of the bat-file, which I run the script from under Windows):
#clean_something.bat
cd "E:\myproject\utils"
php clean_something.php localhost
pause
(If you use PHP, the parameters passed to the script can be obtained via $ _SERVER ["
argc "] and $ _SERVER ["
argv "].)
The second option is to set the global environment variable in the operating system. In this case, it does not have to register in the configuration file of your web server. Under Windows, this is done through the properties of “My Computer” (hereinafter referred to as the Advanced tab, the Environment Variables button, then the New Variables section in the System Variables section). Translation into Russian may be inaccurate - please correct. After that, you will most likely have to reboot. Under the NIX platform, this is made a little more complicated, and this is beyond the scope of this article. However, if you are developing for Windows, and only releases on NIX, you will not need it.
Yes, why am I writing these obvious lines for many? It's just that I still too often have to work with projects that use one configuration file that is not customizable for the environment. Make configs that do not need to be edited often. You are welcome :)