Domeinnamen voor localhost aanmaken

Elke keer opnieuw als ik een domeinnaam toevoeg aan m’n localhost dacht ik: hier moet ik echt eens een scriptje voor schrijven.
Nu ik de laatste tijd nogal veel domeinnamen moest toevoegen is het er eindelijk van gekomen.

Moest je ooit met hetzelfde probleem geconfronteerd worden, aarzel dan niet om onderstaand scriptje te gebruiken.

Let wel dat je in apache httpd-vhosts.conf moet includen en dat je je paden nog moet aanpassen.
Ohja, ik heb het enkel getest op een Mac met Mamp, maar in theorie zou het ook bruikbaar moeten zijn in Linux.

Voor ik het script run sluit ik Mamp (Apache) af. Ik run het script en daarna start ik Mamp (Apache) terug op.
Waarschijnlijk hoef je Mamp/Apache niet volledig af te sluiten maar goed, lang duurt het niet en het kan geen kwaad.

#!/usr/bin/php -dmemory_limit=100M
<?php
 /**********************************************
  * func.php
  *
  * Creates a new domainname for your localhost
  *
  * @author: Jan-Bart
  * @date: 20130201
  *
  * Run as:
  * sudo php func.php "domainname.dev"
  *
***********************************************/

$localhost = '127.0.0.1'; // Your localhost. Could be localhost too I guess
$hosts_path = '/etc/hosts'; // Location of hosts
$vhosts_path = '/Applications/MAMP/conf/apache/extra/httpd-vhosts.conf'; // Location of vhosts.conf
$www_path = '/Users/USERNAME/Sites/'; // Place where you place your files

$name = $argv[1];


//-------------------------------------------------
// Write to the hosts file
//-------------------------------------------------
$file = fopen($hosts_path, a) or exit("Unable to open file!"); // A = append
$hostname = "\n".$localhost." ".$name."\n";
fwrite($file, $hostname);
fclose($file);


//-------------------------------------------------
// Write to the vhosts file
//-------------------------------------------------
$file = fopen($vhosts_path, a) or exit("Unable to open file!");
$vhost = $localhost." ".$name."\n";


$vhost = "\n"."<VirtualHost *:80>
    ServerAdmin webmaster@".$name."
    DocumentRoot ".$www_path.$name."
    ServerName ".$name."
    ServerAlias www.".$name."
</VirtualHost>"."\n";


fwrite($file, $vhost);
fclose($file);


exit(0);

?>

Edit 18 feb 2012: Laatst nieuwe versie:

Leave a reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.