Skip to content

Instantly share code, notes, and snippets.

@jprieton
Last active September 22, 2016 17:24
Show Gist options
  • Save jprieton/949213e72775bf91122e6c48b76b66e5 to your computer and use it in GitHub Desktop.
Save jprieton/949213e72775bf91122e6c48b76b66e5 to your computer and use it in GitHub Desktop.
<?php
/**
* CLI for create a virtualhost in Wamp
*/
if ( empty( $argv[1] ) ) {
echo "A vhost name is required";
return false;
}
$vhost = $argv[1];
// Update httpd-vhosts.conf file
$vhost_config = '/wamp/bin/apache/apache2.4.9/conf/extra/httpd-vhosts.conf';
$vhost_snippet = <<<VHOST
<VirtualHost {vhost}:80>
ServerAdmin [email protected]
DocumentRoot "C:\wamp\\vhosts\{vhost}"
ServerName {vhost}
ErrorLog "logs/{vhost}.log"
CustomLog "logs/{vhost}.log" common
</VirtualHost>
#
<VirtualHost _default_:80>
VHOST;
$_vhost = str_replace( '{vhost}', $vhost, $vhost_snippet );
$old_vhost = file_get_contents( $vhost_config );
$new_vhost = str_replace( "<VirtualHost _default_:80>", $_vhost, $old_vhost );
file_put_contents( $vhost_config, $new_vhost );
// Update hosts file
$hosts_file = '/Windows/System32/drivers/etc/hosts';
$hosts_snippet = <<<HOSTS
127.0.0.1 localhost
127.0.0.1 {vhost}
HOSTS;
$_hosts = str_replace( '{vhost}', $vhost, $hosts_snippet );
$old_hosts = file_get_contents( $hosts_file );
$new_hosts = str_replace( "127.0.0.1 localhost", $_hosts, $old_hosts );
file_put_contents( $hosts_file, $new_hosts );
if ( !file_exists( "/wamp/vhosts/{$vhost}" ) ) {
mkdir( "/wamp/vhosts/{$vhost}", 0777, true );
touch( "/wamp/vhosts/{$vhost}/index.html" );
}
echo 'Ready. You must restart the apache server';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment