Created
July 21, 2013 06:19
-
-
Save lubobill1990/6047683 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
site_available_dir=/etc/apache2/sites-available/ | |
site_enabled_dir=/etc/apache2/sites-enabled/ | |
server_admin=lubobill1990@163.com | |
action=() | |
name= | |
port= | |
usage (){ | |
echo "$0 -C|-R -N site_name -P port_number -D document_root" | |
} | |
test_port(){ | |
port=$1 | |
res=`awk '/Listen '$port'$/ {print} ' $site_available_dir../ports.conf ` | |
echo $res | grep -q 'Listen' && echo 'used' || echo 'unused' | |
} | |
test_site_name(){ | |
site_name=$1 | |
if [ -f $site_available_dir$site_name ];then | |
echo 'exist' | |
else | |
echo 'notexist' | |
fi | |
} | |
ARGS=`getopt -o CRUN:P:D:h -l create-site,remove-site,update-site,name:,port:,directory:,help -- "$@"` | |
[ $? -ne 0 ] && usage | |
# Note the quotes around `$TEMP': they are essential! | |
eval set -- "$ARGS" | |
while true | |
do | |
case "$1" in | |
-C|--create-site) | |
action=("${action[@]}" 'c') | |
;; | |
-R|--remove-site) | |
action=("${action[@]}" 'r') | |
;; | |
-U|--update-site) | |
action=("${action[@]}" 'u') | |
;; | |
-N|--name) | |
name="$2" | |
shift | |
;; | |
-P|--port) | |
port="$2" | |
shift | |
;; | |
-D|--directory) | |
dir="$2" | |
shift | |
;; | |
-h|--help) | |
usage | |
exit | |
;; | |
--) | |
shift | |
break | |
;; | |
esac | |
shift | |
done | |
if [ ${#action[@]} -gt 1 ];then | |
echo 'Count of site operation can be only one' | |
exit 1 | |
elif [ ${#action[@]} -eq 0 ];then | |
echo 'You should select a operation from create, remove and update' | |
exit 1 | |
fi | |
case "$action" in | |
c) | |
res=`test_port $port` | |
case "$res" in | |
used) | |
echo "The port $port is already used, you should choose another port number" | |
exit 1 | |
;; | |
unused) | |
echo "Can use the port $port" | |
;; | |
esac | |
res=`test_site_name $name` | |
case "$res" in | |
exist) | |
echo "The site name $name is already used, you should choose another name" | |
exit 1 | |
;; | |
notexist) | |
echo "Can use the site name $name" | |
;; | |
esac | |
if [ -z "$dir" ];then | |
echo 'You have not set the root directory of the site' | |
exit 1 | |
else | |
echo "Root directory is $dir" | |
fi | |
awk '{sub(/\$document_root/,"'$dir'")}{sub(/\$port_number/,"'$port'")}{sub(/\$server_admin/,"'$server_admin'")}{print}' $site_available_dir"sample" >$site_available_dir$name | |
ln -s $site_available_dir$name $site_enabled_dir$name | |
echo "#creator: $SUDO_USER">>$site_available_dir../ports.conf | |
echo "NameVirtualHost *:$port">>$site_available_dir../ports.conf | |
echo "Listen $port">>$site_available_dir../ports.conf | |
echo "Site $name created successfully on port $port, the directory of document root is $document_root" | |
;; | |
r) | |
res=`test_site_name $name` | |
case $res in | |
exist) | |
echo "There exists a site named $name" | |
;; | |
notexist) | |
echo "There is not a site named $name, choose an existed site to remove" | |
exit 1 | |
;; | |
esac | |
port=`grep -Po '(?<=VirtualHost \*:).*(?=>)' $site_available_dir$name` | |
echo $port | |
if [ -z $port ];then | |
echo 'Can not find port information in the file of ' $site_available_dir$name | |
exit 1 | |
fi | |
rm $site_available_dir$name && rm $site_enabled_dir$name | |
awk '{if($0!~/^Listen '$port'\s*/ && $0!~/^NameVirtualHost \*:'$port'\s*/) print $0}' $site_available_dir../ports.conf > $site_available_dir../ports.conf.$$ && mv $site_available_dir../ports.conf.$$ $site_available_dir../ports.conf | |
echo "Site $name removed successfully" | |
;; | |
u) | |
;; | |
esac | |
echo "Restarting Apache Server ..." | |
service apache2 restart | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment