Et php script, som dog trænger til finpudsning, til at oprette vhosts i din httpd.conf:
<?php
if($_SERVER["REMOTE_ADDR"] == "127.0.0.1") {
// Sti til httpd.conf
$httpd_conf_sti = "C:/Apache/conf/httpd.conf";
// Sti til filen hosts
$hosts_sti = "C:/WINDOWS/system32/drivers/etc/hosts";
// Stien til apache.exe For apache2 ligger den i bin, for version 1.3.* ligger den i Apache
// Skal bruges til restart af apache, efter oprettelse af vhost
$apache_exe_sti = "C:/Apache/";
// Evt. sti til hvor alle hosts er placeret
// Bruges kun som "startværdi" i tekstfeltet for "DocumentRoot"
$default_path = "C:/sites/";
$safe = ini_get('safe_mode');
if(isset($_POST["host_navn"])) {
if(!is_dir($_POST["host_path"])) {
mkdir($_POST["host_path"]);
}
$newhost_name = trim($_POST["host_navn"]);
$newhost_name = str_replace(" ", "", $newhost_name);
$conf_str = "\n# ". strtoupper($newhost_name) ."\n";
$conf_str .= "<VirtualHost *>\n";
$conf_str .= "ServerName ". $newhost_name ."\n";
$conf_str .= "DocumentRoot \"". trim($_POST["host_path"]) ."\"\n";
if(!empty($_POST["host_param"])) {
$conf_str .= stripslashes($_POST["host_param"]) ."\n";
}
$conf_str .= "</VirtualHost>\n";
$conf = fopen($httpd_conf_sti, "at");
if ($conf) {
fputs($conf, $conf_str);
fclose($conf);
}
// Skrive til hosts fil
$host_str = "\n127.0.0.1 ". $newhost_name;
$to_hostsfil = fopen($hosts_sti, "at");
if ($to_hostsfil) {
fputs($to_hostsfil, $host_str);
fclose($to_hostsfil);
}
// Restarter apache. Virker kun hvis safe_mode er Off i php.ini
$exe_sti = explode("conf/httpd.conf", $httpd_conf_sti);
$true_exesti = str_replace("/", "\\", $exe_sti[0]);
exec($true_exesti ."Apache.exe -k restart");
header("Location:". $_SERVER["PHP_SELF"] ."");
exit();
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><title>Vhosts</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<style type="text/css">
body {font-family: arial, verdana; font-size: 0.9em; margin: 0px; color: black; background-color: white;}
td, th {font-family: arial, verdana; font-size: 0.9em; color: black; background-color: white;}
a:link {color: #404040; text-decoration: underline;}
a:visited {color: #404040; text-decoration: underline;}
a:hover {color: #4f0000; text-decoration: none;}
.input {color: black; background-color: #ffffff; font-family: arial; font-size: 0.9em; border-color: black; border-width: 1px;}
.submit {font-size: 0.8em; border-width: 1px; border-color: black;}
.green {background-color: #006600; padding: 3px; color: #fefae4; font-size: 1.4em; border-bottom: solid #C0C0C0; border-width: 6px;}
.small {font-size: 0.8em;}
.error {color: #C00000; font-weight: bold;}
</style>
</head>
<body>
<center>
<div class="green">Hosts i Apache</div>
<br />
<?php
if(is_file($httpd_conf_sti)) { // findes httpd.conf?
if(is_file($hosts_sti)) { // findes filen hosts?
echo"<form action=\"". $_SERVER["PHP_SELF"] ."\" method=\"post\">\n\n";
$all_hosts = array();
$fil = file($httpd_conf_sti);
// Finder hosts i httpd.conf
for($i = 0; $i < count($fil); $i++) {
$fil[$i] = trim($fil[$i]);
if(substr($fil[$i], 0, 10) == "ServerName") {
$all_hosts[] = trim(strtolower(substr($fil[$i], 10)));
}
}
// Fjerner evtuelle "duplikater"
$hosts = array_unique($all_hosts);
sort($hosts);
// Hvis der var "duplikater", gi' dem en korrekt "nøgle" igen ( 0=>host1, 1=>host2 )
$b = 0;
foreach($hosts as $value) {
$host_name[$b] = $value;
$b++;
}
$ialt = count($host_name);
//Tjekker om hostnavne i $host_name også findes i hosts filen.........
$ok_name = array();
$nyfile = file_get_contents($hosts_sti);
foreach($host_name as $tjek_hostfil_name) {
$pos = strpos(strtolower($nyfile), $tjek_hostfil_name);
if($pos !== false) {
$ok_name[] = $tjek_hostfil_name;
}
}
// Indeholder evtuelle navne som findes i httdp.conf men ikke i filen hosts
$name_result = array_diff($host_name, $ok_name);
echo"<table cellpadding=\"12\"><tr>\n";
echo" <td valign=\"top\" class=\"small\"><b>". $ialt ." hosts i din conf</b>";
if($ialt > 0) {
echo"<table cellpadding=\"4\"><tr>\n";
for($c = 0; $c < $ialt; $c++) {
echo"</tr><tr>\n";
echo" <td><a href=\"
http://". $host_name[$c] ."/\">". $host_name[$c] ."</a></td>\n";
}
echo"</tr></table><br />\n";
}
if(count($name_result) !=0) {
echo"<span class=\"error\">Fejl i hosts fil:</span>";
foreach($name_result as $navn) {
echo"<br />\n";
echo $navn ." findes ikke";
}
}else{
echo"Alle hostnavne findes i hosts fil!";
}
echo"<br /><br /><a href=\"view-source:
file://". $hosts_sti ."\">Edit hostfil</a></td>\n\n";
echo" <td valign=\"top\" class=\"small\"><b>Ny vhost:</b><br />\n";
echo"Navn: <small>(f.eks. nythost.localhost)</small><br />\n";
echo"<input type=\"text\" name=\"host_navn\" class=\"input\" size=\"24\"><br />\n";
echo"Sti til DocumentRoot: <small>(f.eks. c:/www/nythost)</small><br />\n";
echo"<input type=\"text\" name=\"host_path\" class=\"input\" size=\"40\" value=\"". $default_path ."\"><br />\n";
echo"Evt. tilføjelser: <small>(f.eks. error_log m.m.)</small><br />\n";
echo"<textarea name=\"host_param\" class=\"input\" cols=\"48\" rows=\"8\"></textarea><center><br />\n";
echo"<input type=\"submit\" value=\"Opret vhost\" class=\"submit\">\n";
echo"<br /><br /><a href=\"view-source:". $httpd_conf_sti ."\">Edit httpd.conf</a>";
if(!empty($safe)) {
echo"<br /><br />\nSafe_mode er On, så restart Apache manuelt!";
}
echo"</center></td>\n";
echo"</tr></table>\n\n";
echo"</form>";
}else{
echo"<br /><span class=\"error\">Fejl i stien til din hosts-fil!</span><br />\n";
}
}else{
echo"<br />\n<span class=\"error\">Fejl i stien til din httpd.conf!</span><br />\n";
}
echo $_SERVER["SERVER_SOFTWARE"];
?>
<br />
Scriptet er lavet med udgangspunkt i <a href="
http://www.eksperten.dk/artikler/218">http://www.eksperten.dk/artikler/218</a></center>
</body></html>
<?php
}
?>