设置测试和生产环境

发布于 2024-07-24 22:03:37 字数 174 浏览 5 评论 0原文

我正在 CakePHP 中开发一个系统,使用 Git 作为版本控制系统。 我的测试服务器中有一个工作副本,生产服务器中有另一个工作副本,两者都具有不同的数据库。 每次进行更改时,我都必须更改数据库配置,以便测试系统。 是否有另一种方法来保留两个具有不同内容的文件,一个在测试中,另一个在生产服务器中? 分支机构是一个好方法吗?

I'm developing a system in CakePHP, using Git as the version control system. I have a working copy in my testing server and another in my production server, both with different databases. Everytime I make changes, I have to change the database configuration so I can test the system. Is there another way to keep two files, with different contents, one in the test and another in the production server? Branches are a good way to go?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

好倦 2024-07-31 22:03:38

你不会想用分支来做到这一点。 我不能具体谈论 PHP,但不是将两个文件(测试配置和生产配置)保存在单独的分支中,而是将它们放在一起,但只是让环境变量确定哪个配置是正确的使用在运行时。

(这就是 Rails 中的实现方式,而且效果很好。)

You wouldn't want to do that with branches. I can't speak to PHP specifically, but rather than keep the two files (test config & production config) in separate branches, you'd keep them together, but just let an environment variable determine which config is the correct one to use at runtime.

(This is how it's done in Rails and it works well.)

も星光 2024-07-31 22:03:38

我采用了一种相当粗糙但有效的技术:在我的开发环境中,我有一个名为“environment_development”的空文件。 在我的生产环境中,我有一个名为“environment_Production”的环境(不同情况以增加视觉强调)。 我的 gitignore 文件设置为忽略这两者。

我的应用程序的前端控制器(我使用 Kohana 框架,但我假设 CakePHP 有类似的东西)检查这些文件是否存在并适当地设置 IN_Production 常量。 代码的其余部分(数据库配置、错误处理等)可以检查该常量的值并根据需要更改行为。

我曾经使用 $_SERVER['SERVER_NAME'] 检查,但此方法具有以下优点:

  1. 即使您想从命令行运行应用程序的某些部分(例如,当 $_SERVER 为 cronjobs 时),它也可以工作。没有设置。
  2. 即使您的应用程序在多个域上运行,它也能正常工作。
  3. 这是完全明确的:如果有人检查应用程序的另一个工作副本,则在创建环境文件之前什么都不会起作用,因此没有(或很少)有人针对生产数据库运行开发代码的机会,反之亦然。

I've gone for a rather crude but effective technique: In my development environment I have an empty file called 'environment_development'. In my production environment I have one called 'environment_PRODUCTION' (different case for added visual emphasis). My gitignore file is set to ignore both of these.

The front controller of my application (I use the Kohana framework, but I'm presuming CakePHP has something similar) checks for the presence of these files and sets an IN_PRODUCTION constant appropriately. The rest of the code (database configuration, error handling etc.) can check the value of this constant and alter behaviour as required.

I used to use the $_SERVER['SERVER_NAME'] check, but this method has the following advantages:

  1. It works even if there are parts of your app you want to run from the command line, e.g., as cronjobs, when $_SERVER is not set.
  2. It works even if your app runs on multiple domains.
  3. It is entirely explicit: if someone checks out another working copy of the app, nothing will function until they create an environment file, so there's no (or very little) chance of someone running development code against the production db or vice versa.
幼儿园老大 2024-07-31 22:03:38

如果数据库依赖环境,你可以在database.php文件中做这样的事情:

class DATABASE_CONFIG {

    var $default = NULL;

    var $prod = array(
        'driver' => 'mysql',
        'persistent' => false,
        'host' => 'localhost',
        'login' => 'username',
        'password' => 'password',
        'database' => 'productionDatabaseName',
        'prefix' => '',
    );

    var $staging = array(
        'driver' => 'mysql',
        'persistent' => false,
        'host' => 'localhost',
        'login' => 'username',
        'password' => 'password',
        'database' => 'stagingDatabaseName',
        'prefix' => '',
    );

    //var $dev = ...

    // Chooses production or staging depending on URL
    function __construct ()
    {
        if(isset($_SERVER['SERVER_NAME']))
        {
            switch($_SERVER['SERVER_NAME'])
            {
                case 'myhostname.com':
                case 'www.myhostname.com':
                    $this->default = $this->prod;
                    break;
                case 'staging.myhostname.com':
                    $this->default = $this->staging;
                    break;
                default:
                    $this->default = $this->dev;
            }
        }
        else // Use local for any other purpose
        {
            $this->default = $this->dev;
        }
    }
}

If the database is dependent on the environment, you can do something like this in the database.php file:

class DATABASE_CONFIG {

    var $default = NULL;

    var $prod = array(
        'driver' => 'mysql',
        'persistent' => false,
        'host' => 'localhost',
        'login' => 'username',
        'password' => 'password',
        'database' => 'productionDatabaseName',
        'prefix' => '',
    );

    var $staging = array(
        'driver' => 'mysql',
        'persistent' => false,
        'host' => 'localhost',
        'login' => 'username',
        'password' => 'password',
        'database' => 'stagingDatabaseName',
        'prefix' => '',
    );

    //var $dev = ...

    // Chooses production or staging depending on URL
    function __construct ()
    {
        if(isset($_SERVER['SERVER_NAME']))
        {
            switch($_SERVER['SERVER_NAME'])
            {
                case 'myhostname.com':
                case 'www.myhostname.com':
                    $this->default = $this->prod;
                    break;
                case 'staging.myhostname.com':
                    $this->default = $this->staging;
                    break;
                default:
                    $this->default = $this->dev;
            }
        }
        else // Use local for any other purpose
        {
            $this->default = $this->dev;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文