Hi,

As a project for the school holidays, I am trying to write a PHP script that can add, remove and edit entire <VirtualHost> and <Directory> containters (as well as the directives contained inside them) from Apache's httpd.conf. This will mostly be running on a Windows test server.

Adding the lines to the conf file is easy - just check to see if it already exists and if not, append the text to the end of the file.

To edit/remove containers and directives, I thought I could try and match up lines using regular expressions (i.e delete a specified <Directory /dir-name-here> "tag" and then delete every line after that until I reach the closing "tag" - i.e. </Directory>). The problem is, I am quite inexperienced with PHP, and was wondering if anyone could get me going with some ideas on how to turn that idea into code?

Also comes the problem with grabbing information from the conf file and displaying it as a list on an HTML page (i.e. list all the VirtualHosts that exist).

If anyone can help, it is VERY much appreciated!

Thanks,

Ben Hodgson

    Here's a big hint: make a vhosts.conf file and just include that in your httpd.conf:

    include "conf/vhost.conf"
    

    That'll weed out your worries of checking the entire httpd.conf file every time. Saves you a lot of trouble.

      Follow on: Just store everything in a database, and push the whole thing out into vhosts.conf without reading and parsing the orignal.

        Hey.

        Thanks for your help - ive already included the extra file...

        #####################################
        #   Include Iconnekt Configuration  #
        #####################################
        Include conf/iconnekt.conf

        Database sounds like a good idea for being able to read what should be in the file at any one time - unless of course someone tries to configure it manually. Then I suppose we're screwed lol. Perhaps have a script that syncs them running off cron (or the windows equivalent)?

        I suppose the database solves the removing and updating too. Just update/drop it with a MySQL query and then re-generate the entire .conf file from that. My only worry there is resource usage. What would take up more? A foreach working through an array obtained from a torrent of SQL queries or "parsing" a file by matching strings up using regular expressions?

        Thanks for your help,

        Ben Hodgson

          Resource usage shouldn't really be an issue. I've written similar things and they tend to run in a second or less on a decent machine.

          What I'd be worried about is which is more error prone. Just put a note in the top of the machine generated file when you create it that says that any edits made by hand will be lost the next timethe database driven generator fires off.

            Write a Reply...