Debian/Apache2 Virtualized Host Script

I wrote this script so that I could quickly create virtualized subdomains on my server. I wrote it so that all you have to do to reuse it is simply change the first 3 lines of the file to match your server. This is version 1.o and I plan to add some more error handling and improvements further down the road. Feel free to contact me with any feedback or suggestions.

Click Here to download the script

Or Heres the code:

#!/bin/bash
#  script: create virtualized subdomain #
#                                       #
#  written by:                          #
#    Patrick Burns                      #
#########################################
 
APACHE_CONFIG=/etc/apache2/sites-enabled/000-default
SUB_DOM_ROOT=subdomains ###Change this line to set the directory for your subdomains
#DOMAIN_NAME=jiggleforce.com
 
##This function checks to make sure the system is using apache2 instead of httpd
function check_apache() {
APACHE_HTTPD=`which apache2`
 
if [ ${APACHE_HTTPD} != "/usr/sbin/apache2" ]; then
        echo "This script is meant for Debian/Apache2 configuration, please ensure that your server is using both Debian and Apache2 before running this script"
        exit
 
else
 
        name_input
fi
}
 
##This function reads in the name of the subdomain that should be created
function name_input() {
read -p "Set the domain name [ex: jiggleforce.com]: " DOMAIN_NAME
echo "You have issued: ${DOMAIN_NAME}"
read -p "Set subdomain name [ex: wordpress]: " NEW_SUB_D
echo "You have issued: ${NEW_SUB_D}"
}
 
##This function creates the required directories and files for each subdomain
function mkdir_sub() {
# bash check if directory exists
if [ -d /${SUB_DOM_ROOT} ]; then
        echo "${SUB_DOM_ROOT} exists...skipping"
else
        echo "Directory does not exist"
        echo "creating ${SUB_DOM_ROOT}..."
        mkdir /${SUB_DOM_ROOT}
fi
 
echo "creating /${SUB_DOM_ROOT}/${NEW_SUB_D}" ; mkdir /${SUB_DOM_ROOT}/${NEW_SUB_D}/ ; echo "...... [done]"
echo "creating /${SUB_DOM_ROOT}/${NEW_SUB_D}/htdocs/" ; mkdir /${SUB_DOM_ROOT}/${NEW_SUB_D}/htdocs/ ; echo "...... [done]"
echo "creating /${SUB_DOM_ROOT}/${NEW_SUB_D}/cgi-bin/" ; mkdir /${SUB_DOM_ROOT}/${NEW_SUB_D}/cgi-bin/ ; echo "...... [done]"
echo "creating /${SUB_DOM_ROOT}/${NEW_SUB_D}/logs/" ; mkdir /${SUB_DOM_ROOT}/${NEW_SUB_D}/logs/ ; echo "...... [done]"
echo "                                             "
 
echo "creating log files: " ; touch /${SUB_DOM_ROOT}/${NEW_SUB_D}/logs/error.log ; touch /${SUB_DOM_ROOT}/${NEW_SUB_D}/logs/access.log ; touch /${SUB_DOM_ROOT}/${NEW_SUB_D}/htdocs/index.html ; echo "....... [done]"
echo "hello world!" >> /${SUB_DOM_ROOT}/${NEW_SUB_D}/htdocs/index.html
}
 
##This function simply edits the apache config file for the new subdomain
function edit_vhost_file() {
echo "####################################################################################" >> ${APACHE_CONFIG}
echo "###                     ${NEW_SUB_D} SUBDOMAIN                                  ####" >> ${APACHE_CONFIG}
echo "####################################################################################" >> ${APACHE_CONFIG}
echo "    " >> ${APACHE_CONFIG}
echo "    ServerAdmin burnsoft@gmail.com" >> ${APACHE_CONFIG}
echo "    ServerName  ${NEW_SUB_D}.${DOMAIN_NAME}" >> ${APACHE_CONFIG}
echo "    ServerAlias ${NEW_SUB_D}.${DOMAIN_NAME}" >> ${APACHE_CONFIG}
echo " " >> ${APACHE_CONFIG}
echo "    # Indexes + Directory Root." >> ${APACHE_CONFIG}
echo "    DirectoryIndex index.html index.htm index.php" >> ${APACHE_CONFIG}
echo "    DocumentRoot /${SUB_DOM_ROOT}/${NEW_SUB_D}/htdocs/" >> ${APACHE_CONFIG}
echo " " >> ${APACHE_CONFIG}
echo "                Options Indexes FollowSymLinks MultiViews +Includes" >> ${APACHE_CONFIG}
echo "                AllowOverride None" >> ${APACHE_CONFIG}
echo "                Order allow,deny" >> ${APACHE_CONFIG}
echo "                allow from all" >> ${APACHE_CONFIG}
echo "        " >> ${APACHE_CONFIG}
echo "     # CGI Directory" >> ${APACHE_CONFIG}
echo "     ScriptAlias /cgi-bin/ /${SUB_DOM_ROOT}/${NEW_SUB_D}/cgi-bin/" >> ${APACHE_CONFIG}
echo "     " >> ${APACHE_CONFIG}
echo "     Options +ExecCGI" >> ${APACHE_CONFIG}
echo "     " >> ${APACHE_CONFIG}
echo " " >> ${APACHE_CONFIG}
echo "    # Logfiles" >> ${APACHE_CONFIG}
echo "    ErrorLog /${SUB_DOM_ROOT}/${NEW_SUB_D}/logs/error.log" >> ${APACHE_CONFIG}
echo "    CustomLog /${SUB_DOM_ROOT}/${NEW_SUB_D}/logs/access.log combined" >> ${APACHE_CONFIG}
echo "    " >> ${APACHE_CONFIG}
 
}
 
##This restarts apache after all the changes have been made
function restart_apache() {
        /etc/init.d/apache2 restart
}
 
####  Main Functions Below ####
check_apache
mkdir_sub
edit_vhost_file
restart_apache
  1. i found this “old post” and i just wanted to share how usefull i find it!
    it saves a lot of hassle once you use it :)

    apreciatted verry much, thank you!

  1. No trackbacks yet.

Unfortunately you don't have Adobe Flash-Player.... Klicken Sie hier fü kostenlosen Adobe Flash-Player.