3

Given a path X or a copy of the apache vhosts configuration file, how would I parse that file using PHP?

E.g. Given a variable containing a string that has the contents of an Apache vhosts configuration, how would I get a list of hosted domains/subdomains alias?

For example, given:

#
# Use name-based virtual hosting.
#
NameVirtualHost *:80



<VirtualHost *:80>
    ServerAdmin contact@tomjn.com
    DocumentRoot "/srv/www/localhost/

    ServerName 127.0.0.1
    ServerAlias localhost
    CustomLog "/srv/www/logs/localhost-access_log.log" combined
    ErrorLog "/srv/www/logs/localhost-error_log.log"

    <Directory "/srv/www/localhost">
        Options Indexes FollowSymLinks Includes ExecCGI
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin contact@tomjn.com
    DocumentRoot "/srv/www/2.7.localhost.com/

    ServerName 2.7.localhost.com
    ServerAlias 2.7.localhost.com
    CustomLog "/srv/www/logs/2.7.localhost.com-access_log.log" combined
    ErrorLog "/srv/www/logs/2.7.localhost.com-error_log.log"

    <Directory "/srv/www/2.7.localhost.com">
        Options Indexes FollowSymLinks Includes ExecCGI
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

How would I get this output:

  • localhost
  • 2.7.localhost.com

Here's something very close written in Python:

http://www.poldylicious.de/system/files/apacheconfig.py.txt

4

4 回答 4

6
# Get Vhosts files
$path       = '/etc/apache2/sites-enabled'; # change to suit your needs
$a_directory = scandir($path);
$a_conf_files = array_diff($a_directory, array('..', '.'));
$info = array(); $x=0;

foreach($a_conf_files as $conf_file){
 $Thisfile   = fopen($path .'/'.$conf_file, 'r')or die('No open ups..');

    while(!feof($Thisfile)){
        $line = fgets($Thisfile);
        $line = trim($line);

       // CHECK IF STRING BEGINS WITH ServerAlias
        $tokens = explode(' ',$line);

        if(!empty($tokens)){
            if(strtolower($tokens[0]) == 'servername'){
                $info[$x]['ServerName'] = $tokens[1];
            }
            if(strtolower($tokens[0]) == 'documentroot'){
                $info[$x]['DocumentRoot'] = $tokens[1];
            }
            if(strtolower($tokens[0]) == 'errorlog'){
                $info[$x]['ErrorLog'] = $tokens[1];
            }
            if(strtolower($tokens[0]) == 'serveralias'){
                $info[$x]['ServerAlias'] = $tokens[1];
            }

        }else{
            echo "Puked...";
        }
    }

fclose($file);
$x++;
}

print_r($info);

OUTPUT:

Array
(
    [0] => Array
        (
            [ServerName] => bootstrap
            [ServerAlias] => BootstrapProject
            [DocumentRoot] => /data/sites/bootstrap/htdocs
            [ErrorLog] => /data/sites/bootstrap/log/error.log
        )

    [1] => Array
        (
            [ServerName] => localhost
            [ServerAlias] => dfs
            [DocumentRoot] => /data/sites/scott/htdocs
            [ErrorLog] => /data/sites/scott/log/error.log
        )

    [2] => Array
        (
            [ServerName] => wordpress
            [ServerAlias] => wordpress
            [DocumentRoot] => /data/sites/wordpress/public_html
            [ErrorLog] => /data/sites/wordpress/log/error.log
        )

)
于 2014-03-19T17:35:47.797 回答
1

As far as I found, there is no such library available. If you want a simple solution, you could just use regexes to find all virtual hosts, and use regexes on that to find all the variables and their values. Though you should take care with things like <Directory> inside vhost definitions.

于 2013-01-08T14:05:57.120 回答
1

hmmm the answer this was based on has dissapeared/got deleted, for whatever reason, here's my finalised version of it:

<?php
function return_server_alias($fileName){
    $file = fopen($fileName,'r');
    $servers = array();
    while(!feof($file)) { 
        $line = fgets($file);
        // STRIP WHITE SPACE HERE
        $line = trim($line);
        // CHECK IF STRING BEGINS WITH ServerAlias
        $tokens = explode(' ',$line);
        if(!empty($tokens)){
            if(strtolower($tokens[0]) == 'serveralias'){
                $servers[] = $tokens[1];
            }
        }
    }
    fclose($file);
    return $servers;
}
于 2013-01-08T17:38:42.620 回答
-1

Here's my rework to get more informations from yours vhosts:

<pre><?php

    # Get Vhosts files
    $path       = '/etc/apache2/sites-enabled'; # change to suit your needs
    $a_directory = scandir($path);
    $a_conf_files = array_diff($a_directory, array('..', '.'));
    $info = array(); $x=0;`enter code here`

    foreach($a_conf_files as $conf_file){
     $Thisfile   = fopen($path .'/'.$conf_file, 'r')or die('No open ups..');

        $info[$x]['VHostFile'] = $conf_file;
        while(!feof($Thisfile)){
            $tokens = array();
            $line = fgets($Thisfile);
            $line = trim($line);

            if(preg_match('/\s*#+.*/',$line))
                $info[$x]['Commentaires'][] = $line;
            else
    {


            if(count($tokens)==3){
                if(strtolower($tokens[1]) == 'servername'){
                    $info[$x]['ServerName'][] = $tokens[2];
                    $index[$tokens[2]] = $x;
                }
                elseif(strtolower($tokens[1]) == 'documentroot'){
                    $info[$x]['DocumentRoot'] = $tokens[2];
                }
                elseif(strtolower($tokens[1]) == 'errorlog'){
                    $info[$x]['ErrorLog'] = $tokens[2];
                }
                elseif(strtolower($tokens[1]) == 'serveralias'){
                    $info[$x]['ServerAlias'][] = $tokens[2];
                }
                elseif(strtolower($tokens[1]) == 'virtualhost'){
                    $info[$x]['VirtualHost'] = $tokens[2];
                }
                elseif(strtolower($tokens[1]) == 'loglevel'){
                    $info[$x]['LogLevel'] = $tokens[2];
                }
                elseif(strtolower($tokens[1]) == 'assignuserid'){
                    $info[$x]['AssignUserId'] = $tokens[2];
                }
                elseif(strtolower($tokens[1]) == 'ifmodule'){
                    $info[$x]['IfModule'][] = $tokens[2];
                }
                elseif(strtolower($tokens[1]) == 'directory'){
                    $info[$x]['Directory'][] = $tokens[2];
                }
                elseif(strtolower($tokens[1]) == 'serveradmin'){
                    $info[$x]['ServerAdmin'] = $tokens[2];
                }
                elseif(strtolower($tokens[1]) == 'options'){
                    $info[$x]['Options'] = $tokens[2];
                }
                elseif(strtolower($tokens[1]) == 'indexoptions'){
                    $info[$x]['IndexOptions'] = $tokens[2];
                }
                elseif(strtolower($tokens[1]) == 'alias'){
                    $info[$x]['Alias'][] = $tokens[2];
                }
                elseif(!empty($tokens[1]))
                    $info[$x]['Autre'][$tokens[1]] = $tokens[2];

            }elseif(!empty($line)){
                $info[$x]['Illisible'][] = $line;
            }
    }
        }

    fclose($Thisfile);
    $x++;
    }

    echo var_export($index,true);

    echo var_export($info,true);

?></pre>
于 2014-11-05T17:21:31.413 回答