views:

1079

answers:

6

I'd like to create a script to manipulate Apache2 configuration directly, reading and writing its properties (like adding a new VirtualHost, changing settings of one that already exists).

Are there any libs out there, for Perl, Python or Java that automates that task?

+2  A: 

This is the ultimate Apache configurator:

http://perl.apache.org/

exposes many if not all Apache internals to programs written in Perl.

For instance: http://perl.apache.org/docs/2.0/api/Apache2/Directive.html

(Of course that it can do much much more than just configuring it).

On the other hand, it needs to be loaded and runs within Apache, it's not a config file parser/editor.

Vinko Vrsalovic
+7  A: 

Rather than manipulate the config files, you can use mod_perl to embed Perl directly into the config files. This could allow you, for example, to read required vhosts out of a database.

See Configure Apache with Perl Example for quick example and Apache Configuration in Perl for all the details.

Paul Dixon
That's SO cool! I never even thought such a thing existed! Thanks so much...
kolrie
Warning; it seriously bloats the server, it makes it more brittle (one more source of memory leaks) and, when upgrading your system, you have one more dependency to take care of.For such an use case, producing the configuration from a program seems safer.
bortzmeyer
If you only use the perl blocks during configuration, memory usage will not be an issue. The extra library linked in will make the children look bigger, but it's all shared memory.
Ken Fox
+2  A: 

Try the Apache::ConfigFile Perl module.

David Grant
That doesn't actually write config files though. The manual says of the write() function: "This method is currently under development and does not work. Patches welcome."
Paul Dixon
+7  A: 

In Perl, you've got at least 2 modules for that:

Apache::ConfigFile

Apache::Admin::Config

sebthebert
+1  A: 

Look at Augeas, it's not specifically for Apache-httpd config. files it's just a generic config. file "editor" API. One of it's major selling points is that it will keep comments/etc. is happy for other tools to alter the files and will refuse to let you save broken files.

Also the fact that you can use the same API in all the languages you asked about, and that you can edit other config. files using the same APIs are both major advantages IMO.

James Antill
A: 

Also see Config::General, which claims to be fully compatible with Apache configuration files. I use it to parse my Apache configuration files for automatic regression testing after configuration changes.

Corion