Generally, we have a development environment (Dev), a test environment, and a production environment (prod ).
How can I automatically switch the corresponding configuration file according to different environments?
This document uses PHP as an example.
The simplest way is to ensure that the servers in the three environments can provide identical settings to the program, such as the database address, port, database name, username, and password. However, it is not feasible in an environment where resources are insufficient.
First, three corresponding configuration files are required.
For example:
Config. Dev. php
Config. Test. php
Config. Prod. php/config. php
Then there is the problem of automatic switching.
In fact, under normal circumstances, the operating environments of these three may be the same and cannot be distinguished simply by different environments. Unless the geeks, such as modifying PHP code, add different environment variable labels to manually compile PHP, so that the runtime environment has differentiated labels and then encapsulates a judgment method. In the case of resource shortage, the operability is also not strong.
The remaining feasible method is soft file connection. * Both nix and windows can create soft file connections. No matter how many configuration files are available in the system, only one file is required during running. Therefore, if the code of the corresponding program is not significantly changed, this is the solution:
Config. Dev. php
Config. Test. php
Config. Prod. php
Then, manually create a soft connection config. php In each environment and point to the corresponding file config. XXX. php.
Like a VM, you can directly change config. Prod. php to config. php on the server.
PS
There is also an environment for complex and complex resources to be called in large applications. A resource management and scheduling system will be dedicated to naming each resource, such as the MySQL service number, then, in the application environment, call the unified SDK to obtain configuration resources. In this way, we still need to ensure that the test environment and the formal environment cannot be on one machine when the resources are sufficient. In this way, we can judge based on the IP Address requested by the application. If the server is a test server, I will return a configuration data for testing. If this server is a formal environment, the formal configuration will be returned.
Environment configuration file layout Solution