tohokuaikiのチラシの裏

技術的ネタとか。

httpd.confからDocumentRootを取るPHPスクリプト

サーバー名が$_SERVER['SERVER_NAME']で分かっていること前提。

<?php
$conf_file = "/etc/httpd/conf.d/virtual.conf";
$httpd_conf = array();
$server_name = "";
foreach (file($conf_file) as $line){
    $line = trim($line);
    $cell = explode(" ", $line);
    if (count($cell) == 2){
        if (strcasecmp($cell[0], 'ServerName') === 0){
            $server_name = trim($cell[1], '"');
            $httpd_conf[$server_name] = array();
        }
        if ($server_name){
            $httpd_conf[$server_name][strtolower($cell[0])] = trim($cell[1], '"');
        }
    }
}
if (isset($httpd_conf[$_SERVER['SERVER_NAME']]['documentroot'])){
    $_SERVER['DOCUMENT_ROOT'] = $httpd_conf[$_SERVER['SERVER_NAME']]['documentroot'];
}

CLIで困ったので。