#! /bin/bash # # ====================== # VHost Setup Script 1.1 # Written by Samet Kilictas # Created: May 2011 # Based on the Siteup Setup Script 0.1 by Command Line Idiot (http://commandlineidiot.com) # You may use, modify, and redistribute this script freely # ======================= # ======================= # set functions # =======================v # make_web_dir is the function used to create web accessible directories # using the appropriate permissions etc... function make_web_dir { mkdir -p $1 chown -Rf www-data:www-data $1 chmod -Rf 755 $1 } # make_index is the function to create a basic index.html file # within the documents directory of the new domain. The variable # for domain name is passed into the file at $dname. You can alter # any of the code between the terms _EOF_ and it will be reflected # in the index.html file. function make_index { cat <<- _EOF_ $domain welcome to $domain _EOF_ } # create a file that contains basic information about the site function make_siteinfo { cat <<- _EOF_ Site name: $domain Site email: $admin Site host: $host Project directories: Web documents: /www/htdocs/$domain Web cgi-bin: /www/cgi-bin/$domain Web logs: /www/logs/$domain/apache2 _EOF_ } # create_site_dirs creates all directories automagically function create_site_dirs { make_web_dir /www/htdocs/$domain make_web_dir /www/cgi-bin/$domain echo "## Setup $domain web directories in /www ##" make_web_dir /www/logs/$domain/apache2 touch /www/logs/$domain/apache2/{access,error}.log echo "## Setup $domain log directories in /www ##" } function delete_site_dirs { rm -Rf /www/htdocs/$domain rm -Rf /www/cgi-bin/$domain echo "## Removed $domain web directories in /www ##" rm -Rf /www/logs/$domain/apache2 echo "## Removed $domain log directories in /www ##" rm -f /etc/apache2/sites-available/$domain } # make_vhost is the function to create a config file that # Apache2 can interpret. The variable for the domain name is passed # into the file at $domain. function make_vhost { cat <<- _EOF_ ServerAdmin $admin ServerName $domain ServerAlias *.$domain DirectoryIndex index.html index.html index.php DocumentRoot /www/htdocs/$domain Options FollowSymLinks AllowOverride All Options -Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all ScriptAlias /cgi-bin/ /www/cgi-bin/$domain AllowOverride None Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch -Indexes Order allow,deny Allow from all ErrorLog /www/logs/$domain/apache2/error.log # Possible values include: debug, info, notice, warn, error, crit, # alert, emerg. LogLevel warn CustomLog /www/logs/$domain/apache2/access.log combined _EOF_ } function create_site { domain=$1 admin=$2 host=$3 # ======================= # header # ======================= clear echo "*** Site Setup ***" # ======================= # set domain name variable # ======================= if [ "$domain" = "" ]; then echo -n "==> domain (without the www. eg, example.com): " read domain fi if [ "$admin" == "" ]; then echo -n "==> Email (eg, admin@example.com): " read admin fi if [ "$host" == "" ]; then echo -n "==> Host is empty(use * for all, or specify a host ip): " read host fi echo echo "*** Setting up files for $domain ***" # ======================= # create needed directories # ======================= create_site_dirs echo echo "*** Directories Created ***" echo # ======================= # build index.html file # ======================= make_index > /www/htdocs/$domain/index.html echo "created /www/htdocs/$domain/index.html" echo # ======================= # build vhost config file # ======================= make_vhost > /etc/apache2/sites-available/$domain echo "*** VHost created ***" echo echo -n "==> Would you like to enable this VHost now [Y/n]? " read enable if [ "$enable" = "y" ] || [ "$enable" = "Y" ] || [ "$enable" = "" ]; then a2ensite $domain /etc/init.d/apache2 reload echo "Done" echo else echo "Continuing without enabling VHost..." echo "To enable VHost run:" echo "a2ensite $domain" echo "/etc/init.d/apache2 reload" echo echo fi # ======================= # exit # ======================= echo "*** Site information ***" echo -e "Site domain:\t$domain" echo -e "Project email:\t$admin" echo -e "Site host:\t$host" echo echo "Project directories:" echo -e "Web directory:\t/www/htdocs/$domain" echo -e "Web cgi-bin:\t/www/cgi-bin/$domain" echo -e "Web logs:\t/www/logs/$domain/apache2/" make_siteinfo > /www/logs/$domain/site_info echo echo echo "*** Finished setting up files for $domain. Goodbye! ***" echo "Project information incl. directories at /www/logs/$domain/site_info" } function destroy_site { domain=$1 force=$2 # ======================= # header # ======================= echo "*** Site Removal ***" # ======================= # set domain name variable # ======================= if [ "$domain" = "" ]; then echo -n "==> domain (without the www. eg, example.com): " read domain fi # ======================= # stop the site on apache # ======================= a2dissite $domain /etc/init.d/apache2 reload if [ "$delete" != "y" ]; then echo -n "==> Are you sure you wish to delete this domain and ALL FILES (including htdocs, configs etc...), if so please type 'Yes, I am!' exactly as displayed: " read sure if [ "$sure" = "Yes, I am!" ]; then delete_site_dirs echo "** All files relating to $domain have been removed. Goodbye! ***" fi fi } function display_usage { cmd=`basename $0` cat <<- _EOF_ VHost Setup Script 1.1 Written by Samet Kilictas To create a domain: $cmd create - , and are not required - as there is an interactive prompt also. To remove a domain: $cmd destroy * WARNING: Use with caution as this command * deletes ALL files, including htdocs. - is not required as there is - an interactive prompt also. To display help/usage: $cmd help _EOF_ } if [ "$1" = "create" ]; then create_site $2 $3 elif [ "$1" = "destroy" ]; then destroy_site $2 else display_usage fi exit