webSIGHTdesigns - Web Design, Web Development, Web Hosting

Script to Create Apache Virtual Hosts

Posted on Friday, May 2nd, 2014 at 2:40 am
by webSIGHTdesigns

Use this Bash script to automate adding new virtual hosts to Apache.

To automate adding new virtual hosts to Apache, create a new file in your text editor of choice:

vi /usr/local/bin/addwebsite

Enter the following Bash script into the file:

#!/bin/sh
WEBROOT="/var/www/"
VHOSTDIR="/etc/apache2/sites-available/"
EXTENSION=""
RESTARTCMD="/usr/bin/sudo service apache2 reload"
if [ "$1" != '' ]; then
    if [ ! -f "$VHOSTDIR$1.conf" ]; then
        cp "$VHOSTDIR/skeleton" "$VHOSTDIR$1$EXTENSION"
        echo "created $VHOSTDIR$1$EXTENSION"
    else
        mv "$VHOSTDIR$1.conf" "$VHOSTDIR$1$EXTENSION.bak"
        cp "$VHOSTDIR/skeleton" "$VHOSTDIR$1$EXTENSION"
        echo "created $VHOSTDIR$1$EXTENSION and made a backup of the existing conf"
    fi
    find "$VHOSTDIR$1$EXTENSION" -type f -exec sed -i "s/SKELETON/$1/" {} \;
    if [ ! -d "$WEBROOT$1/" ]; then
        mkdir "$WEBROOT$1/"
        chown -R apache:apache "$WEBROOT$1/"
        echo "created $WEBROOT$1/"
    else
        echo "$WEBROOT$1/ already exists"
    fi
    sudo a2ensite $1
    $RESTARTCMD
    echo "reloaded apache"
elif [ "$1" = 'help' ] || [ "$1" = '' ]; then
    echo "usage:"
    echo "sudo addwebsite "
    echo "example: to create hostname.localhost just run the command 'sudo addwebsite hostname.localhost'"
fi

Make the file executable:

chmod +x /usr/local/bin/addwebsite

Next create a skeleton file in your VHOSTDIR with the following contents:

<VIRTUALHOST *:80>
    ServerAdmin webmaster@localhost
    ServerName SKELETON
    DocumentRoot /var/www/SKELETON
    <DIRECTORY />
        Options FollowSymLinks
        AllowOverride None
    </DIRECTORY>
    <DIRECTORY /var/www/SKELETON/>
        Options +FollowSymLinks
        # <IfModule mod_rewrite.c>
        # RewriteEngine On
        # RewriteBase /
        # RewriteCond %{REQUEST_FILENAME} -f [OR]
        # RewriteCond %{REQUEST_FILENAME} -d
        # RewriteRule ^.*$ - [S=40]
        # RewriteRule (.*)/(.*)/$ /index.php?page=$1&id=$2 [QSA,L]
        # RewriteRule (.*)/$ /index.php?page=$1 [QSA,L]
        # </IfModule>
        # php_value auto_prepend_file /var/www/SKELETON/prepend.php
    </DIRECTORY>
    ErrorLog ${APACHE_LOG_DIR}/SKELETON_error.log
    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn
    CustomLog ${APACHE_LOG_DIR}/SKELETON_access.log combined
</VIRTUALHOST>

The above script was written on an Ubuntu 12.04 system, so you may need to edit file paths for other systems. You'll also probably want to edit the skeleton file to suit your own needs.

Check out our bash scripts repository for the files.

Please Sign In

Please sign in to post a comment.

Web Development

View details »

Web Hosting

View details »

Our Portfolio

View portfolio »

WebSight Designs webSIGHTdesigns preferred email webSIGHTdesigns United States United States