Skip to content

Instantly share code, notes, and snippets.

@cosmomathieu
Last active March 11, 2020 13:24
Show Gist options
  • Save cosmomathieu/9c3fb1ac03f98086b4e57dfe4f4c4baa to your computer and use it in GitHub Desktop.
Save cosmomathieu/9c3fb1ac03f98086b4e57dfe4f4c4baa to your computer and use it in GitHub Desktop.
VirtualHost site creation script

VirtualHost site creation script

Create a VirtualHost via PHP CLI script

Usage

# sudo ./sitegen/cli ${siteName} ${webRoot}
sudo ./sitegen/cli mywebsitename.local /var/www
#!/usr/bin/env php
<?php
/**
* Copyright © SolutionsITW, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
if (PHP_SAPI !== 'cli') {
echo 'sitegen must be run as a CLI application';
exit(1);
}
// ------------------------------------------------------------------------
// Procedural functions
// ------------------------------------------------------------------------
function pullArguments($args) {
$final = array();
for ($i = 1; $i < count($args); $i++) {
if ($args[$i][0] == '-') {
if ($args[$i][1] == '-') {
$final[ltrim($args[$i], '-')] = null;
} else {
if (count($args) <= $i + 1 || $args[$i + 1][0] == '-') {
die('Invalid argument supplied for argument ' . $args[$i]);
}
$final[ltrim($args[$i], '-')] = $args[++$i];
}
}
}
return $final;
}
function getArgument($key, $default) {
global $args;
if (isset($args[$key])) {
return $args[$key];
}
return $default;
}
function hasArgument($key) {
global $args;
return array_key_exists($key, $args);
}
function setupController($args) {
$new_args = [];
foreach($args as $arg) {
if (strpos($arg, ':')) {
$temp = explode(':', $arg);
if (count($temp) > 2 || count($temp) <= 1) {
exit('Malformed CLI command. Type --help for more assistance.');
}
list($controller, $method) = $temp;
array_push($new_args, $controller, $method);
} else {
$new_args[] = $arg;
}
}
return $new_args;
}
function help() {
echo print_r("
Command Description Default Value
-----------------------------------------------------------------------------
Usage command [options] [arguments]
bin/app <controller> <method> <arg1> <arg2>
-----------------------------------------------------------------------------
--help Displays the help menu"
, true);
echo PHP_EOL . PHP_EOL;
exit;
}
// ------------------------------------------------------------------------
// Begin application setup
// ------------------------------------------------------------------------
$SELF = basename(__FILE__, '.php');
$BASEPATH = rtrim(str_replace([$SELF, '/bin'], '', __FILE__), '/') . '/';
unset($SELF);
$args = pullArguments($argv);
$_SERVER['argv'] = setupController($argv);
if (hasArgument('help')) {
help();
exit(1);
}
$siteName = (isset($argv[1])) ? $argv[1] : '';
$webRoot = (isset($argv[2])) ? $argv[2] : '';
$vHostTemplatePath = 'virtual-host-template.tpl';
// Run everything
try {
if (empty($siteName) || empty($webRoot)) {
throw new Exception("The sitename and/or webroot was not found", 1);
}
mkdir($webRoot . '/' . $siteName);
mkdir($webRoot . '/' . $siteName . '/log');
// TODO: Check that dirs exists
$virtualHostTemplate = file_get_contents($BASEPATH . $vHostTemplatePath);
if (empty($virtualHostTemplate)) {
throw new Exception("The virtualhost template file is empty", 1);
}
// Prepare template
$virtualHostTemplate = str_replace(
[
'${adminEmail}',
'${siteName}',
'${siteFolder}',
],
[
'[email protected]',
rtrim($siteName, '/'),
rtrim($siteName, '/'),
],
$virtualHostTemplate
);
file_put_contents('/etc/apache2/sites-available/' . $siteName . '.conf', $virtualHostTemplate);
// TODO: Run the followign shell_exec("command 2>&1"); `a2ensite` `service apache2 reload`
} catch (\Exception $e) {
while ($e) {
echo 'Autoload error: ' . $e->getMessage();
}
exit(1);
}
<VirtualHost *:80>
ServerAdmin ${adminEmail}
ServerName ${siteName}
DocumentRoot /var/www/${siteFolder}/public
ErrorLog /var/www/${siteFolder}/log/error.log
CustomLog /var/www/${siteFolder}/log/requests.log combined
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
<Directory /var/www/${siteFolder}/public>
Options -Indexes +FollowSymLinks +MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment