Automating creation of virtual hosts with Python

This is nothing special, but I posted it for personal reference.

#!/usr/bin/python
import os
hostname = raw_input("Enter hostname: e.g. example.com ")
os.system("/usr/sbin/useradd -s /bin/false %s" % hostname)
print "User added .."

vhost = """

ServerName name.com
ServerAlias www.name.com
DocumentRoot /home/name.com/public_html
CustomLog /home/name.com/web.log combined
CustomLog /home/name.com/main.referer.log referer

"""

open('/etc/httpd/conf/%s' % hostname, 'w' ).write(vhost.replace('name.com', hostname))
print "%s was added" % hostname
os.chmod('/home/%s' % hostname, 755)

Leave a Reply