<?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/ubuntu/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>
		<item>
		<title>Ubuntu 9.10 on an hp mini 110-1125nr</title>
		<link>http://ivanvillareal.com/linux/ubuntu-9-10-on-an-hp-mini-110-1125nr/</link>
		<comments>http://ivanvillareal.com/linux/ubuntu-9-10-on-an-hp-mini-110-1125nr/#comments</comments>
		<pubDate>Wed, 27 Jan 2010 00:56:14 +0000</pubDate>
		<dc:creator>Ivan Villareal</dc:creator>
				<category><![CDATA[Hardware]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[netbook]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://ivanvillareal.com/?p=148</guid>
		<description><![CDATA[I got this new mini 110, it came with Windows 7 Starter, and after a day of using it I was very frustrated because it was very limiting, for example, [...]]]></description>
			<content:encoded><![CDATA[<p>I got this new mini 110, it came with Windows 7 Starter, and after a day of using it I was very frustrated because it was very limiting, for example, I was unable to change my Desktop background, the network management is confusing and frustrating, and it had a lot of crapware, except for the hp games, that some of them are fun.</p>
<p>Anyway, right now I don&#8217;t have time to spend getting a new OS on this machine, I just want something that works, I don&#8217;t plan using this to work, but I would like to have some of the tools I use into this.</p>
<p>So my options were, trying Ubuntu remix or Windows Xp, after checking that hp had the drivers for XP, I was going to change Windows 7 to Xp, but I was reading how to create a USB boot drive, and it was too much hassle for me. (I&#8217;m very lazy for any Microsoft stuff).</p>
<p>My next option was Ubuntu remix, I&#8217;m not a big fan of Ubuntu, but I wanted something to just worked out of the box, also this little netbook comes with splashtop, a really nice feature for checking stuff online fast, and I didn&#8217;t want to loose this so I really didn&#8217;t knew how well this was going to work.</p>
<p>I started downloading Ubuntu 9.10 Karmic yesterday night, just in case, Today after more frustrating things, I decided to give Karmic a try.</p>
<p>I grabbed my usb pendrive, a 1gb &nbsp;kingston traveler, mounted the iso and ran usbinst.exe, it was pretty straightforward, my only advice to you would be to avoid saving space for your documents in the usb, my first try I set this to 30Mb and the boot failed, so I set the do not save feature.</p>
<p>It took about 40 minutes to finish copying the files, after it was done, I restarted the netbook with the pendrive connected, and at the first boot screen I pressed F9 key for booting options, selected the pendrive and that was it.</p>
<p>After testing ubuntu from the pendrive, it ran pretty well and everything but the wifi worked, I did an installation, keeping the windows partition and the hp restore partition, just in case.</p>
<p>The installation was pretty smooth, no issues at all, after the first boot I just installed the broadcom drivers, it was pretty easy with the driver manager in ubuntu, and the wifi was working</p>
<p>Update: My hp mini was stolen by a couple of elderly people, I saw the video <img src='http://ivanvillareal.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> , so I&#8217;m unable to play more with this, but the time I had this I was very happy with it, it performed well.</p>
]]></content:encoded>
			<wfw:commentRss>http://ivanvillareal.com/linux/ubuntu-9-10-on-an-hp-mini-110-1125nr/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>File Type Detection in PHP</title>
		<link>http://ivanvillareal.com/linux/file-type-detecion-in-php/</link>
		<comments>http://ivanvillareal.com/linux/file-type-detecion-in-php/#comments</comments>
		<pubDate>Fri, 25 Dec 2009 04:47:24 +0000</pubDate>
		<dc:creator>Ivan Villareal</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[fileinfo]]></category>
		<category><![CDATA[pecl]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://ivanvillareal.com/?p=150</guid>
		<description><![CDATA[I&#8217;m working on a project where the users can upload files, and I need to know the type of file they are uploading, a simple solution would be to check [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m working on a project where the users can upload files, and I need to know the type of file they are uploading, a simple solution would be to check the extensions of the files, but this has many flaws, so I was looking for a reliable way to detect the type of file, and I found a PECL extension called <a href="http://pecl.php.net/package/Fileinfo/php-src/ext/fileinfo">file info</a> this extension is enabled by default in PHP 5.3, unfortunately I&#8217;m runing PHP 5.2.10, and the prod server also has this version.</p>
<p>So what I did to install this on ubuntu 9.10 karmic was the following:</p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">root<span style="color: #000000; font-weight: bold;">@</span>mini:<span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>php5<span style="color: #000000; font-weight: bold;">/</span>apache2<span style="color: #666666; font-style: italic;"># apt-get install php5-dev libmagic-dev php-pear</span>
....
root<span style="color: #000000; font-weight: bold;">@</span>mini:<span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>php5<span style="color: #000000; font-weight: bold;">/</span>apache2<span style="color: #666666; font-style: italic;"># pear channel-update pear.php.net</span>
...
root<span style="color: #000000; font-weight: bold;">@</span>mini:<span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>php5<span style="color: #000000; font-weight: bold;">/</span>apache2<span style="color: #666666; font-style: italic;"># pecl install Fileinfo</span>
....
Build process completed successfully
Installing <span style="color: #ff0000;">'/usr/lib/php5/20060613+lfs/fileinfo.so'</span>
<span style="color: #c20cb9; font-weight: bold;">install</span> ok: channel:<span style="color: #000000; font-weight: bold;">//</span>pear.php.net<span style="color: #000000; font-weight: bold;">/</span>Fileinfo-1.0.4</pre></div></div>

<p>&nbsp;</p>
<p>and finally I added the extension to php.ini</p>
<p>extension=fileinfo.so</p>
<p>I restarted the apache server, and tested with this</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$fileInfo</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> finfo<span style="color: #009900;">&#40;</span>FILEINFO_MIME<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #000088;">$mimeType</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$fileInfo</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">buffer</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$uploadedFile</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://ivanvillareal.com/linux/file-type-detecion-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

