djekl
10/2/2012 - 2:17 PM

Used within BASH Shell to add virtual hosts

Used within BASH Shell to add virtual hosts

    #!/bin/sh

    ## clear the output
    clear

    ## auth the sudo command
    sudo -p "Please enter you admin password: " whoami 1>/dev/null
    if [ "$(sudo whoami)" != 'root' ]; then
        echo "You entered an invalid password or you are not an admin/sudoer user!\n\nScript aborted"
        exit 1;
    fi

    ## get the current username
    username=$(whoami)

    ## get parameters for hostname and document root
    echo "\nHost name (e.g. dev.domain.com):"
    read hostname

    echo "\nDocument root (project directory '~/projects/'):"
    read project

    ## print a nice spacer
    echo "\n==================================\n"

    ## add entry to hosts file
    sudo bash -c "echo '127.0.0.1       $hostname' >> /etc/hosts"

    ## add entry to vhosts file
    sudo bash -c "echo \"<VirtualHost *:80>
        ServerName  $hostname
        ServerAlias www.$hostname

        DocumentRoot '/Users/$username/projects/$project/'

        ErrorLog  '/Users/$username/Documents/www_logs/$hostname/error.log'
        CustomLog '/Users/$username/Documents/www_logs/$hostname/access.log' combined

        SetEnv APP_ENVIRONMENT development

    </VirtualHost>
    \" >> /etc/apache2/extra/httpd-vhosts.conf"

    ## create the log folder as $username
    mkdir -p /Users/$username/Documents/www_logs/$hostname

    ## set correct permissions
    chmod 755 "/Users/$username/projects/$project"
    chmod 755 "/Users/$username/Documents/www_logs/$hostname"

    ## restart apache
    sudo bash -c "apachectl restart"

    ## kill the sudo session
    sudo -k

    ## inform user we are finished
    echo "\n\nVirtualHost added for $hostname.\n\n"