编写安装配置httpd服务基于域名的虚拟主机脚本

要求:
1、自动完成安装,基本配置
2、通过提示输入域名、网站根目录、等信息完成虚拟主机的添加工作

#!/bin/bash

service httpd stop &> /dev/null

conf="/usr/local/apache/conf/httpd.conf"

vhostconf="/usr/local/apache/conf/extra/httpd-vhosts.conf"
bin="/usr/local/apache/bin/apachectl"
rootdir="/www"
mkdir -p $rootdir

grep "vhost" $conf | grep "#" &> /dev/null
if [ $? -eq 0 ]; then
vhost=$(grep "vhost" $conf | sed "s/#//")
sed -i '/vhost/ d' $conf &> /dev/null
echo "$vhost" >> $conf

echo "NameVirtualHost *:80" > $vhostconf

echo "<Directory $rootdir>

order allow,deny
allow from all
</Directory>">> $vhostconf
fi

while true

do
read -p "hostname of FDQN:" fdqn
read -p "directory of website:" sitedir
mkdir -p $rootdir/$sitedir/html
mkdir -p $rootdir/$sitedir/logs

domain=$(echo $fdqn | awk -F. '{print $2"."$3}')

echo "<VirtualHost *:80>
   ServerAdmin
   DocumentRoot $rootdir/$sitedir/html
   ServerName  $fdqn
   ErrorLog $rootdir/$sitedir/logs/error_log
   CustomLog $rootdir/$sitedir/logs/access_log common
</VirtualHost>" >> $vhostconf

read -p "continue to do?(yes/no)" yn

if [ $yn = no ];then
 $bin start &> /dev/null  
 exit 0
fi
done