Skip to content

Instantly share code, notes, and snippets.

@summic
Created March 12, 2010 08:46
Show Gist options
  • Save summic/330167 to your computer and use it in GitHub Desktop.
Save summic/330167 to your computer and use it in GitHub Desktop.
快速添加虚拟主机脚本
#! /bin/bash
#
# =======================
# Snow Leopard 创建虚拟主机脚本
# Written by Summer Wang <[email protected]>
# Blog: http://summic.com
# Created: feb 21 2010
# =======================
function make_web_dir
{
mkdir -p $1
chmod -Rf 755 $1
}
function create_site_dirs
{
make_web_dir $HOME/Sites/$domain
echo "## Setup $domain web directories in $HOME/Sites/ ##"
touch /private/var/log/apache2/$domain.com-{access,error}.log
echo "## Setup $domain log directories in $HOME/Sites/ ##"
}
function delete_site_dirs
{
rm -Rf $HOME/Sites/$domain
echo "## Removed $domain web directories in $HOME/Sites/ ##"
rm -f /private/var/log/apache2/$domain.com-{access,error}.log
echo "## Removed $domain log directories in /private/var/log/apache2/ ##"
rm -f /etc/apache2/vhosts/$domain.conf
}
function makeConf
{
cat <<- _EOF_
<VirtualHost *:80>
ServerAdmin $admin
ServerName $domain
ServerAlias www.$domain
DocumentRoot "$HOME/Sites/$domain"
<Directory "$HOME/Sites/$domain">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>
ErrorLog "/private/var/log/apache2/$domain.com-error.log"
CustomLog "/private/var/log/apache2/$domain.com-access.log" common
</VirtualHost>
_EOF_
}
function addHosts
{
cat <<- _EOF_
127.0.0.1 $domain
127.0.0.1 www.$domain
_EOF_
}
function add
{
domain=$1
admin=$2
echo "*** Site Setup ***"
if [ "$domain" = "" ]; then
echo -n "==> domain (without the www. eg, example.com): "
read domain
fi
if [ "$admin" == "" ]; then
echo -n "==> Email (eg, [email protected]): "
read admin
fi
echo
echo "*** Setting up files for $domain ***"
create_site_dirs
echo
echo "*** Directories Created ***"
echo
makeConf > /etc/apache2/vhosts/$domain.conf
echo "*** VHost created ***"
echo
addHosts >> /etc/hosts
echo "*** Hosts table Added ***"
echo -n "==> Would you like to enable this VHost now [Y/n]? "
read enable
if [ "$enable" = "y" ] || [ "$enable" = "Y" ] || [ "$enable" = "" ]; then
apachectl -k restart
else
echo "Continuing without enabling VHost..."
echo "To enable VHost run: apachectl -k restart"
echo
fi
echo "*** Site information ***"
echo -e "Site domain:\t$domain"
echo -e "Project email:\t$admin"
echo
echo "Project directories:"
echo -e "Web directory:\t$HOME/Sites/$domain"
echo -e "Web logs:\t/private/var/log/apache2/$domain.com-{access,error}.log"
echo
echo "*** Finished setting up files for $domain. Goodbye! ***"
}
function del
{
domain=$1
force=$2
echo "*** Site Removal ***"
if [ "$domain" = "" ]; then
echo -n "==> domain (without the www. eg, example.com): "
read domain
fi
apachectl -k stop
if [ "$force" != "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
apachectl -k start
}
function help
{
cmd=`basename $0`
cat <<- _EOF_
虚拟主机创建脚本
To add a domain:
$cmd add <domain> <admin-email>
- <domain> and <admin-email> are not required
- as there is an interactive prompt also.
To remove a domain:
$cmd del <domain>
* WARNING: Use with caution as this command
* deletes ALL files, including htdocs.
- <domain> is not required as there is
- an interactive prompt also.
To display help/usage:
$cmd help
_EOF_
}
if [ "$1" = "add" ]; then
sudo add $2 $3
elif [ "$1" = "del" ]; then
sudo del $2
else
help
fi
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment