<?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>Ivan Villareal &#187; Perl script to manage virtual hosts &#8211; Ivan Villareal</title>
	<atom:link href="http://ivanvillareal.com/tag/apache/feed/" rel="self" type="application/rss+xml" />
	<link>http://ivanvillareal.com</link>
	<description>IT stuff and more...</description>
	<lastBuildDate>Tue, 01 Nov 2011 23:00:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Perl script to manage virtual hosts</title>
		<link>http://ivanvillareal.com/linux/perl-script-to-manage-virtual-hosts/</link>
		<comments>http://ivanvillareal.com/linux/perl-script-to-manage-virtual-hosts/#comments</comments>
		<pubDate>Wed, 08 Jun 2011 19:54:57 +0000</pubDate>
		<dc:creator>Ivan Villareal</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Sysadmin]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[sysadmin]]></category>
		<category><![CDATA[ubuntu]]></category>
		<category><![CDATA[vhost]]></category>

		<guid isPermaLink="false">http://ivanvillareal.com/?p=237</guid>
		<description><![CDATA[I did this script a while back, and I&#8217;ve been using it on gentoo, ubuntu and centos distros with a few changes, this can be extended to add new functionality, [...]]]></description>
			<content:encoded><![CDATA[<p>I did this script a while back, and I&#8217;ve been using it on gentoo, ubuntu and centos distros with a few changes, this can be extended to add new functionality, and it may contain bugs, but so far it has worked for me pretty well, I can add/remove virtual hosts very quickly.</p>
<p>To use this script you only need the packages File::Path and Getopt::Long; both of them are already on newer distros, just put the script somewhere I have it in /usr/local/bin and call it with sudo.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">ivan<span style="color: #000000; font-weight: bold;">@</span>orion:<span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>vhosts$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> vhost-updater.pl <span style="color: #660033;">--add</span> <span style="color: #660033;">--domain</span> test.orion</pre></div></div>

<p>This is the output:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">Creating docroot <span style="color: #c20cb9; font-weight: bold;">dir</span>: <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>vhosts<span style="color: #000000; font-weight: bold;">/</span>test.orion<span style="color: #000000; font-weight: bold;">/</span>public_html 
Creating log <span style="color: #c20cb9; font-weight: bold;">dir</span>: <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>vhosts<span style="color: #000000; font-weight: bold;">/</span>test.orion<span style="color: #000000; font-weight: bold;">/</span>logs 
Site File: <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>apache2<span style="color: #000000; font-weight: bold;">/</span>sites-available<span style="color: #000000; font-weight: bold;">/</span>test.orion 
Creating Vhost... 
Adding host test.orion 
Run <span style="color: #ff0000;">'/etc/init.d/apache2 reload'</span> to activate new configuration<span style="color: #000000; font-weight: bold;">!</span>
Restarting apache 
 <span style="color: #000000; font-weight: bold;">*</span> Restarting web server apache2
 ... waiting    ...done.</pre></div></div>

<p>Once it has finished, point your browser to http://test.orion, and you&#8217;re done.</p>
<p>Enjoy!</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/perl -w</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#############################################################</span>
<span style="color: #666666; font-style: italic;"># The purpose of this script is to add/remove				# </span>
<span style="color: #666666; font-style: italic;"># virtual hosts easily, this script runs 					#</span>
<span style="color: #666666; font-style: italic;"># on Ubuntu/Debian withouth modifications. 					#</span>
<span style="color: #666666; font-style: italic;">#         													#	</span>
<span style="color: #666666; font-style: italic;">#         													#	</span>
<span style="color: #666666; font-style: italic;"># Author Ivan Villareal ivaano@gmail.com					#</span>
<span style="color: #666666; font-style: italic;">#         													#	</span>
<span style="color: #666666; font-style: italic;">#############################################################</span>
<span style="color: #000000; font-weight: bold;">use</span> strict<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: #000066;">qw</span><span style="color: #009900;">&#40;</span>mkpath rmtree<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> Getopt<span style="color: #339933;">::</span><span style="color: #006600;">Long</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #b1b100;">our</span> <span style="color: #0000ff;">$ipAddress</span>        <span style="color: #339933;">=</span> <span style="color: #ff0000;">'127.0.0.1'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">our</span> <span style="color: #0000ff;">$apacheConfigDir</span>  <span style="color: #339933;">=</span> <span style="color: #ff0000;">'/etc/apache2'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">our</span> <span style="color: #0000ff;">$sitesAvailable</span>   <span style="color: #339933;">=</span> <span style="color: #ff0000;">'sites-available'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">our</span> <span style="color: #0000ff;">$docRootPrefix</span>    <span style="color: #339933;">=</span> <span style="color: #ff0000;">'/var/www/vhosts'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">our</span> <span style="color: #0000ff;">$docRoot</span>          <span style="color: #339933;">=</span> <span style="color: #ff0000;">'public_html'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">our</span> <span style="color: #0000ff;">$logsDir</span>          <span style="color: #339933;">=</span> <span style="color: #ff0000;">'logs'</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$del</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">''</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$add</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">''</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$domain</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">''</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">getpwuid</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$&lt;</span> <span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">ne</span> <span style="color: #ff0000;">'root'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;Script needs root privileges <span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000066;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">unless</span> <span style="color: #009900;">&#40;</span>GetOptions <span style="color: #009900;">&#40;</span>
		<span style="color: #ff0000;">'del'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">\$del</span><span style="color: #339933;">,</span> 
		<span style="color: #ff0000;">'add'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">\$add</span><span style="color: #339933;">,</span> 
		<span style="color: #ff0000;">'domain=s'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">\$domain</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">or</span> usage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    usage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#print $paramResults;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$add</span> <span style="color: #339933;">||</span> <span style="color: #0000ff;">$del</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$domain</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$add</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            createVhost<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$domain</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">elsif</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$del</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            deleteVhost<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$domain</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
        usage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    usage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> usage <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">print</span> <span style="color: #339933;">&lt;</span> <span style="color: #339933;">&lt;</span>USAGE
This program will add <span style="color: #b1b100;">or</span> remove apache virtual hosts<span style="color: #339933;">.</span>
&nbsp;
usage<span style="color: #339933;">:</span> vhost<span style="color: #339933;">-</span>updater<span style="color: #339933;">.</span>pl <span style="color: #009900;">&#91;</span><span style="color: #339933;">--</span>add <span style="color: #339933;">|</span> <span style="color: #339933;">--</span>del<span style="color: #009900;">&#93;</span> <span style="color: #339933;">--</span>domain newhost<span style="color: #339933;">.</span>tld 
USAGE
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> returnVhostPaths
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$vhost</span> <span style="color: #339933;">=</span> <span style="color: #000066;">shift</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@dir</span> <span style="color: #339933;">=</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;">$docRootPrefix</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">%res</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">@dir</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$vhost</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$hostDir</span> <span style="color: #339933;">=</span> <span style="color: #000066;">join</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'/'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">@dir</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$res</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'docRoot'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$hostDir</span> <span style="color: #339933;">.</span> <span style="color: #ff0000;">'/'</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">$docRoot</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$res</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'logsDir'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$hostDir</span> <span style="color: #339933;">.</span> <span style="color: #ff0000;">'/'</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">$logsDir</span><span style="color: #339933;">;</span>
	<span style="color: #0000ff;">$res</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'hostDir'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$hostDir</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">#todo dir validation</span>
    <span style="color: #0000ff;">@dir</span> <span style="color: #339933;">=</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;">$apacheConfigDir</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;">@dir</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$sitesAvailable</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;">@dir</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$vhost</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$res</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'apacheConfig'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #000066;">join</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'/'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">@dir</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066;">return</span> <span style="color: #0000ff;">%res</span><span style="color: #339933;">;</span>   
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> createVhost <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$vhost</span> <span style="color: #339933;">=</span> <span style="color: #000066;">shift</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">#first create the docRoot</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">%vhostInfo</span> <span style="color: #339933;">=</span> returnVhostPaths<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$vhost</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    informOut<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Creating docroot dir: $vhostInfo{'docRoot'}&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    mkpath<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$vhostInfo</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'docRoot'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$user</span> <span style="color: #339933;">=</span> <span style="color: #000066;">getlogin</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$uid</span>  <span style="color: #339933;">=</span> <span style="color: #000066;">getpwnam</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$user</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$gid</span>  <span style="color: #339933;">=</span> <span style="color: #000066;">getgrnam</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$user</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066;">chown</span> <span style="color: #0000ff;">$uid</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$gid</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$vhostInfo</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'hostDir'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
	<span style="color: #000066;">chown</span> <span style="color: #0000ff;">$uid</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$gid</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$vhostInfo</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'docRoot'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    informOut<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Creating log dir: $vhostInfo{'logsDir'}&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    mkpath<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$vhostInfo</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'logsDir'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
    informOut<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Site File: $vhostInfo{'apacheConfig'}&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$vhostContent</span> <span style="color: #339933;">=</span> <span style="color: #cc0000; font-style: italic;">&lt;&lt; &quot;EOF&quot;;
&lt;VirtualHost *:80&gt;
    ServerName $vhost
    DocumentRoot $vhostInfo{'docRoot'}
    &lt;directory $vhostInfo{'docRoot'}&gt;
        Options Indexes FollowSymLinks
        AllowOverride All
        Order allow,deny
        Allow from all
    &lt;/directory&gt;
        ErrorLog $vhostInfo{'logsDir'}/error_log
        CustomLog $vhostInfo{'logsDir'}/access_log &quot;%h %l %u %t \\&quot;%r\\&quot; %&gt;s %b \\&quot;%{Referer}i\\&quot; \\&quot;%{User-agent}i\\&quot;&quot;
        LogLevel debug
&nbsp;
&nbsp;
EOF</span>
    informOut<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Creating Vhost...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">open</span> FILE<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;&gt;&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$vhostInfo</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'apacheConfig'</span><span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">or</span> <span style="color: #000066;">die</span> <span style="color: #0000ff;">$!</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">print</span> FILE <span style="color: #0000ff;">$vhostContent</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">close</span> FILE<span style="color: #339933;">;</span>
&nbsp;
    informOut<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Adding host $vhost&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">open</span> FILE<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;&gt;&gt;&quot;</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">'/etc/hosts'</span> <span style="color: #b1b100;">or</span> <span style="color: #000066;">die</span> <span style="color: #0000ff;">$!</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">print</span> FILE <span style="color: #0000ff;">$ipAddress</span> <span style="color: #339933;">.</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>&quot;</span><span style="color: #339933;">.</span> <span style="color: #0000ff;">$vhost</span> <span style="color: #339933;">.</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">close</span> FILE<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$output</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">`/usr/sbin/a2ensite $vhost`</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">print</span> <span style="color: #0000ff;">$output</span><span style="color: #339933;">;</span>
&nbsp;
    restartApache<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">#print $vhostConten t;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> restartApache
<span style="color: #009900;">&#123;</span>
    informOut<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Restarting apache&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$output</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">`/etc/init.d/apache2 restart`</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">print</span> <span style="color: #0000ff;">$output</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> deleteVhost
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$vhost</span> <span style="color: #339933;">=</span> <span style="color: #000066;">shift</span><span style="color: #339933;">;</span>
     <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">%vhostInfo</span> <span style="color: #339933;">=</span> returnVhostPaths<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$vhost</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    informOut<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Removing $vhost from hosts file&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">open</span> IN<span style="color: #339933;">,</span> <span style="color: #ff0000;">'&lt; '</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">'/etc/hosts'</span> <span style="color: #b1b100;">or</span> <span style="color: #000066;">die</span> <span style="color: #0000ff;">$!</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@hostsFile</span> <span style="color: #339933;">=</span> <span style="color: #009999;">&lt;IN&gt;</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">close</span> IN<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@contents</span> <span style="color: #339933;">=</span> <span style="color: #000066;">grep</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!/^</span>127<span style="color: #339933;">.</span>0<span style="color: #339933;">.</span>0<span style="color: #339933;">.</span>1<span style="color: #0000ff;">\t</span><span style="color: #0000ff;">$vhost</span><span style="color: #339933;">/,</span> <span style="color: #0000ff;">@hostsFile</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066;">open</span> FILE<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;&gt;&quot;</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">'/etc/hosts'</span> <span style="color: #b1b100;">or</span> <span style="color: #000066;">die</span> <span style="color: #0000ff;">$!</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">print</span> FILE <span style="color: #0000ff;">@contents</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">close</span> FILE<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$output</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">`/usr/sbin/a2dissite $vhost`</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">print</span> <span style="color: #0000ff;">$output</span><span style="color: #339933;">;</span>
&nbsp;
    informOut<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Removing  $vhostInfo{'apacheConfig'} file&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">unlink</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$vhostInfo</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'apacheConfig'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    restartApache<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot; manually remove $vhostInfo{'docRoot'}... <span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> informOut <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$message</span> <span style="color: #339933;">=</span> <span style="color: #000066;">shift</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;$message <span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://ivanvillareal.com/linux/perl-script-to-manage-virtual-hosts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

