Dynamically manage Apache virtualhosts in Linux

by jagbir on September 11, 2011

This is second part of article to describe how to dynamically manage Apache Virtual host. You can read first article here.

In earlier article I mentioned using a php script to dynamically create/remove virtualhost entry in Apache (httpd) config file and then reload it using cron.

Here I would describe how to manage DNS to dynamically recognize newly created virtualhosts. Again, this might not be the best or efficient way to implement this but this is what worked for me. After creating virtualhosts in Apache, you need to update DNS so that new virtualhosts start working. To update DNS dynamically, your DNS provider should have some way (like API) which enable you to manipulate its records. There are few providers offering this facility. For this experiment, I selected DNSMadeEasy which provides APIs to add/remove/update records on fly using scripts.

You can see the script below written in php:

<?php
date_default_timezone_set("GMT");
$reqDate = date("D, d M Y H:i:s T",mktime(date("H"),date("i"),date("s")-45,date("m"),date("d"),date("y")));
$apiKey = "my-api-key";
$secretKey = 'my-secret-key';
$secretKey2 = hash_hmac('sha1', $reqDate, $secretKey);
 
$URL = "http://api.dnsmadeeasy.com/V1.2/domains/example.com/records";
echo "<b>Endpoint :</b> ".$URL;
 
$headers = array(
'x-dnsme-requestId: '.time(),
'x-dnsme-apiKey: '.$apiKey,
'x-dnsme-requestDate: '.$reqDate,
'x-dnsme-hmac: '.$secretKey2,
'accept:application/xml',
'content-type:application/xml'
);
 
echo "<br><br><b>Request Headers</b><br>";
echo 'x-dnsme-requestId: '.time()."<br>".
'x-dnsme-apiKey: '.$apiKey."<br>".
'x-dnsme-requestDate: '.$reqDate."<br>".
'x-dnsme-hmac: '.$secretKey2."<br>".
'accept:application/xml'."<br>".
'content-type:application/xml';
 
$body = '
<record>
<data>xx.xx.xx.xx</data>
<gtdLocation>Default</gtdLocation>
<id>123456</id>
<name>dummy</name>
<ttl>1800</ttl>
<type>A</type>
</record>
';
 
echo "<br><br>".$body;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
//curl_setopt($ch, CURLOPT_PUT, 1);
//curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
$ResponseStr = curl_exec($ch);
curl_close ($ch);
echo "<pre>";
echo $ResponseStr;
?>

You need to have Api key and Secret key which you will get upon registration on DNSMadeEasy. This is very basic script to update DNS record dynamically. You can extend it further to incorporate more functions.

So basically to manage Apache virtalhosts dynamically, you need following functionality:
* Script to create/update/remove them from Apache (httpd) config file.
* Reload Apache to apply new settings which can be done through a simple cron entry.
* Script to update DNS records to reflect changes.

Let me know if you have some other way/idea to implement this.

Helpful related articles:
* PHP script to dynamically add/remove/update virtualhosts in Apache
* Upgrade Apache in Linux quickly
* Upgrade to latest PHP using yum upgrade
* 5 Steps to secure your Linux Server
* Disable weak ssl ciphers in lighttpd in Linux
* Disable SSL ver 2 in Apache for security and PCI Compliance

Previous post:

Next post: