如何检查 PHP CGI 上的 apache 模块

Émi*_* C. 7 php apache cgi server-configuration module

我必须编写一个 php 脚本来检查服务器配置。我需要检查 apache 版本和 11 个模块的状态,如 mod_actions、mod_alias (...)。

当服务器模式为CGI或FastCGI时,是否有解决方案来检查apache模块的状态?Apache_get_modules() 仅当服务器模式位于 Apache Handler 上时才有效...

谢谢

D1_*_*__1 3

您可以使用带有该标志的 Apache 服务器控制接口的输出-M。循环内容,获取已加载模块的名称,然后检查数组中是否存在一个或多个模块的子集。

根据您的发行版,获取加载模块的命令可能会有所不同(例如httpd -M)。

function moduleEnabled(string|array $modules)
{
    $modules = is_array($modules) ? $modules : [$modules];

    // Most of the names that we get from this are a mess, normalize them:
    // e.g. " core_module (static)" becomes "core_module"
    //
    // We default the non-module/empty lines to "" using ??.
    $loadedModules = array_map(function ($module) {
        return explode(" ", trim($module))[0] ?? "";
    }, explode("\n", shell_exec("apache2ctl -M")));

    return (count(array_intersect($loadedModules, $modules)) === count($modules));
}
Run Code Online (Sandbox Code Playgroud)

然后,您可以使用该函数来检查是否加载了一个或多个模块:

moduleEnabled('so_module');
moduleEnabled(['so_module', 'watchdog_module']);
Run Code Online (Sandbox Code Playgroud)