Skip to content

Instantly share code, notes, and snippets.

@weatheredwatcher
Last active November 15, 2023 16:16
Show Gist options
  • Save weatheredwatcher/44868f5db74e8916017a to your computer and use it in GitHub Desktop.
Save weatheredwatcher/44868f5db74e8916017a to your computer and use it in GitHub Desktop.
A python script for creating vhost files
#!/usr/bin/python
import sys, getopt
def main(argv):
server = ''
path = ''
try:
opts, args = getopt.getopt(argv, "hs:p:", ["server=", "path="] )
except getopt.GetoptError:
print 'Usage: vhost_writer.py -s <servername> -p <pathname>'
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print 'Usage: vhost_writer.py -s <servername> -p <pathname>'
sys.exit()
elif opt in ("-s", "--server"):
server = arg
elif opt in ("-p", "--path"):
path = arg
print f"""
<VirtualHost *:80>
ServerName {server}
ServerAlias www.{server}
DocumentRoot /var/www/{path}
<Directory /var/www/{path}
Options -Indexes FollowSymLinks -MultiViews
AllowOverride All
</Directory>
CustomLog /var/log/httpd/{server}-access.log combined
ErrorLog /var/log/httpd/{server}-error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
</VirtualHost>
"""
if __name__ == "__main__":
main(sys.argv[1:])
@weatheredwatcher
Copy link
Author

Updated to use f-strings

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment