开发者

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?

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 ab开发者_Go百科ove 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 ;)


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.


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! ;)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜