I do a lot and often virtual hosts. That's why I did alongside the script and I put to Wamp. The script looks at hosts founded in file C:\Windows\System32\drivers\etc\hosts ", orders them and removes duplicate names. Interact with the console asks for the host name and inserts it with the ip adersem the same as localhost (127.0.0.1).
Then the script asks for the host directory, email the Admin, the index file and inserts the file "" settings of virtual hosts and their settings directories.
If someone wants to can easily add a list of hosts as:
1. jump to a folder (shell execute)
2. open the host by default browser
Note: copy and run at your own risk, for me in win7 is OK.
Script : C:\wamp\scripts\addVHost.php
<?php
$hostemail="johndoe@mail.com";
$dindex=array("DirectoryIndex","index.php" ;
start_again:
$path=$_SERVER['SystemRoot'].'\\System32\\drivers\\etc\\hosts';
$vpath="C:\\wamp\\vhosts\\httpd-vhosts.conf";
$vhostStr = file_get_contents($path) ;
$lines = preg_split("/[\r?\n]+/",$vhostStr) ;
$hosts = array() ;
$unique = array() ;
$ipV4Patt ="(\b(??:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b)";
$ipV6Patt ="(((\b[\da-fA-F]{1,4}{7}|:{2,7})([\da-fA-F]{1,4}))";
$hostPatt="([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?";
$virtualHostData ="
<VirtualHost *:80>
ServerName {{hostname}}
DocumentRoot \"{{folder}}\"
{{directory_index}}
ServerAdmin {{admin_email}}
ErrorLog \"logs/{{hostname}}.log\"
CustomLog \"logs/{{hostname}}-access.log\" common
</VirtualHost>
<Directory \"{{folder}}\">
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
Allow from all
</Directory>
";
foreach($lines as $line) {
if (preg_match("/^\s*\#/",$line)) continue;
if (preg_match("%^\s*(?<ip>($ipV6Patt)||($ipV4Patt))\s+(?<host>$hostPatt)%",$line,$match)) {
$hosts[$match['host']]=$match['ip'];
$unique[$match['host']]=$match['ip'];
}
}
$localip="127.0.0.1";
$str="";
foreach($lines as $line) {
if (preg_match("/^\s*\#/",$line)) $str .= $line."\n";
if (preg_match("%^\s*(?<ip>($ipV6Patt)||($ipV4Patt))\s+(?<host>$hostPatt)%",$line,$match)) {
if (isset ($hosts[$match['host']])) {
$str .= "\t".$match['ip']."\t".$match['host']."\n";
if (trim($match['host'])=="localhost" $localip=trim($match['ip']) ;
unset ($hosts[$match['host']]) ;
}
}
}
echo "
virtual host be create on addres : $localip
input name of new virtual host (eg) : www.my.zone >";
$newVirtHost = strtolower (trim(fgets(STDIN))) ;
$message = "";
if (! preg_match("%^($hostPatt)$%",$newVirtHost)) $message="Host name is not correct\n";
if (isset ($unique[$newVirtHost])) $message .= "Host is not unique (hostname conflict!)\n";
if ($message =="" {
$str .= "\t$localip\t$newVirtHost\n";
echo "
",$str;
echo "
If this list is good, and to be saved, enter ([no] or yes):>";
$save = trim(fgets(STDIN)) ;
if (strtolower($save)=="yes" {
if (file_put_contents($path,$str)) {
echo $message,"\nVHost is saved,Press Enter to continue...";
trim(fgets(STDIN)) ;
$vpath="C:\\wamp\\vhosts\\httpd-vhosts.conf";
$virtualStr = file_get_contents($vpath) ;
$appdata=$_SERVER['APPDATA']."\\$newVirtHost";
echo "
to complette input data for real path on virtual host : $newVirtHost
input folder name to this host (eg) : $appdata >>";
$result= (trim(fgets(STDIN))) ;
$newFolder = $result=="" ? $appdata : $result;
if(!is_dir($newFolder)) {
echo "The Directory {$newFolder} not exists, you want to create it ? Press <yes> to create >";
$message="";
$result= strtolower(trim(fgets(STDIN))) ;
if ($result=='yes') {
if (! mkdir($newFolder , 0777)) {
$message="Error Create Folder: $newFolder \n";
goto toend;
} else $message.="Succesfully, Create Folder: $newFolder \n";
}
}
echo "
input host admin email (eg) : <$hostemail>>";
$result= (trim(fgets(STDIN))) ;
$newEmail = $result=="" ? $hostemail : $result;
echo "
input file name for Directory Index (eg) : <$dindex[1]>>";
$dindex[1]= (trim(fgets(STDIN))) ;
$newDirIndex = $dindex[0]." ".$dindex[1];
$virtualHostData = preg_replace(array("/{{hostname}}/","/{{folder}}/","/{{admin_email}}/","/{{directory_index}}/",
array($newVirtHost,$newFolder,$newEmail,$newDirIndex),
$virtualHostData) ;
$virtualStr .=$virtualHostData;
if (file_put_contents($vpath,$virtualStr)) {
$message .="You Have New VirtualHost: [$newVirtHost]
DocumentRoot = $newFolder
";
}
goto toend;
}
}
}
else {
goto toend;
}
toend:
echo "$message\n\nPress Enter to exit, Any Key then Again";
$result = trim(fgets(STDIN)) ;
if ($result=="" exit; else goto start_again;
?>
fragments to add to WAMP menu:change file : C:\wamp\wampmanager.ini
[apacheMenu]
;WAMPAPACHEMENUSTART
Type: submenu; Caption: "Version"; SubMenu: apacheVersion; Glyph: 3
Type: submenu; Caption: "Service"; SubMenu: apacheService; Glyph: 3
Type: submenu; Caption: "Moduly Apache"; SubMenu: apache_mod; Glyph: 3
Type: submenu; Caption: "Apache Virtual Host"; SubMenu: vhost_add; Glyph: 3
Type: submenu; Caption: "Katalog aliasów"; SubMenu: alias_dir; Glyph: 3
Type: item; Caption: "httpd.conf"; Glyph: 6; Action: run; FileName: "C:/Program Files/notepad++.exe"; parameters:"c:/wamp/bin/apache/apache2.4.9/conf/httpd.conf"
Type: item; Caption: "Apache error log"; Glyph: 6; Action: run; FileName: "C:/Program Files/notepad++.exe"; parameters:"c:/wamp/logs/apache_error.log"
Type: item; Caption: "Apache access log"; Glyph: 6; Action: run; FileName: "C:/Program Files/notepad++.exe"; parameters:"c:/wamp/logs/access.log"
;WAMPAPACHEMENUEND
the same changes in file : C:\wamp\wampmanager.tpl
[apacheMenu]
;WAMPAPACHEMENUSTART
Type: submenu; Caption: "Version"; SubMenu: apacheVersion; Glyph: 3
Type: submenu; Caption: "Service"; SubMenu: apacheService; Glyph: 3
Type: submenu; Caption: "${w_apacheModules}"; SubMenu: apache_mod; Glyph: 3
Type: submenu; Caption: "Apache Virtual Host"; SubMenu: vhost_add; Glyph: 3
Type: submenu; Caption: "${w_aliasDirectories}"; SubMenu: alias_dir; Glyph: 3
Type: item; Caption: "httpd.conf"; Glyph: 6; Action: run; FileName: "${c_editor}"; parameters:"${c_apacheConfFile}"
Type: item; Caption: "${w_apacheErrorLog}"; Glyph: 6; Action: run; FileName: "${c_editor}"; parameters:"${c_installDir}/${logDir}apache_error.log"
Type: item; Caption: "${w_apacheAccessLog}"; Glyph: 6; Action: run; FileName: "${c_editor}"; parameters:"${c_installDir}/${logDir}access.log"
;WAMPAPACHEMENUEND
and in file : C:\wamp\wampmanager.tpl
[alias_dir]
;WAMPALIAS_DIRSTART
Type: separator; Caption: "${w_aliasDirectories}"
Type: item; Caption: "${w_adddAlias}"; Action: multi; Actions: add_alias;Glyph : 1
Type: separator
;WAMPADDALIAS
;WAMPALIAS_DIREND
[vhost_add]
;WAMPALIAS_DIRSTART
Type: separator; Caption: "VIRTUAL HOST"
Type: item; Caption: "AddVHOST"; Action: multi; Actions: add_vhost;Glyph : 1
Type: separator
regards Andrzej Kmicic