I'd like to be able to split an old Apache2 configuration file into separate files one per virtual host. Would Config::ApacheFormat be the right module for that? It seems to read the configuration file in just fine, but how do I separate the VirtualHost blocks generically? And once, separate, how do I make the output something that Apache2 can tolerate?

PS. What if the value of VirtualHost (e.g. *:80, *:443) is different? I'd like the separation to occur for all vhosts, regardless of settings.

#!/usr/bin/perl use strict; use warnings; use Config::ApacheFormat; my $configfile = shift || '/dev/stdin'; my $config = Config::ApacheFormat->new( root_directive => 'ServerRoot', hash_directives => [ 'AddHandler' ], include_directives => [ 'Include', 'AccessConfig', 'ResourceConfig' ], setenv_vars => 1, fix_booleans => 1); $config->read( $configfile ); my @vhosts = $config->block( 'VirtualHost'=>'*:80' ); exit( 0 ); my $http = qq( + # Ensure that Apache listens on port 80 + Listen 80 <VirtualHost *:80> DocumentRoot "/www/example1" ServerName www.example.com # Other directives here </VirtualHost> + <VirtualHost *:80> DocumentRoot "/www/example2" ServerName www.example.org # Other directives here </VirtualHost> + );

In reply to Separate files for vhosts using Config::ApacheFormat by mldvx4

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.