【问题标题】:Parse Apache VHosts to get list of Domains and Folder Paths解析 Apache VHosts 以获取域和文件夹路径列表
【发布时间】:2012-12-22 09:24:58
【问题描述】:

给定路径 X 或 apache vhosts 配置文件的副本,我将如何使用 PHP 解析该文件?

例如给定一个包含 Apache vhosts 配置内容的字符串的变量,我将如何获取托管域/子域别名的列表?

例如,给定:

#
# 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>

我如何得到这个输出:

  • 本地主机
  • 2.7.localhost.com

这是用 Python 编写的非常接近的东西:

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

【问题讨论】:

  • 通过快速搜索,我没有看到任何已经执行此操作的 php 库...您可能只需要自己解析它。快速搜索确实产生了一个 python 库poldylicious.de/system/files/apacheconfig.py.txt
  • 嗯,这被标记为重复但我找不到这样的重复?
  • 嗯,这不是重复的,它也没有回答我的问题。那家伙想找到错误日志,我不想。答案也没有给我我需要的东西

标签: php apache vhosts


【解决方案1】:

据我所知,没有这样的库可用。如果您想要一个简单的解决方案,您可以使用正则表达式查找所有虚拟主机,然后使用正则表达式查找所有变量及其值。尽管您应该注意 vhost 定义中的 &lt;Directory&gt; 之类的内容。

【讨论】:

    【解决方案2】:

    嗯,这个基于的答案已经消失/被删除,无论出于何种原因,这是我的最终版本:

    <?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;
    }
    

    【讨论】:

      【解决方案3】:
      # 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);
      

      输出:

      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
              )
      
      )
      

      【讨论】:

      • 改编了他上面的答案,并发布了我的最终代码,以显示它是如何输出以进行传播的。
      【解决方案4】:

      这是我从您的虚拟主机获取更多信息的返工:

      <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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-09-23
        • 2010-12-27
        • 2014-09-03
        • 1970-01-01
        • 2019-01-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多