Skip to content

Instantly share code, notes, and snippets.

@Fed0t
Last active October 15, 2019 15:45
Show Gist options
  • Save Fed0t/36a92a0067b121e7f65922f52564bc37 to your computer and use it in GitHub Desktop.
Save Fed0t/36a92a0067b121e7f65922f52564bc37 to your computer and use it in GitHub Desktop.
Create virtualhosts (.test) fast in WSL with apache2
#!/usr/bin/env python3
import os
import sys
euid = os.geteuid()
if euid != 0:
print("You need to be the Superuser to make changes to these files!")
args = ['sudo', sys.executable] + sys.argv + [os.environ]
os.execlpe('sudo', *args)
domainName = input("\nEnter the domain (.test):\n")
documentRoot = input("\nEnter the name of public folder (default empty):\n")
domainTld = '.test'
hostsFilePath = "/mnt/c/Windows/System32/drivers/etc/hosts"
apacheVHostsPath = "/etc/apache2/sites-available/"
vHostFilePath = apacheVHostsPath + domainName + domainTld + '.conf'
vHostDocumentRoot = "/opt/webserver/" + domainName + domainTld + "/" + documentRoot
if not os.path.exists(vHostDocumentRoot):
os.makedirs(vHostDocumentRoot)
vHostConfig = ["<VirtualHost *:443>\n",
"\tServerName " + domainName + ".test\n",
"\tServerAdmin admin@localhost\n",
"\tDocumentRoot " + vHostDocumentRoot + "\n",
"\tSSLEngine on\n",
"\tSSLCertificateFile /etc/apache2/ssl/apache.crt\n",
"\tSSLCertificateKeyFile /etc/apache2/ssl/apache.key\n",
"\t<Directory /opt/webserver/" + domainName + domainTld + ">\n",
"\t\tOptions Indexes FollowSymLinks\n",
"\t\tAllowOverride All\n",
"\t\tRequire all granted\n",
"\t</Directory>\n",
"\tErrorLog ${APACHE_LOG_DIR}/error.log\n",
"\tCustomLog ${APACHE_LOG_DIR}/access.log combined\n",
"</VirtualHost>\n"]
vHostConfigFile = open(vHostFilePath, "w")
vHostConfigFile.writelines(vHostConfig)
print("\n=====\nVHosts Configured\n=====")
serverIP = input("\nEnter the IP address for your server:")
with open(hostsFilePath, "a") as hostsFile:
hostsFile.write("\n" + serverIP + " \t" + domainName + domainTld)
print("\n=====\nEntry added to hosts file\n=====\n")
def shouldRestart():
answer = input('Would you like to restart Apache? yes/no\n')
if answer == "Yes" or answer == "Y" or answer == "yes" or answer == "y":
print("\n=====\nEnabling Apache Site\n=====")
os.system('sudo a2ensite ' + domainName + domainTld)
print("\n=====\nRestarting Apache\n=====")
os.system('sudo service apache2 restart')
elif answer == "No" or answer == "N" or answer == "no" or answer == "n":
print("\n=====\nYou should probably restart your Apache server\n=====")
else:
print("\n=====\nThat was a wierd response...\n=====\n")
shouldRestart()
shouldRestart()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment