Apache

Parses and saves Apache configuration files. No options are provided by this container.

Writing an apache configuration file

This example script will create a new apache configuration file for a virtual host. It first adds a Listen directive and then a virtual host section containing its settings.

<?php
require_once 'Config.php';

$conf = new Config();
$root $conf->getRoot();

$root->createDirective('Listen''82');
$root->createBlank();

$root->createComment('My virtual host');
$vhost $root->createSection('VirtualHost', array('127.0.0.1:82'));
$vhost->createDirective('DocumentRoot''/usr/share/www');
$vhost->createDirective('ServerName''my-pear.example.org');

$conf->writeConfig(sys_get_temp_dir() . '/dummy-apache.conf''apache');
?>

Generated configuration file

# My virtual host
<VirtualHost 127.0.0.1:82>
  DocumentRoot /usr/share/www
  ServerName my-pear.example.org
</VirtualHost>

Reading an existing config file

Our task is to open an existing Apache configuration file and extract all Listen directives from it, using the container's getItem method.

Config file

# If you just change the port or add more ports here, you will likely also
# have to change the VirtualHost statement in
# /etc/apache2/sites-enabled/000-default
# This is also true if you have upgraded from before 2.2.9-3 (i.e. from
# Debian etch). See /usr/share/doc/apache2.2-common/NEWS.Debian.gz and
# README.Debian.gz

NameVirtualHost *:80
Listen 80
Listen 82

<IfModule mod_ssl.c>
    # If you add NameVirtualHost *:443 here, you will also have to change
    # the VirtualHost statement in /etc/apache2/sites-available/default-ssl
    # to <VirtualHost *:443>
    # Server Name Indication for SSL named virtual hosts is currently not
    # supported by MSIE on Windows XP.
    Listen 443
</IfModule>

<IfModule mod_gnutls.c>
    Listen 443
</IfModule>

PHP script

<?php
require_once 'Config.php';

$file dirname(__FILE__) . '/apache-read-config.txt';

$conf = new Config();
$root $conf->parseConfig($file'apache');
if (
PEAR::isError($root)) {
    echo 
'Error reading config: ' $root->getMessage() . "\n";
    exit(
1);
}

$i 0;
while (
$item $root->getItem('directive''Listen'nullnull$i++)) {
    echo 
$item->name ': ' $item->content "\n";
}

?>