Php 解析Apache VHosts以获取域和文件夹路径列表
给定一个路径X或apachevhosts配置文件的副本,如何使用PHP解析该文件 例如,给定一个包含Apache vhosts配置内容的字符串的变量,如何获取托管域/子域别名列表 例如,假设:Php 解析Apache VHosts以获取域和文件夹路径列表,php,apache,vhosts,Php,Apache,Vhosts,给定一个路径X或apachevhosts配置文件的副本,如何使用PHP解析该文件 例如,给定一个包含Apache vhosts配置内容的字符串的变量,如何获取托管域/子域别名列表 例如,假设: # # Use name-based virtual hosting. # NameVirtualHost *:80 <VirtualHost *:80> ServerAdmin contact@tomjn.com DocumentRoot "/srv/www/local
#
# 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>
<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>
#
#使用基于名称的虚拟主机。
#
名称虚拟主机*:80
服务器管理员contact@tomjn.com
DocumentRoot“/srv/www/localhost/
服务器名127.0.0.1
服务器别名本地主机
CustomLog“/srv/www/logs/localhost-access_log.log”组合
ErrorLog“/srv/www/logs/localhost-error\u log.log”
选项索引如下SYMLINKS包括ExecCGI
允许超越所有
命令允许,拒绝
通融
服务器管理员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”组合
ErrorLog“/srv/www/logs/2.7.localhost.com-error\u log.log”
选项索引如下SYMLINKS包括ExecCGI
允许超越所有
命令允许,拒绝
通融
如何获得此输出:
- 本地主机
- 2.7.localhost.com
据我所知,没有这样的图书馆。如果您想要一个简单的解决方案,您可以只使用正则表达式来查找所有虚拟主机,并使用正则表达式来查找所有变量及其值。尽管您应该注意vhost定义中的
之类的内容。hmmm基于此的答案已消失/被删除,无论出于何种原因,以下是我的最终版本:
<?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;
}
输出:
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
)
)
以下是我的重做,以从您的虚拟主机获取更多信息:
通过快速搜索,我没有看到任何php库已经这样做了。。。您可能需要自己解析它。快速搜索确实产生了一个python库,嗯,这被标记为一个副本,但我找不到这样的副本?嗯,这不是一个副本,它也不能回答我的问题。那家伙想找到错误日志,我不知道。答案并没有给我我所需要的。他在上面修改了他的答案,并发布了我的最终代码,以显示它是如何输出以供传播的。
<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>