Quick vhost script

Posted on Thu 01 March 2012 in Tech

Quick script to setup a new virtualhost entry for apache. Works for me on archlinux.

#!/usr/bin/python
 import sys, os, subprocess

vhosts_file = "/etc/httpd/conf/extra/httpd-vhosts.conf"
 hosts_file = "/etc/hosts"

domain_name=sys.argv[1]
 folder=sys.argv[2]

def usage():

if (len(sys.argv) != 3):
 usage()
 sys.exit()

currentUser = subprocess.getoutput("whoami")
 if currentUser != 'root':
 print("You need to be root!")
 sys.exit()

if not os.path.exists(vhosts_file):
 print("Vhosts file doesn't exist, exiting")
 sys.exit()

if not os.path.exists(hosts_file):
 print("Hosts file doesn't exist, exiting")
 sys.exit()

if not os.path.exists(folder):
 print("Web folder doesn't exist, attempting to create")
 os.makedirs(folder)

vhost_entry = "nnntServerAdmin your@%sntDocumentRoot "%s"ntServerName %sntServerAlias %s ntnttDirectoryIndex index.php index.htm index.htmlnttAddHandler cgi-script .cgi .plnttOptions ExecCGI Indexes FollowSymLinks MultiViews +IncludesnttAllowOverride NonenttOrder allow,denynttallow from allntn" % (domain_name, folder, domain_name, domain_name, folder)

vhosts_file.write(vhost_entry)

host_entry = "n127.0.0.1 %sn" % domain_name

vhosts_file.write(host_entry)

subprocess.call('/etc/rc.d/httpd restart', shell=True)