http://qs1969.pair.com?node_id=1184917

nysus has asked for the wisdom of the Perl Monks concerning the following question:

I'm running a webserver on a local machine. I want to automate the process of setting up a website on it, mostly as an exercise to get more proficient with writing larger programs with Perl and getting familiar with more tools and modules out there. When setting up a new website, I'd like to update the apache config file with a very simple virtual host to direct traffic to the site:

<VirtualHost *:80> DocumentRoot /var/www/temp ServerName temp.mydomain.com </VirtualHost>

Now it would be a simple matter to just append these lines to the end of the config file. But that's not a very elegant solution. I'm wondering what tools other Monks might have used to modify apache config files. It doesn't have to be a tool specific to apache like a cpan module. I'd be particularly interested to know if learning something like Template::Toolkit or other general purpose tools might allow me to intelligently handle updating config files in general.

If someone could steer me in the right general direction or things I should investigate, I'd appreciate the help.

$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate";
$nysus = $PM . ' ' . $MCF;
Click here if you love Perl Monks

Replies are listed 'Best First'.
Re: Guidance on updating apache config files
by hippo (Bishop) on Mar 17, 2017 at 09:17 UTC
    I'd be particularly interested to know if learning something like Template::Toolkit or other general purpose tools might allow me to intelligently handle updating config files in general.

    Template Toolkit is precisely what I use when building Apache config files from within Perl. IMHO it is worth the (not inconsiderable) time required to learn it as it is so flexible you will subsequently find it useful for all manner of other scenarios.

    Conversely for parsing the config files I recommend Apache::Admin::Config which seems to work reasonably well in most cases.

      Ok, thanks. That answers my questions perfectly. I was thinking about looking into Template::Toolkit but was worried about investing a lot of time learning something that ends up being unsuitable. I will go this route.

      $PM = "Perl Monk's";
      $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate";
      $nysus = $PM . ' ' . $MCF;
      Click here if you love Perl Monks

Re: Guidance on updating apache config files
by thanos1983 (Parson) on Mar 16, 2017 at 22:53 UTC

    Hello nysus,

    Have you checked the Apache::ConfigFile module? Seems to be exactly what you want to do.

    From the Description of the documentation:

    This module parses the Apache httpd.conf, or any compatible config fil +e, and provides methods for you to access the values from the config +file.

    Hope this helps.

    Seeking for Perl wisdom...on the process of learning...not there...yet!

      Apache::ConfigFile does seem adequate for parsing, which is usually the hardest part. Its write method is documented in the POD as not working, but it probably wouldn't be too hard to just use a template approach to producing a correct file.

      Now here's some advice that I've learned from a real adventure in debugging: Use proper locking that holds the lock from the time you read until the time you're done writing and have closed the output handle. Keep a copy of the original. Validate the newly written file with apachectl configtest and verify Syntax OK, and roll back to the original using the same sort of vigilance if it fails validation. Anything less can eventually get you a corrupted httpd.conf someday.


      Dave

        Good advice. I didn't consider these things.

        $PM = "Perl Monk's";
        $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate";
        $nysus = $PM . ' ' . $MCF;
        Click here if you love Perl Monks

      I have a bit, yes. It's got some quirks to it that I don't like trying to compile in files included by the config files it processes. I'm also looking to something more general purpose that I might be able to adapt to other situations.

      $PM = "Perl Monk's";
      $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate";
      $nysus = $PM . ' ' . $MCF;
      Click here if you love Perl Monks

Re: Guidance on updating apache config files
by Miguel (Friar) on Mar 18, 2017 at 07:42 UTC
    Instead of modifying the httpd.conf file I create a new configuration file (e.g. vh_website.conf) and read it with Include /etc/httpd/extra/vhosts/vh_*.conf in the httpd.conf.
Re: Guidance on updating apache config files
by Anonymous Monk on Mar 17, 2017 at 16:12 UTC
    "If someone could steer me in the right general direction ..."

    Configuration Management. AKA Puppet, Chef, Salt or Ansible. These are the ONLY things you should be using. Period. You are suffering from tunnel vision ... as Abraham Maslow said in 1966, "I suppose it is tempting, if the only tool you have is a hammer, to treat everything as if it were a nail."