Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check apache modules on PHP CGI

I have to do a php script that checks the servers configuration. I need to check apache version and the status of 11 modules like mod_actions, mod_alias (...).

Is there a solution to check the status of apache modules when the server mode is CGI or FastCGI? Apache_get_modules() works only if the server's mode is on Apache Handler ...

Thank you

like image 938
Émir C. Avatar asked Aug 12 '26 00:08

Émir C.


1 Answers

You can use the output of the Apache server control interface with the -M flag. Loop over the content, grab the names of the loaded modules and then check if a subset of one or more modules exist in the array.

Depending on your distribution, the command to get the loaded modules may differ (e.g. 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));
}

You can then use that function to check whether one or more modules are loaded:

moduleEnabled('so_module');
moduleEnabled(['so_module', 'watchdog_module']);
like image 187
D1__1 Avatar answered Aug 14 '26 15:08

D1__1