Quick vhost script

Thu 01 March 2012

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():
print("Usage: add_vhost url_to_add /folder/to/point/to")

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)

with open(vhosts_file, "a") as vhosts_file:
vhosts_file.write(vhost_entry)

host_entry = "n127.0.0.1 %sn" % domain_name

with open(hosts_file, "a") as vhosts_file:
vhosts_file.write(host_entry)

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

Category: Projects Tagged: apache python

comments

Page 1 of 1