简体   繁体   English

PHP 是否可以读取/解析当前 vhosts 的 Apache VirtualHost 配置块,以检索 ErrorLog 和 CustomLog 设置?

[英]possible for PHP to read/parse the current vhosts' Apache VirtualHost config block, to retrieve ErrorLog and CustomLog settings?

Is it possible for PHP to read/parse the current vhosts' Apache VirtualHost config block, in particular, to retrieve ErrorLog and CustomLog settings? PHP 是否可以读取/解析当前虚拟主机的 Apache VirtualHost 配置块,特别是检索 ErrorLog 和 CustomLog 设置?

To be clear we do not need the PHP error log path, that is easy to retrieve.需要明确的是,我们不需要 PHP 错误日志路径,这很容易检索。

I couldn't find any way in phpinfo or _SERVER/getenv() or PHP Apache functions ( apache_module() , apache_note() , apache_getenv() )我在phpinfo_SERVER/getenv()或 PHP Apache 函数( 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.该代码库用于多个服务器上的多个虚拟主机,因此我们无法硬编码SetEnv (或.htaccess等文件 SetEnv 中的 Apache 访问和错误日志路径),或者它可能并不总是匹配使用<VirtualHost>中设置的内容 - Operations 中的某个人可能会更新 VirtualHost,但开发人员可能不会更新代码或代码用于查找相同路径的任何内容。 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.).我们不能在 CustomLog 行之下或之上有一个具有相同值的 SetEnv,因为仍然有可能在没有另一个的情况下更新一个(人为错误等)。

My best hope was apache_getenv() but I've tried apache_getenv('CustomLog') and it does not return anything.我最大的希望是apache_getenv()但我已经尝试过apache_getenv('CustomLog')并且它没有返回任何东西。

Would be ok to run a series of system()/exec() calls to run cli functions to find them, but not ideal.可以运行一系列system()/exec()调用来运行 cli 函数来查找它们,但并不理想。

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. ServerName可能与当前调用的虚拟主机不匹配,因为ServerAlias可能具有包含正则表达式的不同虚拟主机,因此一旦找到 Apache conf 的路径,手动 grepping 文件并不理想/可靠。

Apache 2.2.16 (Unix) PHP 5.3.3 CentOS release 5.5 (Final) Apache 2.2.16 (Unix) PHP 5.3.3 CentOS 5.5 版(最终版)

Please tell me I've missed something obvious;)请告诉我我错过了一些明显的东西;)

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.在您的 httpd.conf 中,因此您可以使用 apache_getenv() function。 getenv() doesn't retrieve configuration directives, just actual environment variables. getenv() 不检索配置指令,只是实际的环境变量。 So.. put the log path into an environment variable.所以..将日志路径放入环境变量中。

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. Apache 根本不会将此字符串存储在您可以获取的任何地方。 From, mod_log_config.c (note the fmt argument which is stored in a config_log_state struct):mod_log_config.c 开始(注意fmt参数,它存储在config_log_state结构中):

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说服开发人员保持 SetEnv 和 CustomLog 相同
  • Parse the conf files yourself and keep your fingers crossed自己解析 conf 文件并保持手指交叉
  • Modify mod_log_config.c (bad idea!) 修改 mod_log_config.c (坏主意!)
  • Forget the whole thing;忘记整件事; ;) ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM