PHP Apache Configuration: Read and write Apache 2 server configuration files

Recommend this page to a friend!
  Info   View files Documentation   View files View files (6)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 66%Total: 266 All time: 7,626 This week: 314Up
Version License PHP version Categories
apache-conf 1.0Custom (specified...5.5PHP 5, Files and Folders, Configuration, H..., P...
Description Author

This package can read and write Apache 2 server configuration files.

It can create a new configuration file for the Apache server and add new directive parameters for virtual hosts.

The class can also load existing Apache configuration from file or a string to retrieve change its values.

The edited configuration can be saved back to the same file or to a new file.

Picture of Aleksey Nemiro
  Performance   Level  
Name: Aleksey Nemiro <contact>
Classes: 6 packages by
Country: Russian Federation Russian Federation
Age: 36
All time rank: 206558 in Russian Federation Russian Federation
Week rank: 639 Up20 in Russian Federation Russian Federation Up
Innovation award
Innovation award
Nominee: 3x

Winner: 1x

Details

ApacheConf.PHP Latest Stable Version Total Downloads License

This is a set of classes for working with configuration files of the Apache2 web server.

Code licensed under Apache License Version 2.0.

Requirements

  • PHP5 >= 5.5, PHP7;
  • Apache2 >= 2.4.

NOTE: Working with the earlier versions were not tested, but it is possible that everything is working.

How to use?

Include Conf.php file and import the class `Nemiro\Apache\Conf`.

# include the class file (use own path of the file location)
require_once 'Apache/Conf.php';

# import class
use Nemiro\Apache\Conf as ApacheConf;

Load config from file

# create instance and load config from file
$conf = new ApacheConf('/etc/apache2/sites-available/example.org.conf');
# or
# $conf = ApacheConf::CreateFromFile('/etc/apache2/sites-available/example.org.conf');

# get values
var_dump($conf['VirtualHost']);
var_dump($conf['VirtualHost']->ParametersAsString());
var_dump($conf['VirtualHost']['DocumentRoot']->ParametersAsString());
var_dump($conf['VirtualHost']['ServerName']->ParametersAsString());
var_dump($conf['VirtualHost']['Alias']);

Load config from string

# config data
$str = '<VirtualHost 127.0.0.1:80>
	DocumentRoot /home/example.org/www
  ServerName example.org

  <Location />
	  AuthType Basic
		AuthUserFile users.pwd
		Require valid-user
	</Location>
</VirtualHost>';

# parse string
$conf = ApacheConf::CreateFromString($str);

# get values
var_dump($conf['VirtualHost']);
var_dump($conf['VirtualHost']->ParametersAsString());
var_dump($conf['VirtualHost']['ServerName']->ParametersAsString());

# get location
$location = $conf['VirtualHost']['Location'][0];
var_dump($location);

Save to file

# load from file
$conf = ApacheConf::CreateFromFile('/etc/apache2/sites-available/example.org.conf');

# set values
$conf['VirtualHost']['ServerName']->Parameters = array('example.org', 'www.example.org');
$conf['VirtualHost']['DocumentRoot']->Parameters = array('/home/example.org/www');

# create a new directive
$new_directory = ApacheConf::CreateDirective('Directory');
$new_directory->AddDirective('AllowOverride', 'All');
$new_directory->AddDirective('Allow', array('from', 'all'));
$new_directory->AddDirective('Require', array('all', 'granted'));

# add the new Directory section to the VirtualHost section
$new_directory->AddTo($conf['VirtualHost']);

# save
$conf->Save();

# or save as...
# $conf->Save('newFileName.conf');

Get string from current instance

# load from file
$conf = new ApacheConf::CreateFromFile('/etc/apache2/sites-available/example.org.conf');

# set values
$conf['VirtualHost']['ServerName']->Parameters = array('example.org', 'www.example.org');
$conf['VirtualHost']['DocumentRoot']->Parameters = array('/home/example.org/www');

# create a new directive
$new_directory = ApacheConf::CreateDirective('Directory');
$new_directory->AddDirective('AllowOverride', 'All');
$new_directory->AddDirective('Allow', array('from', 'all'));
$new_directory->AddDirective('Require', array('all', 'granted'));

# add the new Directory section to the VirtualHost section
$new_directory->AddTo($conf['VirtualHost']);

# get as string
$string = $conf->GetAsString();

# show string
var_dump($string);

Create a new config

# create an instance
$conf = new ApacheConf();

# create VirtualHost
$virtualHost = ApacheConf::CreateDirective('VirtualHost', '192.168.100.39:8080');
$virtualHost->AddDirective('DocumentRoot', '/home/example.org/www');
$virtualHost->AddDirective('ServerName', 'example.org');

# add to conf
$conf->Add($virtualHost);

# create directory
$directory = ApacheConf::CreateDirective('Directory');
$directory->AddDirective('AllowOverride', 'All');
$directory->AddDirective('Allow', array('from', 'all'));
$directory->AddDirective('Require', array('all', 'granted'));

# add the new Directory section to the VirtualHost section
$directory->AddTo($virtualHost);

# get as string
$string = $conf->GetAsString();

# show string
var_dump($string);

# or save
# $conf->Save('newFileName.conf');
  Files folder image Files  
File Role Description
Plain text file Conf.php Class Main class.
Plain text file Directive.php Class Class source
Plain text file DirectiveCollection.php Class Class source
Plain text file DirectiveGroup.php Class Class source
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

 Version Control Unique User Downloads Download Rankings  
 100%
Total:266
This week:0
All time:7,626
This week:314Up
User Ratings User Comments (1)
 All time
Utility:93%StarStarStarStarStar
Consistency:93%StarStarStarStarStar
Documentation:100%StarStarStarStarStarStar
Examples:-
Tests:-
Videos:-
Overall:66%StarStarStarStar
Rank:726
 
Hi.
4 years ago (keran)
70%StarStarStarStar
 

For more information send a message to info at phpclasses dot org.