nicolus/apache-config-parser

轻松从Apache2配置文件中获取配置的域名

v1.0.0 2022-07-31 16:09 UTC

This package is auto-updated.

Last update: 2024-09-10 09:37:09 UTC


README

这是一个小的PHP库,允许您解析Apache2配置文件,并列出配置的每个网站及其域名、端口、文档根和别名。它基于此仓库构建: https://github.com/shabuninil/apache_config_parser

安装

使用 composer 进行安装

composer require nicolus/apache-config-parser

用法

通过提供一个起点(Apache2配置文件或包含配置文件的目录)创建一个新的 Parser,然后使用 ->getHosts() 方法获取 Hosts 数组。

例如

use Nicolus\ApacheConfigParser\Parser;

require 'vendor/autoload.php';

$parser = new Parser('/etc/apache2/apache2.conf');
$hosts = $parser->getHosts();

print_r($hosts);

将输出类似的内容

Array
(
    [0] => Nicolus\ApacheConfigParser\Host Object
        (
            [name] => example.com
            [port] => 80
            [root] => /var/www/example.com/
            [aliases] => Array
                (
                    [0] => www.example.com
                )
        )

    [1] => Nicolus\ApacheConfigParser\Host Object
        (
            [name] => example.com
            [port] => 443
            [root] => /var/www/example.com/
            [aliases] => Array
                (
                    [0] => www.example.com
                )
        )

    [2] => Nicolus\ApacheConfigParser\Host Object
        (
            [name] => mysite.com
            [port] => 443
            [root] => /var/www/mysite/public/
            [aliases] => Array
                (
                )
        )

注意

  • "Include" 和 "IncludeOptional" 指令将被尊重,并将加载包含的文件。
  • 包含中的 '*' 通配符将被尊重。
  • 如果您传递一个以 / 结尾的目录路径(例如 /etc/apache2/sites-enabled/),它将递归地加载此目录及其子目录中的所有文件,然后处理包含。
  • 这绝对不是一个完整的解析器,它应该覆盖大多数常规用例,但请预期在某些边缘情况下可能会出错。

支持和贡献

如果您遇到问题,请随时在github上打开一个问题,并使用示例配置文件描述出了什么问题。

欢迎提交拉取请求,尤其是如果它们不会破坏测试或添加新测试的话。