Apache virtual host script in perl
This is a very short post about a little script I did to create remove virtual hosts easily.
I’ve found a couple of scripts (http://patrickgibson.com/utilities/virtualhost/, http://www.tamdenholm.com/portfolio/apache) to do this but they didn’t worked right, I’m using Ubuntu Karmic, maybe thats why. However instead of fixing those scripts I went to my vim editor and started typing this, I’ve spent about 2 hours writing this while I was watching a crappy tv program, but in the end it worked, it surely contains some bugs, or it is unsafe to run it, I just wanted to share this.
#!/usr/bin/perl -w use strict; use File::Path qw(mkpath rmtree); use Getopt::Long; our $ipAddress = '127.0.0.1'; our $apacheConfigFile = 'apache2.conf'; our $apacheConfigDir = '/etc/apache2'; our $sitesAvailable = 'sites-available'; our $docRootPrefix = '/home/ivan/www'; our $docRoot = 'public_html'; our $logsDir = 'logs'; my $del = ''; my $add = ''; my $domain = ''; unless (GetOptions ('del' => \$del, 'add' => \$add, 'domain=s' => \$domain) or usage()) { usage();}#print $paramResults; if ($add || $del) { if ($domain) { if ($add) { createVhost($domain); } elsif ($del) { deleteVhost($domain);} } else { usage();}#print 'add aqui'; } else { usage();} sub usage { print < <USAGEThis program will add or removeapache virtual hosts on ubuntu karmic 9.10 usage: vhost-updater.pl [--add | --del] --domain newhost.tld USAGE}sub returnVhostPaths{ my $vhost = shift; my @dir = split(/\//, $docRootPrefix); my %res; push(@dir, $vhost); my $hostDir = join('/', @dir); $res{'docRoot'} = $hostDir . '/' . $docRoot; $res{'logsDir'} = $hostDir . '/' . $logsDir;#todo dir validation @dir = split(/\//, $apacheConfigDir); push(@dir, $sitesAvailable); push(@dir, $vhost); $res{'apacheConfig'} = join('/', @dir); return %res;} sub createVhost { my $vhost = shift;#first create the docRoot my %vhostInfo = returnVhostPaths($vhost); informOut("Creating docroot dir: $vhostInfo{'docRoot'}"); informOut("Creating log dir: $vhostInfo{'logsDir'}"); mkpath($vhostInfo{'docRoot'}, $vhostInfo{'logsDir'}); informOut("Site File: $vhostInfo{'apacheConfig'}"); my $vhostContent = << "EOF";<VirtualHost *:80>ServerName $vhostDocumentRoot $vhostInfo{'docRoot'}<directory $vhostInfo{'docRoot'}>Options Indexes FollowSymLinksAllowOverride AllOrder allow,denyAllow from all</directory>ErrorLog $vhostInfo{'logsDir'}/error_logCustomLog $vhostInfo{'logsDir'}/access_log "%h %l %u %t \\"%r\\" %>s %b \\"%{Referer}i\\" \\"%{User-agent}i\\""LogLevel debugEOF informOut("Creating Vhost..."); open FILE, ">", $vhostInfo{'apacheConfig'} or die $!; print FILE $vhostContent; close FILE; informOut("Adding host $vhost"); open FILE, ">>", '/etc/hosts' or die $!; print FILE $ipAddress ."\t". $vhost ."\n"; close FILE; my $output = `/usr/sbin/a2ensite $vhost`; print $output; restartApache();#print $vhostConten t;}sub restartApache{ informOut("Restarting apache"); my $output = `/etc/init.d/apache2 restart`; print $output;}sub deleteVhost{ my $vhost = shift; my %vhostInfo = returnVhostPaths($vhost); informOut("Removing $vhost from hosts file"); open IN, '< ', '/etc/hosts' or die $!; my @hostsFile = <IN>; close IN; my @contents = grep(!/^127.0.0.1\t$vhost/, @hostsFile); open FILE, ">", '/etc/hosts' or die $!; print FILE @contents; close FILE; my $output = `/usr/sbin/a2dissite $vhost`; print $output; informOut("Removing $vhostInfo{'apacheConfig'} file"); unlink($vhostInfo{'apacheConfig'}); restartApache(); print " manually remove $vhostInfo{'docRoot'}... \n";} sub informOut { my $message = shift; print "$message \n";}
Here is a screenshot of the program running
