2

Hi I need to extract each VirtualHosts from my httpd.conf to separate files for easier management.

Original httpd.conf .

..
<VirtualHost *:XXXX>
  SuexecUserGroup user1 groupX
  ...
</VirtualHost>
<VirtualHost *:XXXY>
  SuexecUserGroup user2 groupY
  ...
</VirtualHost>
<VirtualHost *:XXYY>
  SuexecUserGroup user3 groupZ
  ...
</VirtualHost>
...

And I would like to have files like: - XXXX_user1.conf containing:

Listen XXXX
<VirtualHost *:XXXX>
  SuexecUserGroup user1 groupX
  ...
</VirtualHost>

- XXXY_user2.conf containing:

Listen XXXY
<VirtualHost *:XXXY>
  SuexecUserGroup user2 groupY
  ...
</VirtualHost>

Sounds a bit complicated but I have like 500 to do by hand ;(

Many thanks for answers

2 Answers 2

1

Give this a try:

#!/usr/bin/gawk -f
BEGIN {
    RS = "</VirtualHost>\n"
}
{
    prefix = gensub("*:([^>]*)>", "\\1", "1", $2)
    filename = prefix "_" $4 ".conf"
    print "Listen " prefix "\n" $0 "\n" RS > filename
}

Save it in a file named, perhaps, "httpdsplit" and do:

$ chmod u+x httpdsplit
$ ./httpdsplit httpd.conf
3
  • I've tweaked a bit the script so it adds also Listen XXXX at the beginning: . #!/usr/bin/gawk -f BEGIN { RS = "</VirtualHost>\n" } { prefix = gensub(":([^>])>", "\\1", "1", $2) filename = prefix "_" $4 ".conf" print "Listen "prefix $0 "" RS > filename }
    – Warnaud
    Oct 5, 2010 at 11:28
  • @Warnaud: Oops, sorry, I forgot to add that part. I'll edit my answer. Don't forget to mark it as accepted if it satisfies your requirement. Oct 5, 2010 at 14:55
  • I would like to really since it's exactly what I was looking for but ... how to do that :( ? The site is great but the syntax and this kind of options are quite invisible.
    – Warnaud
    Oct 6, 2010 at 11:15
0

Though you didn't mention it, this looks like apache httpd.conf. Though I recommend the existing answer which splits the files, there is another option.

If the apache server is built with mod_perl and if you know (or are willing to learn) Perl, remember that mod_perl binds with all aspects of apache, including config. You could write the config with a Perl stanza and generate the VirtualHost config within perl itself.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .