Below is a small script that will allow you to create a vhost on Ubuntu created using Python. I hope to improve this script over time but it’s a small step in the right direction:
In order to run this script you will need to have Python installed and your user needs to have Sudo privilages. Just copy the contents of this script into a new Python file such as createvhost.py then execute it like so sudo python createvhost.py it will prompt you to enter than name of your vhost like ‘mydomain.com’
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
import os #Get User Input for New Vhost print("Please Type a vhost name in the form of the domain name like 'example.com' including the single quotes\r\n") vhost = input() cmd = "mkdir /var/www/{0}".format(vhost) os.system(cmd) #change permissions cmd = "chown -R $USER:$USER /var/www/{0}".format(vhost) os.system(cmd) cmd = "chmod -R 755 /var/www/{0}".format(vhost) os.system(cmd) #create holding index file filename = '/var/www/{0}/index.html'.format(vhost) f = open(filename, 'w') f.write("<html><head><title>This is an Index Page</title></head><body><h1>this is some content</h1></body></html>") f.close() #create vhost conf filename = '/etc/apache2/sites-available/{0}.conf'.format(vhost) filecontent = """<VirtualHost *:80>\r\n ServerAdmin webmaster@localhost\r\n ServerName {0}\r\n ServerAlias www.{0}\r\n DocumentRoot /var/www/{0}\r\n ErrorLog /var/log/apache2/error.log\r\n CustomLog /var/log/apache2/access.log combined\r\n </VirtualHost>""".format(vhost) f = open(filename, 'w') f.write(filecontent) f.close() #enable new vhost cmd = "a2ensite {0}".format(vhost) os.system(cmd) #disable default host cmd = "a2dissite 000-default.conf" os.system(cmd) #test apache config cmd = "apache2ctl configtest" os.system(cmd) #restart apache2 cmd = "systemctl restart apache2" os.system(cmd) |