• Small Script to Automatically Create Apache Virtualhosts for Local Subdomains

    Date: 2008.10.20 | Category: Linux, Tech Stuff | Tags:

    Just made this small script I’m going to use to set up new subdomains to my localhost whenever I start a new project. E.g., if I’m to make the new Audi site, I’d like to set up audi.localhost with Apache, so that docs, cgi-bin, and logs are in subfolders to my ~/Web folder.

    Read the comment in the beginning, it explains it ;) Also, if you download the script, don’t forget to chmod +x on it so that it’s executable.

    ?Download create_vhost
    #!/bin/bash
    #
    # daniel@danielsmedegaardbuus.dk
    #
    # Last modified: 2008-10-20 19:39
    #
    # This is a script to register a new localhost virtual subdomain with Apache.
    #
    # Very useful, for instance, if you're a developer, and you keep sites in your
    # home folder and like to have your paths in "<projectname>.localhost".
    #
    # The script assumes that you want to keep your files in cgi-bin, htdocs, and
    # logs subdirectories somewhere, e.g. the default:
    #    /home/yourname/Web
    #      +--  htdocs/projectname   [=> http://projectname.localhost/]
    #      +--  cgi-bin/projectname  [=> http://projectname.localhost/cgi-bin/]
    #      +--  logs/projectname     [ home of the apache access and error logs]
    #
    # By default, the directories will be owned by the user executing sudo, or
    # root if sudo was not used. But you will be prompted, so you can change it.
    #
     
     
     
    # Need root privileges, or we can't write the Apache virtualhost file, nor append to /etc/hosts:
    if [ ! `whoami` == "root" ]; then
    	echo
    	echo "This script cannot run without root privileges."
    	echo "Try with 'sudo $0'"
    	echo
    	exit 1
    fi
     
     
     
    # We've already bailed out if we're not either run as root or sudo'ed, so no sudo user means it's root directly:
    if [ "$SUDO_USER" == "" ]; then DIR_OWNER="root:root"; else DIR_OWNER="$SUDO_USER:$SUDO_USER"; fi
    read -p "Which user and group do you want created directories to belong to? [$DIR_OWNER] "
    if [ ! "$REPLY" == "" ]; then DIR_OWNER=$REPLY; fi
     
     
    WEB_FOLDER=~/Web
    read -p "Enter the base web serving folder that contains your htdocs, cgi-bin, and logs directories: [$WEB_FOLDER] "
    if [ ! "$REPLY" == "" ]; then WEB_FOLDER=$REPLY; fi
     
     
     
    DOMAIN=""
    while [ "$DOMAIN" == "" ]; do
    	read -p "Enter the localhost subdomain name (folders will be created under $WEB_FOLDER): "
    	DOMAIN=$REPLY
    done
     
     
     
    APACHE_VHOST_FILE=/etc/apache2/sites-available/$DOMAIN.localhost
     
     
     
    echo "
    Please review:
    ==============
    ===   $DOMAIN.localhost will be configured in $APACHE_VHOST_FILE.
    ===   $DOMAIN.localhost will be appended to /etc/hosts.
    ===   The following new folders will be created if they don't exist:
          $WEB_FOLDER
          $WEB_FOLDER/cgi-bin/$DOMAIN
          $WEB_FOLDER/htdocs/$DOMAIN
          $WEB_FOLDER/logs/$DOMAIN
     
    "
     
     
     
    read -p "Do you wish to proceed? [y] "
    if [[ "$REPLY" != "y" && "$REPLY" != "Y" && "$REPLY" != "" ]]; then
    	echo
    	echo "No changes have been made to your system."
    	echo
    	exit 0
    fi
    echo
     
     
     
    echo ">>>   Creating directories..."
    # Web hosts folder:
    if [ ! -d $WEB_FOLDER ]; then
    	mkdir "$WEB_FOLDER"
    	if [ "$?" -ne 0 ]; then
    		echo "Failed to create $WEB_FOLDER, exiting.";
    		exit 1;
    	else
    		chown $DIR_OWNER "$WEB_FOLDER";
    	fi
    fi
     
    # cgi-bin folder:
    if [ ! -e "$WEB_FOLDER/cgi-bin" ]; then
    	mkdir "$WEB_FOLDER/cgi-bin"
    	if [ "$?" -ne 0 ]; then
    		echo "Failed to create $WEB_FOLDER/cgi-bin, exiting."
    		exit 1
    	else
    		chown $DIR_OWNER "$WEB_FOLDER/cgi-bin"
    	fi
    elif [ ! -d "$WEB_FOLDER/cgi-bin" ]; then
    	echo "Resource $WEB_FOLDER/cgi-bin already in use by non-folder, exiting."
    	exit 1
    else
    	echo ">>>   Folder $WEB_FOLDER/cgi-bin already exists, skipping."
    fi
    if [ ! -e "$WEB_FOLDER/cgi-bin/$DOMAIN" ]; then
    	mkdir "$WEB_FOLDER/cgi-bin/$DOMAIN"
    	if [ "$?" -ne 0 ]; then
    		echo "Failed to create $WEB_FOLDER/cgi-bin/$DOMAIN, exiting."
    		exit 1
    	else
    		chown $DIR_OWNER "$WEB_FOLDER/cgi-bin/$DOMAIN"
    	fi
    elif [ ! -d "$WEB_FOLDER/cgi-bin/$DOMAIN" ]; then
    	echo "Resource $WEB_FOLDER/cgi-bin/$DOMAIN already in use by non-folder, exiting."
    	exit 1
    else
    	echo ">>>   Folder $WEB_FOLDER/cgi-bin/$DOMAIN already exists, skipping."
    fi
     
    # htdocs folder:
    if [ ! -e "$WEB_FOLDER/htdocs" ]; then
    	mkdir "$WEB_FOLDER/htdocs"
    	if [ "$?" -ne 0 ]; then
    		echo "Failed to create $WEB_FOLDER/htdocs, exiting."
    		exit 1
    	else
    		chown $DIR_OWNER "$WEB_FOLDER/htdocs"
    	fi
    elif [ ! -d "$WEB_FOLDER/htdocs" ]; then
    	echo "Resource $WEB_FOLDER/htdocs already in use by non-folder, exiting."
    	exit 1
    else
    	echo ">>>   Folder $WEB_FOLDER/htdocs already exists, skipping."
    fi
    if [ ! -e "$WEB_FOLDER/htdocs/$DOMAIN" ]; then
    	mkdir "$WEB_FOLDER/htdocs/$DOMAIN"
    	if [ "$?" -ne 0 ]; then
    		echo "Failed to create $WEB_FOLDER/htdocs/$DOMAIN, exiting."
    		exit 1
    	else
    		chown $DIR_OWNER "$WEB_FOLDER/htdocs/$DOMAIN"
    	fi
    elif [ ! -d "$WEB_FOLDER/htdocs/$DOMAIN" ]; then
    	echo "Resource $WEB_FOLDER/htdocs/$DOMAIN already in use by non-folder, exiting."
    	exit 1
    else
    	echo ">>>   Folder $WEB_FOLDER/htdocs/$DOMAIN already exists, skipping."
    fi
     
    # logs folder:
    if [ ! -e "$WEB_FOLDER/logs" ]; then
    	mkdir "$WEB_FOLDER/logs"
    	if [ "$?" -ne 0 ]; then
    		echo "Failed to create $WEB_FOLDER/logs, exiting."
    		exit 1
    	else
    		chown $DIR_OWNER "$WEB_FOLDER/logs"
    	fi
    elif [ ! -d "$WEB_FOLDER/logs" ]; then
    	echo "Resource $WEB_FOLDER/logs already in use by non-folder, exiting."
    	exit 1
    else
    	echo ">>>   Folder $WEB_FOLDER/logs already exists, skipping."
    fi
    if [ ! -e "$WEB_FOLDER/logs/$DOMAIN" ]; then
    	mkdir "$WEB_FOLDER/logs/$DOMAIN"
    	if [ "$?" -ne 0 ]; then
    		echo "Failed to create $WEB_FOLDER/logs/$DOMAIN, exiting."
    		exit 1
    	else
    		chown $DIR_OWNER "$WEB_FOLDER/logs/$DOMAIN"
    	fi
    elif [ ! -d "$WEB_FOLDER/logs/$DOMAIN" ]; then
    	echo "Resource $WEB_FOLDER/logs/$DOMAIN already in use by non-folder, exiting."
    	exit 1
    else
    	echo ">>>   Folder $WEB_FOLDER/logs/$DOMAIN already exists, skipping."
    fi
    echo
     
     
     
    echo ">>>   Writing $APACHE_VHOST_FILE..."
    echo
     
    echo "
    #
    # $DOMAIN.localhost ($APACHE_VHOST_FILE)
    #
     
    <VirtualHost *>
    	ServerAdmin	admin@localhost
    	ServerName	$DOMAIN.localhost
    	ServerAlias	$DOMAIN.localhost
     
    	# Indexes + Directory Root
    	DirectoryIndex	index.html index.htm index.php
    	DocumentRoot	$WEB_FOLDER/htdocs/$DOMAIN
     
    	# CGI Directory
    	ScriptAlias /cgi-bin/ $WEB_FOLDER/cgi-bin/$DOMAIN
    	<Location /cgi-bin>
    		Options +ExecCGI
    	</Location>
     
    	# Logfiles
    	ErrorLog	$WEB_FOLDER/logs/$DOMAIN/error.log
    	CustomLog	$WEB_FOLDER/logs/$DOMAIN/access.log combined
    </VirtualHost>
    " > $APACHE_VHOST_FILE
     
     
     
    # We register in /etc/hosts, but only if we haven't done so already
    if [[ ! `cat /etc/hosts` =~ "127.0.0.1	$DOMAIN.localhost" ]]; then
    	echo ">>>   Registering subdomain $DOMAIN.localhost with /etc/hosts"
    	echo "127.0.0.1	$DOMAIN.localhost" >> /etc/hosts
    else
    	echo ">>>   Subdomain $DOMAIN.localhost already registered with /etc/hosts, skipping."
    fi
    echo
     
     
     
    # Enabling in Apache:
    echo ">>>   Enabling host"
    a2ensite $DOMAIN.localhost
    echo
     
    echo ">>>   Reloading Apache"
    /etc/init.d/apache2 reload
    echo
     
    echo ">>>   All done, bye :)"
    echo