脚本练习,写一个脚本,实现本地Web服务器的配置:
1、检查httpd包是否已经安装,如果没有,则使用yum命令安装之;(需要事先配置好yum源)
2、取消中心主机,新建5个基于域名的虚拟主机,并分别提供一个测试主页,主页内容为其对应的FQDN:
www.magedu.com, /vhosts/magedu
bbs.magedu.com, /vhosts/bbs
wp.magedu.com, /vhosts/wordpress
pw.magedu.com, /vhosts/pw
pma.magedu.com, /vhosts/phpmyadmin
3、检查修改后的配置文件语法,如果没有问题,则启动httpd服务,并要求以后服务会开机自动启动;


#!/bin/bash
#Author honway
#ScriptName AutoSetWebServer
SetYUM() {
echo "=============================================="
echo "Start backup files."
echo "backup your .repo file to .repo.bak!"
echo "=============================================="
YUM_CONF_DIR=/etc/yum.repos.d/
for Y in $YUM_CONF_DIR/*.repo; do
mv $Y $Y.bak &>/dev/null
done
echo "=============================================="
echo "Backup successfully."
echo "=============================================="
echo "Create a yum config file."
cat >> $YUM_CONF_DIR/yumconf.repo <<EOF
[Server]
name=Server
baseurl=ftp://172.16.0.1/pub/Server
gpgcheck=0
enabled=1

[VT]
name=VT
baseurl=ftp://172.16.0.1/pub/VT
gpgcheck=0
enabled=1

[Cluster]
name=Cluster
baseurl=ftp://172.16.0.1/pub/Cluster
gpgeheck=0

[ClusterStorage]
name=ClusterStorage
baseurl=ftp://172.16.0.1/pub/ClusterStorage
gpgcheck=0
enabled=1

[errata]
name=errata
baseurl=ftp://172.16.0.1/pub/errata
gpgeheck=0
enabled=0
EOF
echo "OK!"
echo "=============================================="
}

CreateTestPage(){
echo "$u" > $URL_DIR/index.html
}
SetYUM

if ! `rpm -qa | grep httpd &>/dev/null`; then
echo "===============================>"
echo "Start install httpd.please wait..."
yum -y install httpd &>null
else
echo "install is not sucess,something error."
fi
SetVirtualConfig(){
sed -i 's@\(^DocumentRoot[[:space:]]\{1,\}"/var/www/html"\)@#\1@g' /etc/httpd/conf/httpd.conf
sed -i 's@^#\(NameVirtualHost[[:space:]]\{1,\}[^[:space:]]\{1,\}:80$\)@\1@g' /etc/httpd/conf/httpd.conf
}
SetVirtualConfig
mkdir -p /vhosts/{magedu,bbs,wp,pw,pma} &>/dev/null
FQDN=magedu.com
f=/etc/httpd/conf/httpd.conf
for u in {www.$FQDN,bbs.$FQDN,wp.$FQDN,pw.$FQDN,pma.$FQDN}; do
case "$u" in
"www.magedu.com")
URL_DIR=/vhosts/magedu
cat >>$f <<EOF
<VirtualHost *:80>
ServerName $u
DocumentRoot $URL_DIR
</VirtualHost>
EOF
CreateTestPage ;;
"bbs.magedu.com")
URL_DIR=/vhosts/bbs
cat >> $f <<EOF
<VirtualHost *:80>
ServerName $u
DocumentRoot $URL_DIR
</VirtualHost>
EOF
CreateTestPage ;;
"wp.magedu.com")
URL_DIR=/vhosts/wp
cat >>$f <<EOF
<VirtualHost *:80>
ServerName $u
DocumentRoot $URL_DIR
</VirtualHost>
EOF
CreateTestPage ;;
"pw.magedu.com")
URL_DIR=/vhosts/pw
cat >>$f <<EOF
<VirtualHost *:80>
ServerName $u
DocumentRoot $URL_DIR
</VirtualHost>
EOF
CreateTestPage ;;
"pma.magedu.com")
URL_DIR=/vhosts/pma
cat >>$f <<EOF
<VirtualHost *:80>
ServerName $u
DocumentRoot $URL_DIR
</VirtualHost>
EOF
CreateTestPage ;;
*)
echo "Error..." ;;
esac
done
if `service httpd configtest`; then
chkconfig --level 345 httpd on
service httpd start
fi