Skip to content

Instantly share code, notes, and snippets.

@f1code
Created November 24, 2015 15:50
Show Gist options
  • Select an option

  • Save f1code/b858db38dc4d5a960dc4 to your computer and use it in GitHub Desktop.

Select an option

Save f1code/b858db38dc4d5a960dc4 to your computer and use it in GitHub Desktop.
Create new virtualhost in WAMP
import sys, os, errno
from subprocess import call
HOSTS_FILE = "C:\\Windows\\System32\\drivers\\etc\hosts"
VHOSTS_FILE = "C:\\wamp\\bin\\apache\\apache2.4.9\\conf\\extra\\httpd-vhosts.conf"
WAMP_ROOT = "C:\\wamp\\www"
APACHE_SERVICE_NAME = "wampapache64"
def add_host_name(hostname):
text = "127.0.0.1 %(hostname)s %(hostname)s.local\n" % \
{ "hostname": hostname }
with open(HOSTS_FILE, "a") as f:
f.write("127.0.0.1 " + hostname + "\n")
def add_vhost(path, hostname):
path = path.replace("\\", "/")
text = """
<VirtualHost *>
DocumentRoot "%(path)s/web"
ServerName %(host)s.local
ErrorLog "%(path)s/logs/error.log"
CustomLog "%(path)s/logs/access.log" common
</VirtualHost>
""" % { "path": path, "host": hostname }
with open(VHOSTS_FILE, "a") as f:
f.write(text)
def mkdir_p(path):
try:
os.makedirs(path)
except OSError as exc:
if exc.errno == errno.EEXIST and os.path.isdir(path):
pass
else:
raise
def create_path(path):
mkdir_p(path + "\\logs")
mkdir_p(path + "\\web")
def get_host_name():
print "Host name: "
return sys.stdin.readline().rstrip()
def restart_apache():
call(["net", "stop", APACHE_SERVICE_NAME])
call(["net", "start", APACHE_SERVICE_NAME])
host = get_host_name()
if host != "":
path = WAMP_ROOT + "\\" + host
create_path(path)
add_vhost(path, host)
add_host_name(host)
restart_apache()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment