Skip to content

Instantly share code, notes, and snippets.

@renshuki
Forked from fideloper/vhost.py
Created August 31, 2016 02:02
Show Gist options
  • Save renshuki/cdae4b48d42ac9ae77d25f663b90643f to your computer and use it in GitHub Desktop.
Save renshuki/cdae4b48d42ac9ae77d25f663b90643f to your computer and use it in GitHub Desktop.

Revisions

  1. @fideloper fideloper revised this gist Apr 25, 2014. 1 changed file with 21 additions and 9 deletions.
    30 changes: 21 additions & 9 deletions vhost.sh
    Original file line number Diff line number Diff line change
    @@ -15,6 +15,7 @@ Assumes /etc/apache2/sites-available and /etc/apache2/sites-enabled setup used
    -d DocumentRoot - i.e. /var/www/yoursite
    -h Help - Show this menu.
    -s ServerName - i.e. example.com or sub.example.com
    -a ServerAlias - i.e. *.example.com or another domain altogether
    -p File path to the SSL certificate. Directories only, no file name.
    If using an SSL Certificate, also creates a port :443 vhost as well.
    This *ASSUMES* a .crt and a .key file exists
    @@ -41,12 +42,13 @@ cat <<- _EOF_
    <VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName $ServerName
    $ServerAlias
    DocumentRoot $DocumentRoot
    # Uncomment this to proxy pass to fastcgi
    # Assumes Apache 2.4 with mod_proxy_fcgi
    # ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000$DocumentRoot/$1
    # ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000$DocumentRoot/$1
    <Directory $DocumentRoot>
    Options -Indexes +FollowSymLinks +MultiViews
    @@ -61,7 +63,7 @@ cat <<- _EOF_
    LogLevel warn
    CustomLog \${APACHE_LOG_DIR}/$ServerName-access.log combined
    #ProxyPassMatch
    @@ -74,12 +76,13 @@ cat <<- _EOF_
    <VirtualHost *:443>
    ServerAdmin webmaster@localhost
    ServerName $ServerName
    $ServerAlias
    DocumentRoot $DocumentRoot
    # Uncomment this to proxy pass to fastcgi
    # Assumes Apache 2.4 with mod_proxy_fcgi
    # ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000$DocumentRoot/$1
    # ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000$DocumentRoot/$1
    <Directory $DocumentRoot>
    Options -Indexes +FollowSymLinks +MultiViews
    @@ -94,7 +97,7 @@ cat <<- _EOF_
    LogLevel warn
    CustomLog \${APACHE_LOG_DIR}/$ServerName-access.log combined
    #ProxyPassMatch
    SSLEngine on
    @@ -123,7 +126,7 @@ fi
    CertPath=""

    #Parse flags
    while getopts "d:s:p:c:h" OPTION; do
    while getopts "d:s:a:p:c:h" OPTION; do
    case $OPTION in
    h)
    show_usage
    @@ -134,6 +137,9 @@ while getopts "d:s:p:c:h" OPTION; do
    s)
    ServerName=$OPTARG
    ;;
    a)
    Alias=$OPTARG
    ;;
    p)
    CertPath=$OPTARG
    ;;
    @@ -146,12 +152,19 @@ while getopts "d:s:p:c:h" OPTION; do
    esac
    done

    # If alias is set:
    if [ "$Alias" != "" ]; then
    ServerAlias="ServerAlias "$Alias
    else
    ServerAlias=""
    fi

    # If CertName doesn't get set, set it to ServerName
    if [ "$CertName" == "" ]; then
    CertName=$ServerName
    fi

    if [ ! -d $DocumentRoot ]; then
    if [ ! -d $DocumentRoot ]; then
    mkdir -p $DocumentRoot
    #chown USER:USER $DocumentRoot #POSSIBLE IMPLEMENTATION, new flag -u ?
    fi
    @@ -170,5 +183,4 @@ else
    # Enable Site
    cd /etc/apache2/sites-available/ && a2ensite ${ServerName}.conf
    service apache2 reload
    fi

    fi
  2. @fideloper fideloper revised this gist Apr 25, 2014. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions vhost.sh
    Original file line number Diff line number Diff line change
    @@ -61,6 +61,8 @@ cat <<- _EOF_
    LogLevel warn
    CustomLog \${APACHE_LOG_DIR}/$ServerName-access.log combined
    #ProxyPassMatch
    </VirtualHost>
    @@ -92,6 +94,8 @@ cat <<- _EOF_
    LogLevel warn
    CustomLog \${APACHE_LOG_DIR}/$ServerName-access.log combined
    #ProxyPassMatch
    SSLEngine on
  3. @fideloper fideloper revised this gist Apr 2, 2014. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions vhost.sh
    Original file line number Diff line number Diff line change
    @@ -43,6 +43,10 @@ cat <<- _EOF_
    ServerName $ServerName
    DocumentRoot $DocumentRoot
    # Uncomment this to proxy pass to fastcgi
    # Assumes Apache 2.4 with mod_proxy_fcgi
    # ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000$DocumentRoot/$1
    <Directory $DocumentRoot>
    Options -Indexes +FollowSymLinks +MultiViews
    @@ -71,6 +75,10 @@ cat <<- _EOF_
    DocumentRoot $DocumentRoot
    # Uncomment this to proxy pass to fastcgi
    # Assumes Apache 2.4 with mod_proxy_fcgi
    # ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000$DocumentRoot/$1
    <Directory $DocumentRoot>
    Options -Indexes +FollowSymLinks +MultiViews
    AllowOverride All
  4. @fideloper fideloper revised this gist Feb 18, 2014. 1 changed file with 26 additions and 10 deletions.
    36 changes: 26 additions & 10 deletions vhost.sh
    Original file line number Diff line number Diff line change
    @@ -15,13 +15,18 @@ Assumes /etc/apache2/sites-available and /etc/apache2/sites-enabled setup used
    -d DocumentRoot - i.e. /var/www/yoursite
    -h Help - Show this menu.
    -s ServerName - i.e. example.com or sub.example.com
    -c File path to the SSL certificate. Directories only, no file name.
    -p File path to the SSL certificate. Directories only, no file name.
    If using an SSL Certificate, also creates a port :443 vhost as well.
    This *ASSUMES* a .crt and a .key file exists
    at file path /provided-file-path/your-server-name.[crt|key].
    at file path /provided-file-path/your-server-or-cert-name.[crt|key].
    Otherwise you can except Apache errors when you reload Apache.
    Ensure Apache's mod_ssl is enabled via "sudo a2enmod ssl".
    -c Certificate filename. "xip.io" becomes "xip.io.key" and "xip.io.crt".
    Example Usage. Serve files from /var/www/xip.io at http(s)://192.168.33.10.xip.io
    using ssl files from /etc/ssl/xip.io/xip.io.[key|crt]
    sudo vhost -d /var/www/xip.io -s 192.168.33.10.xip.io -p /etc/ssl/xip.io -c xip.io
    _EOF_
    exit 1
    }
    @@ -82,8 +87,8 @@ cat <<- _EOF_
    SSLEngine on
    SSLCertificateFile $CertPath/$ServerName.crt
    SSLCertificateKeyFile $CertPath/$ServerName.key
    SSLCertificateFile $CertPath/$CertName.crt
    SSLCertificateKeyFile $CertPath/$CertName.key
    <FilesMatch "\.(cgi|shtml|phtml|php)$">
    SSLOptions +StdEnvVars
    @@ -106,7 +111,7 @@ fi
    CertPath=""

    #Parse flags
    while getopts "d:s:c:" OPTION; do
    while getopts "d:s:p:c:h" OPTION; do
    case $OPTION in
    h)
    show_usage
    @@ -117,15 +122,23 @@ while getopts "d:s:c:" OPTION; do
    s)
    ServerName=$OPTARG
    ;;
    c)
    p)
    CertPath=$OPTARG
    ;;
    c)
    CertName=$OPTARG
    ;;
    *)
    show_usage
    ;;
    esac
    done

    # If CertName doesn't get set, set it to ServerName
    if [ "$CertName" == "" ]; then
    CertName=$ServerName
    fi

    if [ ! -d $DocumentRoot ]; then
    mkdir -p $DocumentRoot
    #chown USER:USER $DocumentRoot #POSSIBLE IMPLEMENTATION, new flag -u ?
    @@ -137,10 +150,13 @@ if [ -f "$DocumentRoot/$ServerName.conf" ]; then
    else
    create_vhost > /etc/apache2/sites-available/${ServerName}.conf

    # Add :443 handling
    if [ "$CertPath" != "" ]; then
    create_ssl_vhost >> /etc/apache2/sites-available/${ServerName}.conf
    fi

    cd /etc/apache2/sites-available/ && a2ensite ${ServerName}.conf #Enable site
    service apache2 reload #Optional implementation
    fi
    # Enable Site
    cd /etc/apache2/sites-available/ && a2ensite ${ServerName}.conf
    service apache2 reload
    fi

  5. @fideloper fideloper revised this gist Feb 17, 2014. 1 changed file with 9 additions and 8 deletions.
    17 changes: 9 additions & 8 deletions vhost.sh
    Original file line number Diff line number Diff line change
    @@ -15,9 +15,10 @@ Assumes /etc/apache2/sites-available and /etc/apache2/sites-enabled setup used
    -d DocumentRoot - i.e. /var/www/yoursite
    -h Help - Show this menu.
    -s ServerName - i.e. example.com or sub.example.com
    -c Using an SSL Certificate. Also creates a port :443 vhost as well.
    -c File path to the SSL certificate. Directories only, no file name.
    If using an SSL Certificate, also creates a port :443 vhost as well.
    This *ASSUMES* a .crt and a .key file exists
    at file path /etc/ssl/your-server-name/your-server-name.[crt|key].
    at file path /provided-file-path/your-server-name.[crt|key].
    Otherwise you can except Apache errors when you reload Apache.
    Ensure Apache's mod_ssl is enabled via "sudo a2enmod ssl".
    @@ -81,8 +82,8 @@ cat <<- _EOF_
    SSLEngine on
    SSLCertificateFile /etc/ssl/$ServerName/$ServerName.crt
    SSLCertificateKeyFile /etc/ssl/$ServerName/$ServerName.key
    SSLCertificateFile $CertPath/$ServerName.crt
    SSLCertificateKeyFile $CertPath/$ServerName.key
    <FilesMatch "\.(cgi|shtml|phtml|php)$">
    SSLOptions +StdEnvVars
    @@ -102,10 +103,10 @@ if [ "$#" -lt 4 ]; then
    show_usage
    fi

    SSL=0
    CertPath=""

    #Parse flags
    while getopts "d:s:c" OPTION; do
    while getopts "d:s:c:" OPTION; do
    case $OPTION in
    h)
    show_usage
    @@ -117,7 +118,7 @@ while getopts "d:s:c" OPTION; do
    ServerName=$OPTARG
    ;;
    c)
    SSL=1
    CertPath=$OPTARG
    ;;
    *)
    show_usage
    @@ -136,7 +137,7 @@ if [ -f "$DocumentRoot/$ServerName.conf" ]; then
    else
    create_vhost > /etc/apache2/sites-available/${ServerName}.conf

    if [ $SSL -gt 0 ]; then
    if [ "$CertPath" != "" ]; then
    create_ssl_vhost >> /etc/apache2/sites-available/${ServerName}.conf
    fi

  6. @fideloper fideloper revised this gist Feb 17, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion vhost.py
    Original file line number Diff line number Diff line change
    @@ -94,4 +94,4 @@ def create_vhost(documentroot, servername):
    destLink = '/etc/apache2/sites-enabled/%s.conf' % serverName
    symlink(srcLink, destLink)

    os.system('service apache2 reload')
    system('service apache2 reload')
  7. @fideloper fideloper revised this gist Feb 17, 2014. 1 changed file with 59 additions and 6 deletions.
    65 changes: 59 additions & 6 deletions vhost.sh
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    #! /bin/bash

    # May need to run this as sudo!
    # I have it in /usr/local/bin and run command 'vhost' from anywhere, using sudo.
    # Run this as sudo!
    # I move this file to /usr/local/bin/vhost and run command 'vhost' from anywhere, using sudo.

    #
    # Show Usage, Output to STDERR
    @@ -15,6 +15,11 @@ Assumes /etc/apache2/sites-available and /etc/apache2/sites-enabled setup used
    -d DocumentRoot - i.e. /var/www/yoursite
    -h Help - Show this menu.
    -s ServerName - i.e. example.com or sub.example.com
    -c Using an SSL Certificate. Also creates a port :443 vhost as well.
    This *ASSUMES* a .crt and a .key file exists
    at file path /etc/ssl/your-server-name/your-server-name.[crt|key].
    Otherwise you can except Apache errors when you reload Apache.
    Ensure Apache's mod_ssl is enabled via "sudo a2enmod ssl".
    _EOF_
    exit 1
    @@ -36,8 +41,6 @@ cat <<- _EOF_
    <Directory $DocumentRoot>
    Options -Indexes +FollowSymLinks +MultiViews
    AllowOverride All
    Order allow,deny
    Allow from all
    Require all granted
    </Directory>
    @@ -54,13 +57,55 @@ cat <<- _EOF_
    _EOF_
    }

    function create_ssl_vhost {
    cat <<- _EOF_
    <VirtualHost *:443>
    ServerAdmin webmaster@localhost
    ServerName $ServerName
    DocumentRoot $DocumentRoot
    <Directory $DocumentRoot>
    Options -Indexes +FollowSymLinks +MultiViews
    AllowOverride All
    Require all granted
    </Directory>
    ErrorLog \${APACHE_LOG_DIR}/$ServerName-error.log
    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn
    CustomLog \${APACHE_LOG_DIR}/$ServerName-access.log combined
    SSLEngine on
    SSLCertificateFile /etc/ssl/$ServerName/$ServerName.crt
    SSLCertificateKeyFile /etc/ssl/$ServerName/$ServerName.key
    <FilesMatch "\.(cgi|shtml|phtml|php)$">
    SSLOptions +StdEnvVars
    </FilesMatch>
    BrowserMatch "MSIE [2-6]" \\
    nokeepalive ssl-unclean-shutdown \\
    downgrade-1.0 force-response-1.0
    # MSIE 7 and newer should be able to use keepalive
    BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown
    </VirtualHost>
    _EOF_
    }

    #Sanity Check - are there two arguments with 2 values?
    if [ $# -ne 4 ]; then
    if [ "$#" -lt 4 ]; then
    show_usage
    fi

    SSL=0

    #Parse flags
    while getopts "d:s:" OPTION; do
    while getopts "d:s:c" OPTION; do
    case $OPTION in
    h)
    show_usage
    @@ -71,6 +116,9 @@ while getopts "d:s:" OPTION; do
    s)
    ServerName=$OPTARG
    ;;
    c)
    SSL=1
    ;;
    *)
    show_usage
    ;;
    @@ -87,6 +135,11 @@ if [ -f "$DocumentRoot/$ServerName.conf" ]; then
    show_usage
    else
    create_vhost > /etc/apache2/sites-available/${ServerName}.conf

    if [ $SSL -gt 0 ]; then
    create_ssl_vhost >> /etc/apache2/sites-available/${ServerName}.conf
    fi

    cd /etc/apache2/sites-available/ && a2ensite ${ServerName}.conf #Enable site
    service apache2 reload #Optional implementation
    fi
  8. @fideloper fideloper revised this gist Jan 26, 2014. 1 changed file with 5 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions vhost.py
    Original file line number Diff line number Diff line change
    @@ -4,6 +4,7 @@
    from os.path import exists
    from os import makedirs
    from os import symlink
    from os import system
    import getopt

    #
    @@ -35,7 +36,8 @@ def create_vhost(documentroot, servername):
    Options -Indexes +FollowSymLinks +MultiViews
    AllowOverride All
    Order allow,deny
    allow from all
    Allow from all
    Require all granted
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/%s-error.log
    @@ -91,4 +93,5 @@ def create_vhost(documentroot, servername):
    srcLink = '/etc/apache2/sites-available/%s.conf' % serverName
    destLink = '/etc/apache2/sites-enabled/%s.conf' % serverName
    symlink(srcLink, destLink)
    #service apache2 reload #Optional implementation -> from os import system -> os.system('service apache2 reload')

    os.system('service apache2 reload')
  9. @fideloper fideloper revised this gist Oct 18, 2013. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions vhost.sh
    Original file line number Diff line number Diff line change
    @@ -37,7 +37,8 @@ cat <<- _EOF_
    Options -Indexes +FollowSymLinks +MultiViews
    AllowOverride All
    Order allow,deny
    allow from all
    Allow from all
    Require all granted
    </Directory>
    ErrorLog \${APACHE_LOG_DIR}/$ServerName-error.log
    @@ -86,6 +87,6 @@ if [ -f "$DocumentRoot/$ServerName.conf" ]; then
    show_usage
    else
    create_vhost > /etc/apache2/sites-available/${ServerName}.conf
    a2ensite /etc/apache2/sites-available/${ServerName}.conf #Enable site
    cd /etc/apache2/sites-available/ && a2ensite ${ServerName}.conf #Enable site
    service apache2 reload #Optional implementation
    fi
  10. @fideloper fideloper revised this gist Aug 5, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion vhost.sh
    Original file line number Diff line number Diff line change
    @@ -86,6 +86,6 @@ if [ -f "$DocumentRoot/$ServerName.conf" ]; then
    show_usage
    else
    create_vhost > /etc/apache2/sites-available/${ServerName}.conf
    a2ensite /etc/apache2/sites-available/${ServerName} #Enable site
    a2ensite /etc/apache2/sites-available/${ServerName}.conf #Enable site
    service apache2 reload #Optional implementation
    fi
  11. @fideloper fideloper revised this gist Aug 2, 2013. 2 changed files with 4 additions and 4 deletions.
    2 changes: 1 addition & 1 deletion vhost.py
    Original file line number Diff line number Diff line change
    @@ -32,7 +32,7 @@ def create_vhost(documentroot, servername):
    DocumentRoot %s
    <Directory %s>
    Options -Indexes FollowSymLinks MultiViews
    Options -Indexes +FollowSymLinks +MultiViews
    AllowOverride All
    Order allow,deny
    allow from all
    6 changes: 3 additions & 3 deletions vhost.sh
    Original file line number Diff line number Diff line change
    @@ -34,7 +34,7 @@ cat <<- _EOF_
    DocumentRoot $DocumentRoot
    <Directory $DocumentRoot>
    Options -Indexes FollowSymLinks MultiViews
    Options -Indexes +FollowSymLinks +MultiViews
    AllowOverride All
    Order allow,deny
    allow from all
    @@ -86,6 +86,6 @@ if [ -f "$DocumentRoot/$ServerName.conf" ]; then
    show_usage
    else
    create_vhost > /etc/apache2/sites-available/${ServerName}.conf
    ln -s /etc/apache2/sites-available/${ServerName}.conf /etc/apache2/sites-enabled/${ServerName}.conf
    #service apache2 reload #Optional implementation
    a2ensite /etc/apache2/sites-available/${ServerName} #Enable site
    service apache2 reload #Optional implementation
    fi
  12. @fideloper fideloper revised this gist Nov 25, 2012. 2 changed files with 2 additions and 2 deletions.
    2 changes: 1 addition & 1 deletion vhost.py
    Original file line number Diff line number Diff line change
    @@ -31,7 +31,7 @@ def create_vhost(documentroot, servername):
    DocumentRoot %s
    <Directory %s/>
    <Directory %s>
    Options -Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    2 changes: 1 addition & 1 deletion vhost.sh
    Original file line number Diff line number Diff line change
    @@ -33,7 +33,7 @@ cat <<- _EOF_
    DocumentRoot $DocumentRoot
    <Directory $DocumentRoot/>
    <Directory $DocumentRoot>
    Options -Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
  13. @fideloper fideloper revised this gist Jun 11, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions vhost.sh
    Original file line number Diff line number Diff line change
    @@ -40,13 +40,13 @@ cat <<- _EOF_
    allow from all
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/$ServerName-error.log
    ErrorLog \${APACHE_LOG_DIR}/$ServerName-error.log
    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn
    CustomLog ${APACHE_LOG_DIR}/$ServerName-access.log combined
    CustomLog \${APACHE_LOG_DIR}/$ServerName-access.log combined
    </VirtualHost>
  14. @fideloper fideloper revised this gist Jun 10, 2012. 2 changed files with 2 additions and 2 deletions.
    2 changes: 1 addition & 1 deletion vhost.py
    Original file line number Diff line number Diff line change
    @@ -32,7 +32,7 @@ def create_vhost(documentroot, servername):
    DocumentRoot %s
    <Directory %s/>
    Options Indexes FollowSymLinks MultiViews
    Options -Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    allow from all
    2 changes: 1 addition & 1 deletion vhost.sh
    Original file line number Diff line number Diff line change
    @@ -34,7 +34,7 @@ cat <<- _EOF_
    DocumentRoot $DocumentRoot
    <Directory $DocumentRoot/>
    Options Indexes FollowSymLinks MultiViews
    Options -Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    allow from all
  15. @fideloper fideloper revised this gist Jun 2, 2012. No changes.
  16. Chris Fidao revised this gist May 17, 2012. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions vhost.py
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,5 @@
    #! /usr/bin/python

    # ***NOT YET TESTED*** PYTHON VERSION

    from sys import argv
    from os.path import exists
    from os import makedirs
    @@ -86,7 +84,7 @@ def create_vhost(documentroot, servername):
    print 'vHost already exists. Aborting'
    show_usage()
    else:
    target = open('/etc/apache2/sites-available/%r.conf' % serverName)
    target = open('/etc/apache2/sites-available/%s.conf' % serverName, 'w')
    target.write(create_vhost(documentRoot, serverName))
    target.close()

  17. Chris Fidao revised this gist May 17, 2012. No changes.
  18. Chris Fidao revised this gist May 17, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion vhost.sh
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    #! /bin/bash

    # May need to run this as sudo
    # May need to run this as sudo!
    # I have it in /usr/local/bin and run command 'vhost' from anywhere, using sudo.

    #
  19. Chris Fidao revised this gist May 17, 2012. 1 changed file with 96 additions and 0 deletions.
    96 changes: 96 additions & 0 deletions vhost.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,96 @@
    #! /usr/bin/python

    # ***NOT YET TESTED*** PYTHON VERSION

    from sys import argv
    from os.path import exists
    from os import makedirs
    from os import symlink
    import getopt

    #
    # Show Usage, Output to STDERR
    #
    def show_usage():
    print """
    Create a new vHost in Ubuntu Server
    Assumes /etc/apache2/sites-available and /etc/apache2/sites-enabled setup used
    -d DocumentRoot - i.e. /var/www/yoursite
    -h Help - Show this menu.
    -s ServerName - i.e. example.com or sub.example.com
    """
    exit(1)

    #
    # Output vHost skeleton, fill with userinput
    # To be outputted into new file
    #
    def create_vhost(documentroot, servername):
    out = """<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName %s
    DocumentRoot %s
    <Directory %s/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    allow from all
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/%s-error.log
    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn
    CustomLog ${APACHE_LOG_DIR}/%s-access.log combined
    </VirtualHost>""" % (servername, documentroot, documentroot, servername, servername)
    return out

    #Parse flags, fancy python way. Long options also!
    try:
    opts, args = getopt.getopt(argv[1:], "hd:s:", ["help", "document-root=", 'server-name='])
    except getopt.GetoptError, err:
    print str(err)
    show_usage()

    #Sanity check - make sure there are arguments
    if opts.__len__() == 0:
    show_usage()

    documentRoot = None
    serverName = None

    #Get values from flags
    for option, value in opts:
    if option in ('-h', '--help'):
    show_usage()
    elif option in ('-d', '--document-root'):
    documentRoot = value
    elif option in ('-s', '--server-name'):
    serverName = value
    else:
    print "Unknown parameter used"
    show_usage()

    if exists(documentRoot) == False:
    makedirs(documentRoot, 0755)
    #chown USER:USER $DocumentRoot #POSSIBLE IMPLEMENTATION, new flag -u ?
    #from pwd import getpwnam -> inspect: getpwnam('someuser')

    if exists('%s/%s.conf' % (documentRoot, serverName)):
    print 'vHost already exists. Aborting'
    show_usage()
    else:
    target = open('/etc/apache2/sites-available/%r.conf' % serverName)
    target.write(create_vhost(documentRoot, serverName))
    target.close()

    srcLink = '/etc/apache2/sites-available/%s.conf' % serverName
    destLink = '/etc/apache2/sites-enabled/%s.conf' % serverName
    symlink(srcLink, destLink)
    #service apache2 reload #Optional implementation -> from os import system -> os.system('service apache2 reload')
  20. Chris Fidao revised this gist May 16, 2012. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions vhost.sh
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,8 @@
    #! /bin/bash

    # May need to run this as sudo
    # I have it in /usr/local/bin and run command 'vhost' from anywhere, using sudo.

    #
    # Show Usage, Output to STDERR
    #
    @@ -84,4 +87,5 @@ if [ -f "$DocumentRoot/$ServerName.conf" ]; then
    else
    create_vhost > /etc/apache2/sites-available/${ServerName}.conf
    ln -s /etc/apache2/sites-available/${ServerName}.conf /etc/apache2/sites-enabled/${ServerName}.conf
    #service apache2 reload #Optional implementation
    fi
  21. Chris Fidao revised this gist May 16, 2012. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions vhost.sh
    Original file line number Diff line number Diff line change
    @@ -75,6 +75,7 @@ done

    if [ ! -d $DocumentRoot ]; then
    mkdir -p $DocumentRoot
    #chown USER:USER $DocumentRoot #POSSIBLE IMPLEMENTATION, new flag -u ?
    fi

    if [ -f "$DocumentRoot/$ServerName.conf" ]; then
  22. Chris Fidao revised this gist May 16, 2012. 1 changed file with 3 additions and 4 deletions.
    7 changes: 3 additions & 4 deletions vhost.sh
    Original file line number Diff line number Diff line change
    @@ -50,8 +50,8 @@ cat <<- _EOF_
    _EOF_
    }

    #Sanity Check - are there two arguments?
    if [ $# -ne 2 ]; then
    #Sanity Check - are there two arguments with 2 values?
    if [ $# -ne 4 ]; then
    show_usage
    fi

    @@ -70,7 +70,6 @@ while getopts "d:s:" OPTION; do
    *)
    show_usage
    ;;
    ;;
    esac
    done

    @@ -82,6 +81,6 @@ if [ -f "$DocumentRoot/$ServerName.conf" ]; then
    echo 'vHost already exists. Aborting'
    show_usage
    else
    make_vhost > /etc/apache2/sites-available/${ServerName}.conf
    create_vhost > /etc/apache2/sites-available/${ServerName}.conf
    ln -s /etc/apache2/sites-available/${ServerName}.conf /etc/apache2/sites-enabled/${ServerName}.conf
    fi
  23. Chris Fidao created this gist May 16, 2012.
    87 changes: 87 additions & 0 deletions vhost.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,87 @@
    #! /bin/bash

    #
    # Show Usage, Output to STDERR
    #
    function show_usage {
    cat <<- _EOF_
    Create a new vHost in Ubuntu Server
    Assumes /etc/apache2/sites-available and /etc/apache2/sites-enabled setup used
    -d DocumentRoot - i.e. /var/www/yoursite
    -h Help - Show this menu.
    -s ServerName - i.e. example.com or sub.example.com
    _EOF_
    exit 1
    }


    #
    # Output vHost skeleton, fill with userinput
    # To be outputted into new file
    #
    function create_vhost {
    cat <<- _EOF_
    <VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName $ServerName
    DocumentRoot $DocumentRoot
    <Directory $DocumentRoot/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    allow from all
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/$ServerName-error.log
    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn
    CustomLog ${APACHE_LOG_DIR}/$ServerName-access.log combined
    </VirtualHost>
    _EOF_
    }

    #Sanity Check - are there two arguments?
    if [ $# -ne 2 ]; then
    show_usage
    fi

    #Parse flags
    while getopts "d:s:" OPTION; do
    case $OPTION in
    h)
    show_usage
    ;;
    d)
    DocumentRoot=$OPTARG
    ;;
    s)
    ServerName=$OPTARG
    ;;
    *)
    show_usage
    ;;
    ;;
    esac
    done

    if [ ! -d $DocumentRoot ]; then
    mkdir -p $DocumentRoot
    fi

    if [ -f "$DocumentRoot/$ServerName.conf" ]; then
    echo 'vHost already exists. Aborting'
    show_usage
    else
    make_vhost > /etc/apache2/sites-available/${ServerName}.conf
    ln -s /etc/apache2/sites-available/${ServerName}.conf /etc/apache2/sites-enabled/${ServerName}.conf
    fi