Skip to content

Instantly share code, notes, and snippets.

@oranj
Created March 7, 2014 23:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oranj/9422718 to your computer and use it in GitHub Desktop.
Save oranj/9422718 to your computer and use it in GitHub Desktop.
Create a vhost config and add an entry to /etc/hosts, then restart apache.
#! /usr/bin/php
<?php
function bad_usage($extra = null, $status = 1) {
if (! is_null($extra)) {
echo "Error: $extra\n\n";
}
echo "Usage: sudo vhost \$directory \$domain\n";
die($status);
}
$me = trim(shell_exec("whoami"));
if ($me != "root") {
bad_usage("You'll probably want to run that as root.");
}
if ($argc < 3) {
bad_usage("Too few arguments");
}
$dir = $argv[1];
$domain = $argv[2];
if (! is_dir($dir)) {
if (is_file($dir)) {
bad_usage("\$directory must be a directory");
}
echo "$dir does not exist. Would you like to create it? [Y|n] ";
$response = trim(strtolower(fgets(STDIN)));
if ($response == 'y' || $response == '') {
mkdir($dir);
} else {
die(10);
}
}
$dir = realpath ($dir);
$vhost_filename = sprintf("/etc/apache2/other/%s-vhost.conf", preg_replace('/[^a-z]/i', '_', basename($dir)));
$vhost_file = fopen($vhost_filename, "w");
fputs($vhost_file, "<VirtualHost _default_:80>\n\tServerName $domain\n\tDocumentRoot $dir\n</VirtualHost>");
shell_exec("echo \"#$dir\n127.0.0.1\t$domain\n\" >> /etc/hosts");
shell_exec("sudo apachectl restart");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment