PHP 可以读取/解析当前的虚拟主机' Apache VirtualHost 配置块,用于检索 ErrorLog 和 CustomLog 设置?

发布于 2024-11-18 17:23:16 字数 987 浏览 3 评论 0原文

PHP 是否可以读取/解析当前虚拟主机的 Apache VirtualHost 配置块,特别是检索 ErrorLog 和 CustomLog 设置?

需要明确的是,我们不需要 PHP 错误日志路径,因为它很容易检索。

我在 phpinfo_SERVER/getenv() 或 PHP Apache 函数 (apache_module(), apache_note( ), apache_getenv())

代码库用于多个服务器上的多个虚拟主机,因此我们无法在 PHP 中(或在 中硬编码 Apache 访问和错误日​​志路径) .htaccess SetEnv 或某些 ini/csv/任何文件等),因为它可能并不总是与 中设置的内容匹配 - 操作人员可能会更新VirtualHost,但开发人员可能不会更新代码或代码用于查找相同路径的任何内容。我们不能在 CustomLog 行的下方或上方设置具有相同值的 SetEnv,因为仍然有可能在没有另一个的情况下更新一个 SetEnv(人为错误等)。

我最大的希望是 apache_getenv() ,但我尝试过 apache_getenv('CustomLog') ,但它没有返回任何内容。

运行一系列 system()/exec() 调用来运行 cli 函数来查找它们是可以的,但并不理想。

ServerName 可能与当前调用的虚拟主机不匹配,因为 ServerAlias 可能有一个包含正则表达式的不同虚拟主机,因此一旦找到 Apache conf 的路径,请手动 grep该文件不理想/不可靠。

阿帕奇 2.2.16 (Unix) PHP 5.3.3 CentOS 版本 5.5(最终版)

请告诉我我错过了一些明显的东西;)

Is it possible for PHP to read/parse the current vhosts' Apache VirtualHost config block, in particular, to retrieve ErrorLog and CustomLog settings?

To be clear we do not need the PHP error log path, that is easy to retrieve.

I couldn't find any way in phpinfo or _SERVER/getenv() or PHP Apache functions (apache_module(), apache_note(), apache_getenv())

The codebase is used for multiple virtual hosts on multiple servers so we can't hard code the Apache access and error log paths in PHP (or in .htaccess SetEnv or some ini/csv/whatever file etc), as it may not always match up with what has been set in <VirtualHost> - someone in Operations may update the VirtualHost but a developer may not update the code or whatever the code is using to find the same path. We cannot have a SetEnv under or above the line of CustomLog with the same value, as it's still possible one will be updated without the other (human error etc.).

My best hope was apache_getenv() but I've tried apache_getenv('CustomLog') and it does not return anything.

Would be ok to run a series of system()/exec() calls to run cli functions to find them, but not ideal.

The ServerName may not match the current vhost being called, as ServerAlias may have a different virtualhost that contains regex, so once the path to Apache conf has been found, manually grepping through the file is not ideal/reliable.

Apache 2.2.16 (Unix)
PHP 5.3.3
CentOS release 5.5 (Final)

Please tell me I've missed something obvious ;)

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

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

发布评论

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

评论(2

_畞蕅 2024-11-25 17:23:16

这有点 hackish,但你可以放入

ErrorLog /path/to/logfile
SetEnv ErrorLog /path/to/logfile

httpd.conf,这样你就可以使用 apache_getenv() 函数。 getenv() 不检索配置指令,只检索实际的环境变量。所以..将日志路径放入环境变量中。

It's kinda hackish, but you could put

ErrorLog /path/to/logfile
SetEnv ErrorLog /path/to/logfile

in your httpd.conf, so you could use the apache_getenv() function. getenv() doesn't retrieve configuration directives, just actual environment variables. So.. put the log path into an environment variable.

无言温柔 2024-11-25 17:23:16

请告诉我我错过了什么
明显;)

不,你没有。 Apache 根本不会将此字符串存储在您可以访问它的任何地方。来自mod_log_config.c (请注意存储在 config_log_state 结构中的 fmt 参数):

static const char *add_custom_log(cmd_parms *cmd, void *dummy, const char *fn,
                                  const char *fmt, const char *envclause)
{
    const char *err_string = NULL;
    multi_log_state *mls = ap_get_module_config(cmd->server->module_config,
                                                &log_config_module);
    config_log_state *cls;

    cls = (config_log_state *) apr_array_push(mls->config_logs);
    cls->condition_var = NULL;
    if (envclause != NULL) {
        if (strncasecmp(envclause, "env=", 4) != 0) {
            return "error in condition clause";
        }
        if ((envclause[4] == '\0')
            || ((envclause[4] == '!') && (envclause[5] == '\0'))) {
            return "missing environment variable name";
        }
        cls->condition_var = apr_pstrdup(cmd->pool, &envclause[4]);
    }

    cls->fname = fn;
    cls->format_string = fmt;
    if (fmt == NULL) {
        cls->format = NULL;
    }
    else {
        cls->format = parse_log_string(cmd->pool, fmt, &err_string);
    }
    cls->log_writer = NULL;

    return err_string;
}

您必须(无论您之前是否声明过)你不能):

  • 说服开发人员保持 SetEnv 和 CustomLog 相同
  • 自己解析conf文件并祈祷
  • 修改mod_log_config.c(坏主意!)
  • 忘记整个事情! ;)

Please tell me I've missed something
obvious ;)

No, you haven't. Apache simply does not store this string anyplace where you can get at it. From, mod_log_config.c (note the fmt argument which is stored in a config_log_state struct):

static const char *add_custom_log(cmd_parms *cmd, void *dummy, const char *fn,
                                  const char *fmt, const char *envclause)
{
    const char *err_string = NULL;
    multi_log_state *mls = ap_get_module_config(cmd->server->module_config,
                                                &log_config_module);
    config_log_state *cls;

    cls = (config_log_state *) apr_array_push(mls->config_logs);
    cls->condition_var = NULL;
    if (envclause != NULL) {
        if (strncasecmp(envclause, "env=", 4) != 0) {
            return "error in condition clause";
        }
        if ((envclause[4] == '\0')
            || ((envclause[4] == '!') && (envclause[5] == '\0'))) {
            return "missing environment variable name";
        }
        cls->condition_var = apr_pstrdup(cmd->pool, &envclause[4]);
    }

    cls->fname = fn;
    cls->format_string = fmt;
    if (fmt == NULL) {
        cls->format = NULL;
    }
    else {
        cls->format = parse_log_string(cmd->pool, fmt, &err_string);
    }
    cls->log_writer = NULL;

    return err_string;
}

You must either (regardless if you've previously stated that you cannot):

  • Convince the developers to keep SetEnv and CustomLog identical
  • Parse the conf files yourself and keep your fingers crossed
  • Modify mod_log_config.c (bad idea!)
  • Forget the whole thing! ;)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文