<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>netCUBED Blog &#187; dynamic</title>
	<atom:link href="http://blog.netcubed.de/tag/dynamic/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.netcubed.de</link>
	<description>Just another web developer's weblog</description>
	<lastBuildDate>Mon, 29 Jun 2009 20:58:35 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Create Apache2 VHosts on the fly with mod_perl</title>
		<link>http://blog.netcubed.de/2009/06/create-apache2-vhosts-on-the-fly-with-mod_perl/</link>
		<comments>http://blog.netcubed.de/2009/06/create-apache2-vhosts-on-the-fly-with-mod_perl/#comments</comments>
		<pubDate>Mon, 22 Jun 2009 05:56:05 +0000</pubDate>
		<dc:creator>Moritz Onken</dc:creator>
				<category><![CDATA[Perl]]></category>
		<category><![CDATA[apache2]]></category>
		<category><![CDATA[dynamic]]></category>
		<category><![CDATA[mod perl]]></category>
		<category><![CDATA[mod_perl]]></category>
		<category><![CDATA[mod_perl2]]></category>
		<category><![CDATA[virtual host]]></category>

		<guid isPermaLink="false">http://blog.netcubed.de/?p=154</guid>
		<description><![CDATA[This script is in use for many years now and does a decent job. I set up a MySQL table which specifies the basic configuration options for an Apache2 virtual host:

CREATE TABLE `vhosts` &#40;
  `id` int&#40;11&#41; NOT NULL AUTO_INCREMENT,
  `DocumentRoot` text NOT NULL,
  `ServerName` varchar&#40;255&#41; NOT NULL DEFAULT '',
  `ServerAlias` varchar&#40;255&#41; [...]]]></description>
			<content:encoded><![CDATA[<p>This script is in use for many years now and does a decent job. I set up a MySQL table which specifies the basic configuration options for an Apache2 virtual host:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`vhosts`</span> <span style="color: #66cc66;">&#40;</span>
  <span style="color: #ff0000;">`id`</span> int<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`DocumentRoot`</span> text <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`ServerName`</span> varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`ServerAlias`</span> varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`ScriptAlias`</span> text <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`CustomLog`</span> text <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span>  <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span> ;</pre></div></div>

<p>In <code>/etc/apache2/conf.d/vhosts.perl.conf</code> I&#8217;ve put this little script:</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #009999;">&lt;Perl&gt;</span>
<span style="color: #000000; font-weight: bold;">use</span> DBI<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> File<span style="color: #339933;">::</span><span style="color: #006600;">Path</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$dbh</span> <span style="color: #339933;">=</span> DBI<span style="color: #339933;">-&gt;</span><span style="color: #006600;">connect</span><span style="color: #009900;">&#40;</span> <span style="color: #ff0000;">&quot;DBI:mysql:vhosts&quot;</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;apache&quot;</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;password&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$sth</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$dbh</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;select * from vhosts&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$vhost</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">fetchrow_hashref</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">unless</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">-</span>d <span style="color: #009900;">&#40;</span> <span style="color: #000066;">split</span><span style="color: #009900;">&#40;</span> <span style="color: #009966; font-style: italic;">/ /</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$vhost</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>ScriptAlias<span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        mkpath<span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span> <span style="color: #000066;">split</span><span style="color: #009900;">&#40;</span> <span style="color: #009966; font-style: italic;">/ /</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$vhost</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>ScriptAlias<span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">unless</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">-</span>d <span style="color: #0000ff;">$vhost</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>DocumentRoot<span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        mkpath<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$vhost</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>DocumentRoot<span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">unless</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">-</span>e <span style="color: #009900;">&#40;</span> <span style="color: #000066;">split</span><span style="color: #009900;">&#40;</span> <span style="color: #009966; font-style: italic;">/ /</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$vhost</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>CustomLog<span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$dir</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$vhost</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>CustomLog<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
        <span style="color: #0000ff;">$dir</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">s/(.*)\/.*?$/$1/</span><span style="color: #339933;">;</span>
        mkpath<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$dir</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066;">undef</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$vhost</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>id<span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">push</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">@VirtualHost</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> <span style="color: #ff0000;">&quot;*&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$vhost</span> <span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
<span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">finish</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #0000ff;">$dbh</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">disconnect</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>Perl<span style="color: #339933;">&gt;</span></pre></div></div>

<p>As you can see the script uses all columns from the table to set up the virtual host. You can easily add more columns (e.g. ErrorLog) to the table as long as their name is known to the apache2 configuration.</p>
<p>After you added a new vhost to the database you need to restart the apache server so that the perl script is run and the vhost is set up.</p>
<p>This will create the directories of any vhost which has been added to the table &#8220;vhosts&#8221; and create a virtual host inside of apache. This is very handy if you need to maintain multiple sites which share the same layout and need basic functionality.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.netcubed.de/2009/06/create-apache2-vhosts-on-the-fly-with-mod_perl/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
