Skip to content

Instantly share code, notes, and snippets.

@Jan-Bart
Last active December 13, 2015 21:38
Show Gist options
  • Select an option

  • Save Jan-Bart/4978109 to your computer and use it in GitHub Desktop.

Select an option

Save Jan-Bart/4978109 to your computer and use it in GitHub Desktop.
Creates local domain names for Mamp users. (or other if you change your path) - Changes the hosts file (127.0.0.1 domain.dev) - Creates a virtual host (in your apache installation) - Creates a folder in your sites directory with an initial index.php
#!/usr/bin/php -dmemory_limit=100M
<?php
/**********************************************
* jb-vhost
*
* Creates a new domainname for your localhost
*
* @author: Jan-Bart
* @date: 20130201
* @version: 20130406
*
* Run as:
* php jb-vhost create "domainname.dev"
*
***********************************************/
if (!empty($argc) && strstr($argv[0], basename(__FILE__))) {
$function = $argv[1];
$name = $argv[2];
$f = new Functions();
switch ($function) {
case 'help':
$f->help();
break;
case 'create':
$f->create($name);
break;
case 'list':
$f->listvh();
break;
default:
echo "Something went wrong\nDid you forgot some parameters?\n";
$f->help();
break;
}
exit(0);
}
class Functions{
public $localhost = '127.0.0.1';
public $hosts_path = '/etc/hosts';
public $vhosts_path = '/Applications/MAMP/conf/apache/extra/httpd-vhosts.conf'; // MAMP
//public $vhosts_path = "/private/etc/apache2/extra/httpd-vhosts.conf";
public $www_path = '/Users/USERNAME/Sites/';
//public $restart_command = 'sudo /Applications/MAMP/bin/stop.sh'; // MAMP
public $restart_command = 'sudo /Applications/MAMP/bin/apache2/bin/apachectl restart';
// public $restart_command = 'sudo apachectl graceful';
/**
* Help
* @return shows the help
*/
function help(){
echo "*------------------------+-------------------*\n";
echo "| Local domain functions | Jan-Bart.be, 2013 |\n";
echo "*------------------------+-------------------*\n";
echo "Usage: php jb-vhost [comand] [command-arguments]\n";
echo "\n";
echo "Available commands: \n";
echo "help\t\tShows this list with available commands\n";
echo "\n";
echo "list\t\tList al your current vhosts with \$php func.php list \n";
echo "\n";
echo "create <site>\tCreate a new VirtualHost domain\n";
echo "\t\te.g.: \$sudo php func.php create \"domain.dev\" \n";
echo "\n";
}
/**
* List all current virtual hosts in the virtual hosts file
* @return String a list with all current virtual hosts
*/
function listvh(){
$file = fopen($this->vhosts_path, 'r') or exit("Unable to open VirtualHost file!"."\n");
$data = '';
while (!feof($file)) {
$data.= fgetss($file);
}
fclose($file);
echo "----------------------\n";
// Create a better list
if(preg_match_all("/ServerName(.*)\s/",$data,$match)) {
//var_dump($match[1]);
$i=0;
foreach ($match[1] as $m) {
if($i!=0){
echo $i.". ".trim($m)."\n";
}
$i++;
}
}
}
/**
* Create | Create a new local domainname
*
* @param String $name The name you want to give your new domain.
* Ending on .dev
*/
function create($name) {
//-------------------------------------------------
// Write to the hosts file
//-------------------------------------------------
$file = fopen($this->hosts_path, "a") or exit("Unable to open the hosts file!"."\n"); // A append
$hostname = "\n".$this->localhost." ".$name;
fwrite($file, $hostname);
fclose($file);
//-------------------------------------------------
// Write to the vhosts file
//-------------------------------------------------
$file = fopen($this->vhosts_path, "a") or exit("Unable to open virtual hosts file!"."\n"); // A append
$vhost = $this->localhost." ".$name."\n";
$vhost = "\n"."<VirtualHost *:80>
ServerAdmin webmaster@".$name."
DocumentRoot ".$this->www_path.$name."
ServerName ".$name."
ServerAlias www.".$name."
</VirtualHost>"."\n";
fwrite($file, $vhost);
fclose($file);
//-------------------------------------------------
// Create folder with initial file
//-------------------------------------------------
try {
$indexContent = <<<EOD
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<!-- Always force latest IE rendering engine (even in intranet) & Chrome Frame
Remove this if you use the .htaccess -->
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<title>$name</title>
<meta name="description" content="" />
<meta name="author" content="" />
<meta name="viewport" content="width=device-width; initial-scale=1.0" />
<!-- Replace favicon.ico & apple-touch-icon.png in the root of your domain and delete these references -->
<link rel="shortcut icon" href="/favicon.ico" />
<link rel="apple-touch-icon" href="/apple-touch-icon.png" />
</head>
<body>
<p>
This is a placeholder for $name
</p>
</body>
</html>
EOD;
// Create folder
mkdir($this->www_path.$name, 0755);
// Create file
$file = fopen($this->www_path.$name."/index.php", 'x')or die("can't write file");
fwrite($file, $indexContent);
fclose($file);
echo "Created default files";
}catch(exception $e){
echo $e->getMessage();
}
echo "\n";
echo "New domain succesfull created\n";
//-------------------------------------------------
// Restart MAMP
//-------------------------------------------------
echo "Restart Server\n";
exec($this->restart_command);
echo "Done!\n";
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment